Home  >  Article  >  Java  >  what is java class

what is java class

藏色散人
藏色散人Original
2019-11-09 11:01:5712066browse

what is java class

#What is a java class?

Java/Classes and Objects

Java is an object-oriented programming language. In addition to basic data types, Java requires that every data type must is a class.

The object-oriented programming idea strives to make the description of things in computer language as consistent as possible with the original appearance of the thing in the real world. Classes and objects are the core of object-oriented methods. concept.

A class is a description of a certain type of thing, which is an abstract and conceptual definition; the object is an individual of the type of thing that actually exists, so it is also called an instance. The relationship between classes and objects is like the relationship between concepts and objects. A class is like a template, and an object is an instance of the template.

The main idea of ​​object-oriented is to regard objective things as objects with states and behaviors, and find out the common states and behaviors of the same type of objects through abstraction to form a class.

Class declarations in Java all begin with class.

class 类名称 {
//声明成员变量
//声明成员方法
}

Construction method

In a Java program, the main job done by the construction method is to help newly created objects assign initial values. The constructor method can be regarded as a special method. Its definition is similar to that of ordinary methods. Its syntax is as follows:

Definition method of constructor

class 类名称 {
访问权限 构造方法名称(){
}
}

When using the constructor Please note the following points:

It has the same name as the class name

It has no return value

The calling timing of the constructor method is also different from the general method. The general method is called when needed, while the constructor method is automatically called when the object is created, and the contents of the constructor method are executed. Therefore, the constructor does not need to be called directly in the program, but is automatically executed when the object is created. Based on the characteristics of the above construction method, it can be used to initialize the data members of the object. The so-called initialization is to assign an initial value to the object.

Creating objects

Generation of objects

Creating an object belonging to a certain class requires two steps:

Declare variables pointing to "objects created by classes"

Use new to create new objects and assign them to previously created variables.

Class name object name = new class name();

For example, if you want to create an object of the Person class, you can use the following statement to achieve it:

Person p ; // 先声明一个 Person 类的对象 p
p = new Person() ; // 用 new 关键字实例化 Person 的对象 p

Access A variable or method in the object

Access attribute: object name.Attribute name

Access method: object name.Method name()

The above is the detailed content of what is java class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java packageNext article:what is java package