写一个多态的程序,并进行测试
答案:2 悬赏:0 手机版
解决时间 2021-02-06 10:41
- 提问者网友:骑士
- 2021-02-05 23:17
写一个多态的程序,并进行测试
最佳答案
- 五星知识达人网友:第幾種人
- 2021-02-06 00:11
public class A{
String name;
public A(){}
public A(String name){
this.name=name;
}
public void showName(){
System.out.println("A.........."+name);
}
}
public class B{
public B(){}
public B(String name){
super(name);
}
public void showName(){
System.out.println("B.........."+name);
}
}
public class Test{
public static void main(String[] args){
A a=new B("张三");
a.showName();
}
}
String name;
public A(){}
public A(String name){
this.name=name;
}
public void showName(){
System.out.println("A.........."+name);
}
}
public class B{
public B(){}
public B(String name){
super(name);
}
public void showName(){
System.out.println("B.........."+name);
}
}
public class Test{
public static void main(String[] args){
A a=new B("张三");
a.showName();
}
}
全部回答
- 1楼网友:千夜
- 2021-02-06 01:35
package two;
public class animal {
public void food(){}
public void sound(){}
public static void main(string[] args) {
animal c = new cat();
c.food();
c.sound();
animal d = new dog();
d.food();
d.sound();
}
}
class dog extends animal{
public void food(){
system.out.println("骨头");
}
public void sound(){
system.out.println("汪汪");
}
}
class cat extends animal{
public void food(){
system.out.println("鱼");
}
public void sound(){
system.out.println("喵喵");
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯