java程序中super()和this()之间的区别
Java还与其他各种关键字一起提供了this和super作为特殊关键字,它们分别用于分别表示一个类的当前实例和它的超类。由于具有相似性,这两个关键字之间的区别很大,如下所示-
这个与超级的例子
Equals.jsp
class A {
public int x, y;
public A(int x, int y) {
this.x = x;
this.y = y;
}
}
class B extends A {
public int x, y;
public B() {
this(0, 0);
}
public B(int x, int y) {
super(x + 1, y + 1);// calls parent class constructor
this.x = x;
this.y = y;
}
public void print() {
System.out.println("Base class : {" + x + ", " + y + "}");
System.out.println("Super class : {" + super.x + ", " + super.y + "}");
}
}
class Point {
public static void main(String[] args) {
B obj = new B();
obj.print();
obj = new B(1, 2);
obj.print();
}
}输出结果
Base class : {0, 0}
Super class : {1, 1}
Base class : {1, 2}
Super class : {2, 3}热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短