永发信息网

用C#编写程序,定义一个animal类,具有color,weight属性和sound,eat等方

答案:2  悬赏:0  手机版
解决时间 2021-12-28 18:25
  • 提问者网友:姑娘长的好罪过
  • 2021-12-28 00:39
,然后创建animal类的dog,cat,cow子类,并根据各子类的特征设置相关的方法和属性(如不同的叫声汪汪,喵呜,哞哞,食用不同的食欲骨头,鱼,草)
最佳答案
  • 五星知识达人网友:一秋
  • 2021-12-28 01:29

class Vehicle {
private int wheels;
private float weight;
protected Vehicle(int wheels, float weight){
this.wheels = wheels;
this.weight = weight;
}
public int getWheels() {
return wheels;
}
public float getWeight() {
return weight;
}
public void print(){
System.out.println("汽车:");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
}
}
class Car extends Vehicle{
private int passenger_load;
public Car(int wheels, float weight, int passenger_load) {
super(wheels, weight);
this.passenger_load = passenger_load;
}
public int getPassenger_load() {
return passenger_load;
}
public void print(){
System.out.println("小车:");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
System.out.println("载人数为"+this.getPassenger_load()+"人");
}
}

class Truck extends Vehicle{
private int passenger_load;
private float payload;
public Truck(int wheels, float weight, int passenger_load, float payload) {
super(wheels, weight);
this.passenger_load = passenger_load;
this.payload = payload;
}
public int getPassenger_load() {
return passenger_load;
}
public float getPayload() {
return payload;
}
public void print(){
System.out.println("卡车:");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
System.out.println("载人数为"+this.getPassenger_load()+"人");
System.out.println("载重量为"+this.getPayload()+"吨");
}
}

public class Test{
public static void main(String args[]){
Vehicle car = new Car(4, 3, 4);
Vehicle truck = new Truck(6, 6, 2, 10);
System.out.println("*****************************");
car.print();
System.out.println("*****************************");
truck.print();
}

}

全部回答
  • 1楼网友:玩家
  • 2021-12-28 01:51
定义一个基类,sound和eat抽象,然后定义子类写具体实现 /// /// 父抽象类 /// public abstract class animal { public string color { get; set; } public decimal weight { get; set; } public abstract string sound(); public abstract string eat(); } /// /// 狗狗 /// public class dog : animal { public override string eat() { return "骨头"; } public override string sound() { return "汪汪"; } } /// /// 猫猫 /// public class cat : animal { public override string eat() { return "鱼"; } public override string sound() { return "喵呜"; } } /// /// 牛牛 /// public class dog : animal { public override string eat() { return "草"; } public override string sound() { return "哞哞"; } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯