标签:
1.什么是super?什么是this?
class Person {
protectedvoid print() {
System.out.println("The print() in class Person.");
}
}
publicclass DemoSuper
extends Person {
publicvoid print() {
System.out.println("The print() in class DemoSuper.");
super.print();//
调用父类的方法
}
publicstaticvoid main(String[] args) {
DemoSuper ds =
new DemoSuper();
ds.print();
}
}
|
publicclass DemoThis {
private String
name;
publicvoid setName(String name) {
this.name = name;//
前一个name是private name;后一个name是setName中的参数。
}
}
|
Button bn;
…
bn.addActionListener(this);
|
class Person {
publicstaticvoid prt(String s) {
System.out.println(s);
}
Person() {
prt("A Person.");
}
Person(String name) {
prt("A person name is:" + name);
}
}
publicclass Chinese
extends Person {
Chinese() {
super();//
调用父类构造函数。
prt("A chinese.");
}
Chinese(String name) {
super(name);//
调用父类具有相同形参的构造函数。
prt("his name is:" + name);
}
publicstaticvoid main(String[] args) {
Chinese cn = new Chinese();
cn = new Chinese("kevin");
}
}
|
Point(int a,int b){
x=a;
y=b;
}
Point(){
this(1,1);
//调用point(1,1),必须是第一条语句。
}
|
标签:
原文地址:http://blog.csdn.net/love_hachi/article/details/42298551