首页 > 代码库 > 父类引用指向子类的对象;
父类引用指向子类的对象;
public class Tuzi { //父类
public String name;
public void show(){
System.out.println(name+"是个笨蛋");
}
public Tuzi(){ } public Tuzi(String name){ this.name=name; }
public void setname(String name){ this.name=name; }
public String getname(){ return name; }
}
public class Tuzi0 extends Tuzi{ //子类
public String weight;
public Tuzi0(){
super();
}
public Tuzi0(String name,String weight){
super(name);
this.weight=weight;
}
public void main(){
System.out.println(name+"说,他承认自己是个笨蛋,并向你发出一拨666");
}
}
public String weight;
public Tuzi0(){
super();
}
public Tuzi0(String name,String weight){
super(name);
this.weight=weight;
}
public void main(){
System.out.println(name+"说,他承认自己是个笨蛋,并向你发出一拨666");
}
}
public class Rabbit {
public void showinfo(Tuzi q){
q.show();
}
public void maininfo(Tuzi0 a){
a.main();
}
}
public void showinfo(Tuzi q){
q.show();
}
public void maininfo(Tuzi0 a){
a.main();
}
}
public class Test2 {
public static void main(String[] args) {
Tuzi q=new Tuzi("xx");
public static void main(String[] args) {
Tuzi q=new Tuzi("xx");
// 父类的引用指向子类的对象
Tuzi p=new Tuzi0();
Tuzi0 a=new Tuzi0("xx","");
Rabbit b=new Rabbit();
b.showinfo(q);
b.maininfo(a);
}
}
Tuzi p=new Tuzi0();
Tuzi0 a=new Tuzi0("xx","");
Rabbit b=new Rabbit();
b.showinfo(q);
b.maininfo(a);
}
}
父类引用指向子类的对象;
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。