Java basics 09 class data and class methods

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

We have always defined it to produce objects.Class). Objects are functional entities, while classes are object types. This is a basic concept of object-oriented.

In inheritance, we regard classes as the main body that can be expanded, which improves our understanding of "classes.

The class itself has many points worth discussing. We will continue to explore.

  Static Data Member

Some data is used to express the status of the class. For example, for the human class, we can use "Population" to represent the total number of objects in the human class. "Population" directly describes the state of the class, rather than an object.

The population of the human class is 8.

 

All objects of the classShare"Population" data. Such data is calledClass field).

In the class definition, we useStaticKeyword to declare a class data member, for example:

 Class  Human {
/**
* Constructor
*/
PublicHuman (IntH)
{
This. Height = h;
}

/** * Accessor */ Public Int Getheight (){ Return This . Height ;} /** * Mutator */ Public Void Growheight ( Int H ){ This . Height = This . Height + H ;} /** * Breath */ Public Void Breath () {system. Out. println ( "Hu ..." );} Private Int Height;
Private Static IntPopulation; Public Static Boolean Is_mammal = True ;}

We have defined two types of data members: Population and is_mammal. All human objects share a population data. The is_mammal (a mammal) attribute of any human object is true.

Class data members must also be setAccess permission. For class data members declared as public, you can useClass. FieldOrObject. Field(If an object of this class exists. Both of these access methods are reasonable, because class data members can be considered to beClass attributes, Can be considered as allAttributes shared by members. If the class data member is defined as private, the class data member can only access from the class.

(Set is_mammal to public, just for demonstration. This is dangerous.Human. is_mammal = false;All humans suffer. Or the basic principle, we should try to set the data to private .)

  Static Method

We can also haveClass MethodThat is, the method declared as static. The class method represents the actions that can be implemented by the class, and the operations do not involve a specific object. If a method is declared as static, it can only call static data and methods, rather than non-static data and methods.

In fact, in the static method, this and super parameters are not implicitly passed. We cannot reference data and methods of objects (this is what we want ).

As mentioned above, we have the following relationships:

The red dotted line indicates no access. That is, the data of the object cannot be accessed in the class method.

 

Next we will add a static method.Getpopulation (), This method returns static data population:

 Class Human {
/**
* Constructor
*/
PublicHuman (IntH)
{
This. Height = h;
}
/** * Accessor */ Public Int Getheight (){ Return This . Height ;} /** * Mutator */ Public Void Growheight ( Int H ){ This . Height = This . Height + H ;} /** * Breath */ Public Void Breath () {system. Out. println ( "Hu ..." );} Private Int Height; /* * Static method, access population */ Public Static Int Getpopulation (){ Return Human. Population ;} Private Static Int Population; Private Static Boolean Is_mammal = True ;}

 

When calling a class method, we can useClass. Method ()You can also useObject. Method (). For example, use the following test class test:

Public ClassTest {Public Static VoidMain (string [] ARGs) {system. Out. println (human. getpopulation (); Human aperson=NewHuman (160); system. Out. println (aperson. getpopulation ());}}

We call the class method outside the class definition in two ways.Getpopulation ().

 

Object method modification class data

We can see that object methods can be used for metadata-type data. This is a very useful concept. The status of the class may change with the object. For example, "Population" should increase by 1 with the generation of an object. We can modify the "Population" data of the class in the object method. In the following constructor, we will use the struct class data member. The constructor here isNon-staticObject method:

 Class  Human {  /**  * Constructor  */      Public Human ( Int  H ){  This . Height = H; human. populatin = Human. Population + 1;}  /**  * Accessor  */      Public   Int  Getheight (){  Return   This  . Height ;}  /**  * Mutator  */      Public   Void Growheight ( Int H ){  This . Height = This . Height + H ;}  /**  * Breath  */      Public   Void  Breath () {system. Out. println ( "Hu ..." );}  Private   Int  Height;  /* * Static method, access population  */      Public   Static   Int  Getpopulation (){  Return  Human. Population ;}  Private   Static   Int  Population;  Private   Static   Boolean Is_mammal =True  ;} 

Each time we create an object, the class data is modified through the object construction method, adding 1 to the population class data. In this way, population can reflect the total number of objects belonging to this class in real time (you can create multiple objects in test and print human. Population ).

In addition to the example of the preceding constructor method, we can also extract class data in Common Object methods.

 

Final

FinalThe basic meaning of the keyword is: this data/method/classCannot be changed.

    • Final data of the basic type: constant value, which can only be assigned once and cannot be modified.
    • Final method: This method cannot be overwritten. The default private method is final.
    • Final class: the class cannot be inherited.

A common object can also have a final keyword, indicating that the object reference cannot be modified. That is, the reference can only point to one object. However, the object content can be changed (similar to the static pointer in C ). We will introduce object references later.

If a basic data type is both final and static, it indicates a free storage value. This is very suitable for storing some constants, such as the circumference rate.

 

Summary

Static Field, static method

Class. static_method ()

Final

 

Welcome to continue reading the "Java quick tutorial" SeriesArticle

 

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.