Java Basic 02 methods and data members

Source: Internet
Author: User
Tags class definition

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

In Java Foundation 01 from HelloWorld to object-oriented , we initially learned about objects (object). 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.

We went further into the object. Learn some of the details of methods and data members in Java.

calling data members of the same object

Method 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{ Span style= "color: #0000ff;" >public static voidnew Human (); System.out.println (Aperson.getheight ()); }}class Human{/**< Span style= "color: #008000;" > * accessor */intreturn this .height;} 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.

(There are some special methods that do not implicitly pass this, which we will see later)

This is not required, and the above method can be written as:

    /**     * accessor * */         intreturn height;    

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

We also saw a way to add annotations to/** comments * *.

parameter list of the method

The method definition in Java 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:

PublicClasstest{PublicStaticvoidMain (string[] args) {Human Aperson =NewHuman (); System.out.println (Aperson.getheight ());
Aperson.growheight (10);
System.out.println (Aperson.getheight ()); }}class Human{/** * accessor */intreturn 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 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, you 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:

PublicClasstest{PublicStaticvoidMain (string[] args) {Human Aperson =New Human (); Aperson.repeatbreath (10class human{void breath () {System.out.println ("hu...hu ..." /** call Breath () */ Span style= "color: #0000ff;" >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.

(This is similar to an automatic variable in a C language function)

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{ Span style= "color: #0000ff;" >public static voidnew Human (); System.out.println (Aperson.getheight ()); }}class Human{/**< Span style= "color: #008000;" > * accessor */intreturn this .height;} int height = 175;        

Here, the initial value of the data member height is 175, not the default of 0.

There are other ways to initialize objects in Java, which I'll cover later.

Summary

Return

This, This.field, This.method ()

Default initial value, explicit initialization

Java Basic 02 methods and data members

Related Article

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.