C#定义一个动物类,要求有品种,毛色,性别,三个属性,在吃饭和在运动两个方法 定义完了 类之后 怼3个类对象 动物的 比如 猫狗猩猩

C#定义一个动物类,要求有品种,毛色,性别,三个属性,在吃饭和在运动两个方法 定义完了 类之后 怼3个类对象 动物的 比如 猫狗猩猩

参考:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
 
namespace ConsoleApplication1
{
    public abstract class Animal
    {
        public abstract void Eat();
        public abstract void Jump();
    }
    public class Sheep : Animal
    {
        public override void Eat()
        {
            Console.WriteLine("羊吃草!");
        }
 
        public override void Jump()
        {
            Console.WriteLine("羊跳栏!~~~");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Animal a = new Sheep();
            a.Eat();
            a.Jump();
        }
    }
}

这样吗?题主需求有些不明确,是实力化3个animal的类呢还是另外生成3个继承自animal的类?

img


有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

using System;
namespace ConsoleApp1
{
    class Animal
    {
        public string Type { get; set; }
        public string Color { get; set; }
        public string Sex { get; set; }
        public void Eat()
        {
            Console.WriteLine(this.Color + "的" + this.Sex + this.Type + "在吃饭");
        }
        public void Motion()
        {
            Console.WriteLine(this.Color + "的" + this.Sex + this.Type + "在运动");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var o = new Animal { Type = "猫", Color = "黑", Sex = "公" };
            o.Eat();
            o.Motion();

            o = new Animal { Type = "狗", Color = "黑", Sex = "木" };
            o.Eat();
            o.Motion();

            o = new Animal { Type = "猩猩", Color = "黑", Sex = "公" };
            o.Eat();
            o.Motion();


            Console.ReadKey();
        }
    }
}



using System;
//动物类
public class Animal{
    //定义属性
    private string varieties;
    //品种
    public string Varieties{
        get{return varieties;}
        set{varieties=value;}
    }
    private string hairColor;
    //毛色
    public string HairColor{
        get{return hairColor;}
        set{hairColor=value;}
    }
    private string sex;
    //性别
    public string Sex{
        get{return sex;}
        set{sex=value;}
    }
    //定义方法

    //吃饭方法
    public string HavingDinner (){

        return "吃饭";
    }
    //运动方法
    public string Motion (){

        return "运动";
    }
}

class Program
{
    static void Main(string[] args)
    {
        //实例化动物:猫
        Animal cat = new Animal();
        //实例化动物:狗
        Animal dog = new Animal();
        //实例化动物:猩猩
        Animal chimpanzee = new Animal();
        chimpanzee.Varieties="苏门达腊猩猩";
        chimpanzee.HairColor="灰色";
        chimpanzee.Sex="公";
        //调用实体方法
        Console.WriteLine(chimpanzee.HavingDinner());
        Console.WriteLine(chimpanzee.Motion());
    }
}