Data and class methods like the Java Basics Tutorial _java

Source: Internet
Author: User
Tags class definition inheritance

We have been defining classes (class) for the purpose of generating objects. An object is a functional entity, and a class is a type taxonomy of an object. This is a basic concept of object-oriented.

In Inheritance (inheritance), we see the class as a subject that can be expanded, which improves our understanding of "class".

The class itself has a lot to discuss. We will continue to deepen

Static data member

Some data is used to describe the state of a class. For example, human class, we can use "population" to represent the total number of objects in the human class. "Population" directly describes the state of a class, not an object.

The population of the human category is 8

All objects of the class share the population data. Such data are referred to as class data members (classes field).

In the class definition, we use the static keyword to declare class data members, such as:

Copy Code code as follows:

Class Human
{
/**
* Constructor
*/
Public Human (int h)
{
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...hu ...");
}

private int height;

private static int population;
public static Boolean is_mammal = true;

}

We have defined two class data members: Population and is_mammal. All human objects share a population data, and the properties of any human object's is_mammal (which are mammals) are true.

Class data members also want to set access permissions. Class data members declared as public can be accessed directly from the outside by means of Class.field or Object.field (if an object exists for that class). Both types of access are reasonable, because class data members can be considered properties of a class and can be considered properties shared by all members. If a class data member is defined as private, the class data member can only be accessed from within the class.

(The Is_mammal is set to public for demonstration purposes only.) This is a dangerous thing to do if someone uses human.is_mammal=false, and all humans suffer. Or the basic principle, to try to set the data as private. )

static method

We can also have a class method, which is a method of declaring static. A class method represents an action that a class can implement, where the action does not involve a specific object. If a method is declared static, it can only invoke static data and methods, not static data and methods.

In fact, in the static method, there will be no implicitly-passed this and super parameters. We have no way of referencing the data and methods that belong to the object (which is exactly the effect we want).

To synthesize the above, we have the following relationship:

A dashed red line indicates no access. In other words, the object's data cannot be accessed in a class method.

Here we add a static method Getpopulation (), which returns the static data population:

Copy Code code as follows:

Class Human
{
/**
* Constructor
*/
Public Human (int h)
{
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...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 call it by Class.method () or by Object.Method (). For example, use the test class below:

Copy Code code as follows:

public class Test
{
public static void Main (string[] args)
{
System.out.println (Human.getpopulation ());
Human Aperson = new Human (160);
System.out.println (Aperson.getpopulation ());
}
}

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

Object methods Modify class data

We see that object methods can access class data. This is a very useful concept. The state of a class may change with the object. For example, "population," it should increase by 1 as an object is produced. We can modify the "population" data of the class in the object's method. We are accessing class data members in the construction method below. The constructor here is a non static method, that is, an object's method:

Copy Code code as follows:

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...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 by the constructor method of the object, increasing by 1 for the population class data. In this way, population can reflect the total number of objects belonging to the class in real time (you can create multiple objects in test and then print human.population).

In addition to the examples of the above constructs, we can also access class data in normal object methods.

Final

The basic meaning of the final keyword is that this data/method/class cannot be changed.

1.final basic type of data: fixed value (constant value), can only be assigned once, can no longer be modified.
2.final method: The method cannot be overwritten. The private method defaults to the final method.
3.final class: This class cannot be inherited.

An object of ordinary type can also have the final keyword, which means that an object reference (reference) cannot be modified again. That is, the reference can only point to one object. However, the contents of an object can be changed (similar to a static pointer in C). We'll introduce the object references later.

If a basic type of data is both final and static, it stores only a single set of values. This is ideal for storing some constants, such as Pi.

Summarize

static field, static method

Class.static_method ()

Final

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.