标签:简单的 rgs dex public 类构造 调用 nes sys 比较
代码比较简单不多说,直接看:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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); }} |
关键是看(1),(2),(3),(4),this指代当前对象,所以this(name)调用的是当前具有相同形参的构造函数.
而super指代父类对象,所以super(name)调用的是父类具有相同形参的构造函数.
更简单的this.变量,指的是当前对象的变量.super.变量,指的是父类的变量
有个小技巧,父类类名.this.变量名/方法名,此时的this指的是父类对象.
标签:简单的 rgs dex public 类构造 调用 nes sys 比较
原文地址:http://www.cnblogs.com/vincent-air/p/7071389.html