java的继承与上溯
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 22:18 点击:次
java的继承与上溯
整理了下代码,这样看的会更清楚了。如下;
Java代码
class Parent {
int i = 1;
String j = "Parent J";
StringBuffer k = new StringBuffer("Parent:");
String j = "Parent J";
StringBuffer k = new StringBuffer("Parent:");
public String getJ() {
return j;
}
return j;
}
public int getI() {
return i;
}
return i;
}
public void printBuffer() {
k.append("Parent Buffer;");
System.out.println(k);
}
k.append("Parent Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Parent";
}
return "Parent";
}
}
class Child extends Parent {
int i = 2;
String j = "Child J";
StringBuffer k = new StringBuffer("Child:");
String j = "Child J";
StringBuffer k = new StringBuffer("Child:");
public String getJ() {
return j;
}
return j;
}
public int getI() {
return -i;
}
return -i;
}
public void printBuffer() {
k.append("Child Buffer;");
System.out.println(k);
}
k.append("Child Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Child";
}
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();
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());
}
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:");
String j = "Parent J";
StringBuffer k = new StringBuffer("Parent:");
public String getJ() {
return j;
}
return j;
}
public int getI() {
return i;
}
return i;
}
public void printBuffer() {
k.append("Parent Buffer;");
System.out.println(k);
}
k.append("Parent Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Parent";
}
return "Parent";
}
}
class Child extends Parent {
int i = 2;
String j = "Child J";
StringBuffer k = new StringBuffer("Child:");
String j = "Child J";
StringBuffer k = new StringBuffer("Child:");
public String getJ() {
return j;
}
return j;
}
public int getI() {
return -i;
}
return -i;
}
public void printBuffer() {
k.append("Child Buffer;");
System.out.println(k);
}
k.append("Child Buffer;");
System.out.println(k);
}
static String getClassName() {
return "Child";
}
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();
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());
}
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
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.i:1
parent.getI():-2 <
相关新闻>>
- 发表评论
-
- 最新评论 更多>>