We have always defined classes to create objects. Objects are entities with functionality, while classes are type classifications of objects. This is a basic concept of object-oriented.
In inheritance, we treat classes as expandable subjects, which improves our understanding of "classes".
There is much more to discuss about the class itself. We will continue to deepen
static data members
There is some data used to express the state of the class. For example, in the 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 Human class has a population of 8
All objects of the class share the "population" data. Such data is called a class data member (class field).
In the class definition, we use the static keyword to declare class data members, such as:
Copy the code code as follows:
classHuman
{
/**
* 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 define two class data members: population and is_mammal. All Human objects share a population data; the is_mammal (is a mammal) attribute of any Human object is true.
Class data members also need to set access permissions. For class data members declared as public, they can be directly accessed from the outside using class.field or object.field (if an object of this class exists). Both access methods are reasonable because class data members can be thought of as attributes of the class and can be thought of as attributes shared by all members. If a class data member is defined as private, then the class data member can only be accessed from within the class.
(Is_mammal is set to public above just for demonstration. This is quite dangerous. If someone uses Human.is_mammal=false;, all humans will suffer. Again, the basic principle is to set the data to private as much as possible.)
static method
We can also have class methods, which are methods declared as static. Class methods represent actions that a class can implement, and the operations do not involve a specific object. If a method is declared static, it can only call static data and methods, but not non-static data and methods.
In fact, in a static method, there will be no implicitly passed this and super parameters. We have no way of referencing the data and methods belonging to the object (which is exactly what we want).
Based on what has been said above, we have the following relationship:
A red dotted line indicates inaccessibility. In other words, the object's data cannot be accessed in class methods.
Next we add a static method getPopulation(), which returns static data population:
Copy the code code as follows:
classHuman
{
/**
* 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 through class.method() or object.method(). For example, use the following Test class test:
Copy the 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 called 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 objects. For example, "population" should increase by 1 as an object is created. We can modify the "population" data of the class in the object's methods. We access the class data members in the constructor below. The construction method here is a non-static method, that is, an object method:
Copy the code code as follows:
classHuman
{
/**
* constructor
*/
public Human(int h)
{
this.height = h;
Human.population = 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;
}
Every time we create an object, we modify the class data through the object's construction method and increase the population class data by 1. 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 then print Human.population).
In addition to the constructor example given above, we can also access class data in ordinary object methods.
final
The basic meaning of the final keyword is: This data/method/class cannot be changed.
1. Final basic type data: constant value, which can only be assigned once and cannot be modified again.
2.Final method: This method cannot be overridden. Private methods default to final methods.
3.Final class: This class cannot be inherited.
Objects of ordinary types can also have the final keyword, which means that the object reference (reference) can no longer be modified. That is, the reference can only point to one object. However, the object's contents can change (similar to static pointers in C). We'll cover object references later.
If a basic type of data is both final and static, then it stores only one copy of the fixed value. This is great for storing constants such as pi.
Summarize
static field, static method
class.static_method()
final