java的继承与上溯
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());
相关新闻>>
- 发表评论
-
- 最新评论 更多>>