编写Java程序:(1)定义一个接口Valuable(值钱的东西),该接口中有一个抽象方法getMoney(),该方法表示值多少钱;
(2)定义另一个接口Protectable(受保护的东西),该接口中有一个抽象方法beProtected(),该方法表示怎么被保护。
(3)定义一个抽象类Animal,该类中有成员变量name,tizhong还有一个抽象方法enjoy(),该方法表示动物的爱好。
(4)定义一个GoldenMonkey类,该类继承Animal类,并同时实现了Valuable、Protectable接口。enjoy()方法实现:输出“GoldenMonkey喜欢吃香蕉”;getMoney()方法实现:GoldenMonkey每斤卖10000元,根据体重计算返回金额;beProtected()方法实现:输出保护的方式即:GoldenMonkey被养在动物园里。
(5)定义一个GoldenMonkey对象,初始化name和体重,并实现它的所有方法的输出。
public interface Valuable{
float getMoney();
}
public interface Protectable{
void beProtected();
}
public abstract class Animal{
String name;
float tizhong;
void enjoy();
}
public class GoldenMonkey extends Animal implement Valuable,Protectable{
public GoldenMonkey(String name,float tizhong){
this.name = name;
this.tizhong = tizhong;
}
public float getMoney(){
return 10000*tizhong;
}
public void beProtected(){
System.out.println("GoldenMonkey被养在动物园里");
}
public void enjoy(){
System.out.println("GoldenMonkey喜欢吃香蕉");
}
}
public class Test{
public static void main(String args[]){
GoldenMonkey gm = new GoldenMonkey("GoldenMonkey",20);
gm.beProtected();
gm.enjoy();
System.out.println(gm.getMoney());
}
}