Using methods with objects

We learned that an object is a container for data. We can store specific data inside the objects in its variables, and we can also write some more useful methods. OOP is a very neat and flexible concept. There is nothing stopping us from using our encapsulated object and passing it as a parameter to the other method. Let's write the following code as an example where the class name is Person:

Using methods with objects

I removed most of the variables from the Person class to make this example clearer. If you are writing this example in the same Unity Project as the previous example, you will get some errors in the Family class we were using before. I recommend starting this example in a new Unity Project and the class here is LearningObjects:

Using methods with objects

Yes, lots of new code to analyze, awesome! What we are trying to do here is create two instances of the Person object. We'll cross-reference them by assigning a public member spouse, and then call the method within the object class itself. Let's analyze this step by step—we do not want to get you confused.

Let's talk about the Person class first. You most certainly understand what an object is and we can hold variables in them. However, I want to talk about the IsMarriedWith method inside the class itself. As you can see, this is a return type method that returns a bool value and takes one parameter. Notice the type of the parameter. The IsMarriedWith method takes another instance of the Person class.

Line 13 of the Person class checks whether there is any value stored in the spouse variable.

The easiest way to check if there is any value assigned to the variable is comparing the variable to null. If the variable isn't null, it means there is something assigned to the variable.

Note

The null keyword is a literal that represents a null reference, one that does not refer to any object. It is the default value of reference-type variables.

So, if spouse isn't assigned, it means our instance isn't married and we are returning false in line 27. Let's talk about the LearningObjects class. It must be pretty obvious to you what is going on there. We are creating two instances of the Person class and assigning the values. Notice lines 22 and 23. We are assigning woman.spouse with a man object and man.spouse with a woman object. I asked you to do it this way to demonstrate that you can easily reference objects inside other objects even if both of them are the same type.

Line 26 is where the main logic happens. We are calling the IsMarriedWith method on the man instance and passing woman object. Yet again, this is to demonstrate how flexible objects are. The condition in line 26 will be true if two Person type objects are married. If so, the suitable message is printed out in the Unity Console panel.

Go ahead and play with the code. Try to add some more methods or variables to the Person class. Why not add an age public member again and write a method to return the total number of years? Sky is the limit! The best you can do is write a lot of code. Learning by experience is the fastest way to become a decent programmer. Fingers crossed!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset