1、定义如下类
Sheel(贝壳)
Prawn(虾)
Fish(鱼)
Seal(海豹)
2、以上类都有name属性,还有如下方法:
(1)eat方法;
(2)构造方法,构造方法的参数是name,构造方法里完成name的初始化;
(3)getName方法。
3、其中Fish的eat方法是重载的:
eat(Sheel s);
eat(Sheel s1, Sheel s2);
eat(Prawn p);
eat(Sheel s, Prawn p);
eat(Prawn p, Sheel s);
eat(Prawn prawn, Sheel sheel)。
其中一个eat方法示范如下,其他的eat方法的方法体请参照完成。
eat(Sheel s, Prawn p){
System.out.println("我是" + this.name + ", 我先吃" + s.getName() + ", 后吃" + p.getName());
}
4、Seal的eat的重载方法类似。
java面向对象编程的基础,建议自己动手写一遍比较好。
class halobios
{
protected string name;
halobios(string myname)
{
name = myname;
}
public string getname()
{
return name;
}
public void eat(halobios s)
{
System.out.println("我是"+this.name+",我吃"+s.getname());
}
public void eat(halobios s, halobios p)
{
System.out.println("我是"+this.name+",我先吃"+s.getname()+"后吃"+p.getname());
}
}
class Sheel:halobios
{}
class Prawn:halobios
{}
class Fish:halobios
{}
class Seal:halobios
{}