首页 > 代码库 > JAVA学习--子类对象实例化的全过程
JAVA学习--子类对象实例化的全过程
public class TestDog {
publicstatic void main(String[] args) {
Dog d = newDog();
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 intage;
public intgetAge() {
returnage;
}
public voidsetAge(int age) {
this.age =age;
}
publicCreator() {
super();
System.out.println("this is Creator‘s constructor");
}
}
// 动物类
class Animal extends Creator {
privateString name;
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name =name;
}
publicAnimal() {
super();
System.out.println("this is Animal‘s constructor");
}
}
// 狗
class Dog extends Animal {
privateString hostName;
publicString getHostName() {
returnhostName;
}
public voidsetHostName(String hostName) {
this.hostName = hostName;
}
public Dog(){
super();
System.out.println("this is Dog‘s constructor");
}
}
Dog d = newDog();
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 intage;
public intgetAge() {
returnage;
}
public voidsetAge(int age) {
this.age =age;
}
publicCreator() {
super();
System.out.println("this is Creator‘s constructor");
}
}
// 动物类
class Animal extends Creator {
privateString name;
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name =name;
}
publicAnimal() {
super();
System.out.println("this is Animal‘s constructor");
}
}
// 狗
class Dog extends Animal {
privateString hostName;
publicString getHostName() {
returnhostName;
}
public voidsetHostName(String hostName) {
this.hostName = hostName;
}
public Dog(){
super();
System.out.println("this is Dog‘s constructor");
}
}
JAVA学习--子类对象实例化的全过程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。