Java basics 02 methods and data members

Source: Internet
Author: User

From HelloWorld to object-oriented in Java Foundation 01, we have a preliminary understanding of objects ). The data member in the object indicates the state of the object. Objects can execute methods to indicate specific actions.

In addition, we also understand the class ). Objects of the same class belong to the same type ). We can define classes and use this definition to generate objects.

We further dive into objects. Measure the test taker's knowledge about methods and data members in Java.

 

 

Call data members of the same object
You can call the data member of this object. For example, we will add a getHeight () method to the Human class. This method returns the value of the height data member:


Public class Test
{
Public static void main (String [] args)
{
Human aPerson = new Human ();
System. out. println (aPerson. getHeight ());
}

}

Class Human
{/**
* Accessor
*/
Int getHeight ()
{
Return this. height;
}

Int height;
} We added the getHeight method. This method has a return value of the int type. Return value is used in Java.

Note that this is used to refer to the object itself. When we create an aPerson instance, this indicates the aPerson object. This. height indicates the height of aPerson.

This is an implicit parameter (implicit argument ). When a method is called, although the parameter list of The method does not contain this, Java will silently pass this parameter to the method.

(Some special methods will not implicitly pass this, we will see it later)

 

In fact, this is not necessary. The above method can be written:


/**
* Accessor
*/
Int getHeight ()
{
Return height;
} Java judges by itself that height is a data member in the class. However, using this is clearer.

We also see the method for adding comments to/** comments.

 

Method parameter list
The method definition in Java is similar to that in C. The Java method can also receive the parameter list (argument list), which is placed in brackets after the method name. Next we define a growHeight () method. The function of this method is to increase the height of a 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
{
/**
* Accessor
*/
Int getHeight ()
{
Return this. height;
}
/*** Pass argument */void growHeight (int h)
{
This. height = this. height + h;
}
Int height;
} We can see that in growHeight (), h is the passed parameter. In the class definition, we need to specify the parameter type (int ). In the subsequent method definition, we can use this parameter. This parameter is valid only for this method.

During the call, we pass 10 to growHeight (). The height of aPerson increased by 10.

 

Call other methods of the same object
We can call other methods of the same object within the method. This. method () is used for calling. We still remember that this refers to the object. Therefore, this. method () can clearly indicate that the method to be called is the method () method of the object.

 

 

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 ...");
}


/**
* Call breath ()
*/
Void repeatBreath (int rep)
{
Int I;
For (I = 0; I <rep; I ++ ){
This. breath ();
}
}
Int height;
} Note that in the repeatBreath () method, we declare the int type object I. The scope of I is limited to the scope of the repeatBreath () method. This is similar to automatic variables in C functions.

 

Data member Initialization

In Java, data members can be initialized in multiple ways. For example, in the above getHeight () example, although we have never provided the value of height, Java selects a default value of 0 for us.

Default values of basic data members:

Numeric: 0

Boolean value: false

Default Value of data members of other types: null

 

We can also specify the initial values of data members when declaring data members in the class. This is called explicit initialization ).

 


Public class Test
{
Public static void main (String [] args)
{
Human aPerson = new Human ();
System. out. println (aPerson. getHeight ());
}

}

Class Human
{/**
* Accessor
*/
Int getHeight ()
{
Return this. height;
}

Int height = 175;
}

We can see that the initial value of the data member height is 175, rather than the default value of 0.

There are other important methods for initializing objects in Java. I will introduce them later.

 

Summary
Return

This, this. field, this. method ()

Default initial value, explicitly initialized

 

 

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.