父类引用指向子类的对象;
Posted 如果能够
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了父类引用指向子类的对象;相关的知识,希望对你有一定的参考价值。
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);
}
}
以上是关于父类引用指向子类的对象;的主要内容,如果未能解决你的问题,请参考以下文章