Java basics: 29th member Methods

Source: Internet
Author: User

Class member methods are used to operate member variables. They can be divided into three types: constructor methods, which are used to initialize member variables; accessors methods, assign values to member variables or obtain the value of member variables. business methods are used to complete specific functions.

1. Constructor

The constructor is used to initialize member variables. The constructor has the following features:

The method name is the same as the class name;

Method has no return value type.

For example:

Public class person {

Private int height;

Private date birthday;

Private string name;

Public Person (){

}

}

The constructor of a class can have multiple constructor methods, which are called the overload of constructor methods. Different constructor Methods initialize member variables in different ways. The following three constructor methods of the person class are provided:

Public Person (){

}

Public Person (string name ){

This. Name = Name;

}

Public Person (string name, int height ){

This. Name = Name;

This. Height = height;

}

You can call other constructor methods in the constructor to reduce repeated code. To call the constructor, you can use this (parameter list). The above 3rd constructor methods can be written as follows:

Public Person (string name, int height ){

This (name );

This. Height = height;

}

Note: the code that calls other constructor must be placed in the first line of the constructor. The following code is incorrect:

Public Person (string name, int height ){

This. Height = height;

This (name); // This is incorrect and must be placed in the first line

}

The usage of this keyword is described later.

2. Access Method

Generally, member variables are private. To provide external access, public methods must be provided, including the get method for obtaining attribute values and the Set Method for assigning attribute values. For example, the preceding height attribute should provide the following two methods:

Public int getheight (){
Return height;
}

Public void setheight (INT height ){
This. Height = height;
}

The naming rule of the set method is set with the attribute name, and the first letter of the attribute name is capitalized. For example, the setbirthday method should be provided for the attribute birthday. Generally, the Return Value of the set method is usually void. The parameter type of the method is the same as that of the Operation attribute;

The naming rule of the get method is get with the attribute name, and the first letter of the attribute name is capitalized. For example, if the attribute is birthday, the getbirthday method should be provided. Generally, the Return Value Type of the get method is the same as that of the attribute to be accessed. No parameters are required. Therefore, the parameter list of the method is empty;

For Boolean attribute values, the method for obtaining attribute values can sometimes be written as the is + attribute method. For example, if an attribute indicates whether to enable the attribute, the method name can be written as isopen, and the definition of the method can be written as follows:

Public Boolean isopen (){

Retrun open;

}

For the array type (for details about the array content, referJava basics: 14th Array), You can get the value of the entire array, you can also get the value of an element in the array, you can set the value of the entire array, you can also set the value of an element in the array, therefore, four methods should be provided, such as attribute int [] a. The following methods should be provided:

Public int [] geta (){
Return;
}

Public void Seta (INT [] ){
This. A =;
}
 
Public void Seta (INT value, int index ){
A [Index] = value;
}
 
Public int geta (INT index ){
Return a [Index];
}

The first two Methods Operate the entire array, and the last two methods operate on a cell in the array.

3. Business Methods

Business methods are used to describe the functions of objects. For the definition and writing of business methods, refer to the method definition described above (Java basics: 15th usage)

The previous section introduced the member variables. This section describes the member methods and describes how to create and call an object using a class.

Last time: 28th member variablesNext time: 30th lecture on Object creation and access to Li xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5

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.