java的繼承與上溯
整理了下代碼,這樣看的會更清楚了。以下;
Java代碼
class Parent {
int i = 1;
String j = "Parent J";
StringBuffer k = new StringBuffer("Parent:");
public String getJ() {
return j;
}
public int getI() {
return i;
}
public void printBuffer() {
k.append("Parent Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Parent";
}
}
class Child extends Parent {
int i = 2;
String j = "Child J";
StringBuffer k = new StringBuffer("Child:");
public String getJ() {
return j;
}
public int getI() {
return -i;
}
public void printBuffer() {
k.append("Child Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Child";
}
}
public class ExtendDemo {
public static void main(String[] args) {
Child child = new Child();
System.out.println("Child type test:");
System.out.println("child.i:" + child.i);
System.out.println("child.getI():" + child.getI());
System.out.println("child.j:" + child.j);
System.out.println("child.getJ():" + child.getJ());
child.printBuffer();
System.out.println("child.getClassName():" + child.getClassName());
System.out.println();
Parent parent = child;
System.out.println("Parent type test:");
System.out.println("parent.i:" + parent.i);
System.out.println("parent.getI():" + parent.getI());
System.out.println("parent.j:" + parent.j);
System.out.println("parent.getJ():" + parent.getJ());
parent.printBuffer();
System.out.println("parent.getClassName():" + parent.getClassName());
}
}
class Parent {
int i = 1;
String j = "Parent J";
StringBuffer k = new StringBuffer("Parent:");
public String getJ() {
return j;
}
public int getI() {
return i;
}
public void printBuffer() {
k.append("Parent Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Parent";
}
}
class Child extends Parent {
int i = 2;
String j = "Child J";
StringBuffer k = new StringBuffer("Child:");
public String getJ() {
return j;
}
public int getI() {
return -i;
}
public void printBuffer() {
k.append("Child Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Child";
}
}
public class ExtendDemo {
public static void main(String[] args) {
Child child = new Child();
System.out.println("Child type test:");
System.out.println("child.i:" + child.i);
System.out.println("child.getI():" + child.getI());
System.out.println("child.j:" + child.j);
System.out.println("child.getJ():" + child.getJ());
child.printBuffer();
System.out.println("child.getClassName():" + child.getClassName());
System.out.println();
Parent parent = child;
System.out.println("Parent type test:");
System.out.println("parent.i:" + parent.i);
System.out.println("parent.getI():" + parent.getI());
System.out.println("parent.j:" + parent.j);
System.out.println("parent.getJ():" + parent.getJ());
parent.printBuffer();
System.out.println("parent.getClassName():" + parent.getClassName());
}
}
運行結果以下:
Child type test:
child.i:2
child.getI():-2
child.j:Child J
child.getJ():Child J
Child:Child Buffer;
child.getClassName():Child
Parent type test:
parent.i:1
parent.getI():-2
parent.j:Parent J
parent.getJ():Child J
Child:Child Buffer;Child Buffer;
parent.getClassName():Parent
Child繼承Parent,子類的變量和靜態方法分別隱藏父類的變量和靜態方法,子類的實例方法覆蓋父類的實例方法。隱藏只是把父類的東東藏起來,可是其仍是實質存在的;而覆蓋就是把父類的東東徹底抹掉以替換成子類的,是不可恢復的。在child被強制轉換成Parent類型後,被隱藏的東西又被恢復了,而被覆蓋的方法倒是一去不復返了(子類的方法在這個實例裏面永久替代了原先父類的方法)。這就是區別,也是這個題的考點所在了。