In the article "From Hello World to Object-Oriented" in the Basic Java Tutorial, we have a preliminary understanding of objects. Data members in an object represent the state of the object. Objects can perform methods that represent specific actions.
In addition, we also learned about classes. Objects of the same class belong to the same type. We can define classes and use that definition to create objects.
Let's dig deeper into objects. Learn some details about methods and data members in Java.
Calling data members of the same object
Methods can call the object's data members. For example, below we add a getHeight() method to the Human class. This method returns the value of the height data member:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human();
System.out.println(aPerson.getHeight());
}
}
classHuman
{/**
*accessor
*/
int getHeight()
{
return this.height;
}
int height;
}
We have added the getHeight method. This method has a return value of type int. Return is used in Java to return a value.
Note this, which is used to refer to the object itself. When we create an aPerson instance, this represents the aPerson object. this.height refers to the height of aPerson.
this is an implicit argument. When a method is called, even though the method's parameter list does not include this, Java will "silently" pass this parameter to the method.
(There are some special methods that do not pass this implicitly, we will see it later)
this is not required, the above method can be written as:
Copy the code code as follows:
/**
*accessor
*/
int getHeight()
{
return height;
}
Java will determine by itself that height is a data member in the class. But it will be clearer using this.
We also saw the /** comments */ method of adding comments.
method parameter list
Method definitions in Java are similar to functions in C language. Java methods can also receive an argument list (argument list) in parentheses after the method name. Next we define a growHeight() method. The function of this method is to increase the height of a person:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human();
System.out.println(aPerson.getHeight());
aPerson.growHeight(10);
System.out.println(aPerson.getHeight());
}
}
classHuman
{
/**
*accessor
*/
int getHeight()
{
return this.height;
}
/**
* pass argument
*/
void growHeight(int h)
{
this.height = this.height + h;
}
int height;
}
In growHeight(), h is the passed parameter. In the class definition, the type of the parameter (int) is stated. Inside a specific method, we can use this parameter. This parameter is only valid within the scope of this method, that is, growHeight().
When calling, we pass 10 to growHeight(). aPerson's height is increased by 10.
Call other methods on the same object
Inside a method, other methods of the same object can be called. When calling, use the form of this.method(). We also remember that this refers to the object. So this.method() refers to the method() method of the object itself.
For example, the following repeatBreath() function:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human();
aPerson.repeatBreath(10);
}
}
classHuman
{
void breath()
{
System.out.println("hu...hu...");
}
/**
* call breath()
*/
void repeatBreath(int rep)
{
int i;
for(i = 0; i < rep; i++) {
this.breath();
}
}
int height;
}
In order to facilitate looping, in the repeatBreath() method, we declare an object i of type int. The scope of i is limited to the scope of the repeatBreath() method.
(This is similar to automatic variables in C language functions)
Data member initialization
In Java, there are many ways to initialize data members. For example, in the getHeight() example above, although we never provide a value for height, Java chooses a default initial value of 0 for us.
Default initial values for data members of basic types:
1. Numeric type: 0
2.Boolean value: false
3. Other types: null
We can provide the initial value of the data member while declaring it. This is called explicit initialization. The display initialization value must be written hard in the program:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human();
System.out.println(aPerson.getHeight());
}
}
classHuman
{/**
*accessor
*/
int getHeight()
{
return this.height;
}
int height = 175;
}
Here, the initial value of the data member height is 175, not the default 0.
There are other ways to initialize objects in Java, which I will introduce later.
Summarize
return
this, this.field, this.method()
Default initial value, explicit initialization