您的位置:首页 > 编程语言 > Java开发

JAVA学习--子类对象实例化的全过程

2015-01-30 22:00 489 查看
public class TestDog {

public
static void main(String[] args) {

Dog d = new
Dog();

d.setAge(10);

d.setName("花花");

d.setHostName("小明");

System.out.println("name:" + d.getName() + " age:" +
d.getAge()

+
"hostName:" + d.getHostName());

System.out.println(d.toString());

}

}

// 生物

class Creator {

private int
age;

public int
getAge() {

return
age;

}

public void
setAge(int age) {

this.age =
age;

}

public
Creator() {

super();

System.out.println("this is Creator's constructor");

}

}

// 动物类

class Animal extends Creator {

private
String name;

public
String getName() {

return
name;

}

public void
setName(String name) {

this.name =
name;

}

public
Animal() {

super();

System.out.println("this is Animal's constructor");

}

}

// 狗

class Dog extends Animal {

private
String hostName;

public
String getHostName() {

return
hostName;

}

public void
setHostName(String hostName) {

this.hostName = hostName;

}

public Dog()
{

super();

System.out.println("this is Dog's constructor");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: