Java Basic 02 methods and data members (reprint)

Source: Internet
Author: User

The data members in the object represent the state of the object. An object can execute a method that represents a specific action.

In addition, we have learned about classes (class). Objects of the same class belong to the same type. We can define the class and use that definition to produce the object. A data member method that invokes the same object can invoke the data member of the object. For example, let's add a getheight () method to the human class. The method returns the value of the height data member: public class test{
public static void Main (string[] args) {
Human Aperson = new Human ();
Aperson.breath ();
System.out.println (Aperson.height);
}
}
Class human{
void Breath () {
System.out.println ("Hu.. Hu.. ");
}
int height;
}
We have added the GetHeight method. This method has a return value of type int. Return values are returned in Java using return.

Note this, which is used to refer to the object itself. When we create an Aperson instance, this represents the object of Aperson. This.height refers to the height of Aperson.

This is an implicit parameter (implicit argument). Method invocation, although the parameter list of the method does not This,java will "silently" pass the This parameter to the method.

This is not required, and the above method can be written as: int getheight () {
return height;
}

Java will itself judge that height is a data member in a class. But using this will be more clear.

/** Comments */method of adding annotations. (You can display a commented message when you click a parameter later)

Method's parameter list in Java, the method definition is similar to a function in the C language. The Java method can also receive a parameter list (argument list), placed in parentheses after the method name. Here we define a method of Growheight (), the function of which is to increase the height of the person: 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);
}
}
Class human{
int GetHeight () {
return this.height;
}
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 described. Within a specific method, we can use this parameter. This parameter is valid only within the range of the method, that is, Growheight (). At the time of invocation, we pass 10 to Growheight (). The height of the Aperson increased by 10.

Other methods that call the same object inside a method can call other methods of the same object. In the call, use the form This.method (). We remember that this refers to the object. So This.method () refers to the method () of the object itself. For example, the following Repeatbreath () function: public class test{
public static void Main (string[] args) {
Human Aperson = new Human ();
Aperson.repeatbreath (10);
}
}
Class human{
void Breath () {
System.out.println ("Hu.. Hu.. ");
}
void Repeatbreath (int rep) {
int i;
for (i = 0;i < rep;i++) {
This.breath ();
}
}
int height;
}
For ease of circulation, in the Repeatbreath () method, we declare an object of type int i. The scope of I is scoped to the scope of the Repeatbreath () method. data member initialization

In Java, data members have several ways of initializing (initialize). For example GetHeight () above, although we never provide a height value, Java has chosen a default initial value of 0 for us.

Default initial values for data members of the base type:

    • Numeric type: 0
    • Boolean value: False
    • Other types: null
We can provide the initial values of the data members at the same time as the declaration data member. This is called an explicit initialization (explicit initialization). Displays the initialized values to be hard written in the program: public class test{
public static void Main (string[] args) {
Human Aperson = new Human ();
System.out.println (Aperson.getheight ());
}
}
Class human{
int GetHeight () {
return this.height;
}
int height = 175;
}
Here, the initial value of the data member height is 175, not the default of 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.