Home >Java >Javagetting Started >What is a class in java
A 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 the object of the "human" class, and information such as "name, height, weight" are the attributes of the object. Person Actions such as "eating, dressing", etc. are methods of objects. In short, a class is a collection of things with the same characteristics, and an object is a specific instance of the class. At the same time, 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 }}
Class in Java
Class can be regarded as a template for creating Java objects .
Let’s understand the definition of a class in Java through the following simple class:
public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }
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 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. php Chinese website, a large number of freeJava introductory tutorials, welcome to learn online!
The above is the detailed content of What is a class in java. For more information, please follow other related articles on the PHP Chinese website!