本次项目是:以面向对象的思想设计动物乐园系统。 动物乐园中有猫,鸭子等成员,还可以增加新成员。 猫和鸭子都有自己的名字,都有自己的腿,但腿的条数不同,猫和鸭子会发出叫声,猫的叫声是:喵喵喵……,鸭子的...

public abstract class animal {
protected String name;
 
public animal(String name) {
super();
this.name = name;
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
public abstract void show();
 
}
---------------------------------2
public interface lu {
public int leg();
 
}
-------------------------3
 
public class cat extends animal implements lu{
private int leg;
 
public cat(String name, int leg) {
super(name);
this.leg = leg;
}
 
 
public int getLeg() {
return leg;
}
 
 
public void setLeg(int leg) {
this.leg = leg;
}
---------------------------------------------4
 
public class duck extends animal implements lu {
private int leg;
 
public duck(String name, int leg) {
super(name);
this.leg = leg;
}
 
public int getLeg() {
return leg;
}
 
public void setLeg(int leg) {
this.leg = leg;
}
 
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("嘎");
}
 
@Override
public int leg() {
 
return this.leg;
}
 
}
 
------------------5
 
public class hai extends animal {
 
public hai(String name) {
super(name);
}
 
@Override
public void show() {
System.out.println("海豚");
}
 
}
 
 
---------------
 
 
 
 
 
public class test {
 
private static Object lu;
 
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
animal[] a=new animal[3];
a[0]=new cat("小猫",4);
a[1]=new duck("鸭子",2);
a[2]=new hai("海豚");
System.out.println("动物的名字"+"\t"+"腿数"+"\t\t"+"动物叫");
for(int i=0;i<a.length;i++)
{
if(a[i] instanceof lu)
{
String name=a[i].getName();
int legnum=((lu)a[i]).leg();
//System.out.println(name+ "\t\t" +legnum+ "\t\t");
 
System.out.print(name + "\t\t" + legnum + "\t\t");
a[i].show();
 
}
else
{
String name=a[i].getName();
System.out.print(name+ "\t\t" +0+ "\t\t");
 
a[i].show();
 
 
}
 
 
 
 
 
 
}
 
 
}
 
}

转载于:https://www.cnblogs.com/liuruiguang/p/5951433.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值