This chapter brings you what is a class in Java? The relevant introduction of class lets everyone know some knowledge about classes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
class Point{ constructor(){ } toString(){ } } console.log(Object.keys(Point.prototype)) console.log(Object.getOwnPropertyNames(Point.prototype))
The above is a class
1. The data type of the class is a function, and the class itself points to the constructor
console.log(typeof Point) // "function" console.log(Point ===Point.prototype.constructor) // true
2. The prototype attribute of the constructor is in the "ES6" class" continues to exist. In fact, all methods of a class are defined on the prototype property of the class. The code starting with
is equivalent to
class Point{} Point.prototype = { constructor() {}, toString() {}, }
Since the methods of the class (except constructor) are all defined on the prototype object, new methods of the class can be added on the prototype object. The Object.assign method can add multiple methods to the class at one time.
It is very important that all methods defined internally in the class are not enumerable.
console.log(Object.keys(Point.prototype)) // [] console.log(Object.getOwnPropertyNames(Point.prototype)) // ["constructor", "toString"]
Among them, Object.keys() returns an array containing all the enumerable properties of the object itself, excluding Symbol, and Object.getOwnPropertyNames() returns an array containing all its own properties, excluding Symbol
3. Constructor method
The constructor method is the default method of the class. This method is automatically called when generating an object instance through the new command. A class must have a constructor method. If it is not defined, an empty constructor method will be added by default.
The constructor method returns the instance object by default, which is the point of this. However, you can specify to return another object
4. Inheritance
class ColorPoint extends Point { constructor(x, y, color) { super(x, y) // 调用父类的 constructor(x, y) this.color = color } toString() { return this.color + '' + super.toString() // 调用父类的 toString() } }
Do you feel familiar when you see extends? People who have used React must know that in React’s ES6 writing method, we often write like this
class XXXXX extends Component{}
ColorPoint can inherit all properties and methods of the Point class through extends
Have you noticed that the super keyword appears in both the constructor and toString methods, which refers to the instance of the parent class.
Subclasses must call the super method in the constructor method, otherwise an error will be reported when creating a new instance. Because the subclass does not have its own this object, but inherits the this object of the parent class, if super is not called, the subclass will not get this.
In fact, class is a more concise way of writing the object prototype
The above is the detailed content of What is a class in Java? Related introduction to class. For more information, please follow other related articles on the PHP Chinese website!