The code is relatively simple to say, look directly:
1234567891011121314151617181920212223242526272829303132333435363738 |
class
Person {
public
static
void
prt(String s) {
System.out.println(s);
}
Person() {
prt(
"A Person."
);
}
Person(String name) {
prt(
"A person name is:"
+ name);
}
}
public
class
Chinese
extends
Person {
Chinese() {
super
();
// 调用父类构造函数(1)
prt(
"A chinese."
);
// (4)
}
Chinese(String name) {
super
(name);
// 调用父类具有相同形参的构造函数(2)
prt(
"his name is:"
+ name);
}
Chinese(String name,
int
age) {
this
(name);
// 调用当前具有相同形参的构造函数(3)
prt(
"his age is:"
+ age);
}
public
static
void
main(String[] args) {
Chinese cn =
new
Chinese();
cn =
new
Chinese(
"kevin"
);
cn =
new
Chinese(
"kevin"
,
22
);
}
}
|
The key is to look at (1), (2), (3), (4), this refers to the current object, so this (name) invokes the constructor that currently has the same formal parameter.
Super refers to the parent class object, so super (name) calls the constructor of the parent class with the same parameters.
The simpler this. Variable, refers to the variable of the current object. Super. Variable, which is the variable of the parent class
There's a little trick, the parent class name. This is the variable name/method name, at which point this refers to the parent class object.
Usage of super and this in Java