A Java class is a collection of entities with certain common characteristics. It is an abstract data type, which is an abstraction of entities with the same characteristics. In object-oriented programming languages, a class is an abstraction of the properties and behavior of a type of "thing".
Give an example to illustrate the following class. For example, Person (person) is a class , then a specific person "Zhang San" is Objects of the "human" class, and information such as "name, height, weight" are the attributes of the object, and human actions such as "eating, dressing" are the methods of the object. (Recommended learning: java course)
In short, a class is a collection of things with the same characteristics, and an object is a specific instance of a class. Simultaneous classes have polymorphism and inheritance. For example, "human beings" can be divided into "men, women", "elderly people, children", then "men, women" are subclasses of "human beings" and so on.
The definition of class Person in Java language is often as follows:
public class Person { private String name; //属性:姓名 private int height; //属性:身高 private int weight; //属性:体重 public Person() {} public Person(String name, int height, int weight) { this.name = name; this.height = height; this.weight = weight; } //... some methods... public void doSth() { //行为: //... do something }}
A class can be regarded as a template for creating Java objects.
A class can contain the following types of variables:
Local variables: Variables defined in methods, constructors, or statement blocks are called local variables. Variable declaration and initialization are all in methods. After the method ends, the variables will be automatically destroyed.
Member variables: Member variables are variables defined in the class and outside the method body. Such variables are instantiated when the object is created. Member variables can be accessed by methods, constructors, and statement blocks of a specific class.
Class variables: Class variables are also declared in the class, outside the method body, but they must be declared as static type.
A class can have multiple methods. In the above example: barking(), hungry() and sleeping() are all methods of the Dog class.
The above is the detailed content of What does java class mean?. For more information, please follow other related articles on the PHP Chinese website!