java的继承与上溯

来源:未知 责任编辑:责任编辑 发表时间:2014-01-25 11:39 点击:

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());

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Java编程语言的八大优点
  • JVM对象生命周期详细介绍
  • Java平台上的CRM系统
  • Java 算数测试小程序
  • Java环境 使用Resin在NT环境下配置JSP环境
  • Command(命令模式)
  • Java 一个简单的画图程序
  • Java 日历的小程序
  • Java 统计代码的小工具
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1