如何从单独的 class 访问 getter 和 setter 到主 class(在 C# 中向下转型)

How do I get access to getters and setters from a separate class to the main class(with downcasting in C#)

我知道如何在 Java 中执行此操作,但 C# 无法正常工作。我确定我遗漏了一些明显的东西。如您所见,如果 Pet 不是 Dog,我想将 Pet 转换为 Dog。我认为沮丧是对的,但也许不是。 问题是这一行 Console.WriteLine("{0,12}\t {1}\t {2,10:C}\t {3}\t {4}", Name, Type, Price, Weight, Sound(), AKCRegistration); 它不允许我访问我所指的吸气剂。

这是我的主class

        static void Main(string[] args)
        {
            Pet[] pets = new Pet[5];
            pets[0] = new Dog("King", 55, PET_TYPE.Canine, "akc1000", 1000);
            pets[1] = new Dog("Princess", 25, PET_TYPE.Canine, "akc1000", 2000);
            pets[2] = new Dog("Spike", 25, PET_TYPE.Canine, "akc1000", 25);
            pets[3] = new Cat("Missy", 15, PET_TYPE.Feline, 50);
            pets[4] = new Cat("Mr Boogangle", 5, PET_TYPE.Feline, 30);

            //for (int i = 0; i < pets.Length; i++)
            //{
            //    Console.WriteLine($"{pets[i]}");
            //}

            for each (Pet pet in pets)
            {
                if (pet is Dog)
                {
                    Dog Pet = (Dog) pet;

                    Console.WriteLine("{0,12}\t {1}\t {2,10:C}\t {3}\t {4}", Name, Type, Price, Weight, Sound(), AKCRegistration);
                }
            }

这是我的狗class

class Dog: Pet
    {
        private string aKCregistration;

        public Dog(string name, int weight, PET_TYPE type, string AKC, double price) : base(name, weight, type, price)
        {
            this.aKCRegistration = AKC;
            if (aKCRegistration == "")
            {
                aKCRegistration = "Mutt";
            }
        }

        public int AKCRegistration { get; set; }

        public override string Sound()
        {
            return "Woof, Woof";
        }

        public override string ToString()
        {
            return String.Format("{0,12}\t {1}\t {2,10:C}\t {3}\t {4}", Name, Type, Price, Weight, Sound());
        }
    }

我的宠物Class

public enum PET_TYPE
    {
        Canine, Feline
    }

    public abstract class Pet
    {
        internal string name;
        internal int weight;
        internal PET_TYPE type;
        internal double price;

        public Pet(string name, int weight, PET_TYPE type, double price)
        {
            this.name = name;
            this.weight = weight;
            this.type = type;
            this.price = price;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Weight
        {
            get { return weight; }
            set { weight = value; }
        }

        public double Price
        {
            get { return price; }
            set { price = value; }
        }

        public PET_TYPE Type
        {
            get { return type; }
            set { type = value; }
        }

        public override string ToString()
        {
            return String.Format("{0,12}\t {1}\t {2,10:C}\t {3}\t {4}", Name, Type, Price, Weight, Sound());
        }

        public abstract string Sound();


    } // end class pet  

除了我的猫class,我把所有东西都包括进来了,如果你需要看,请告诉我。

在这种情况下,您没有使用实例变量 Pet 引用它们,因为您是从 class 外部访问它们,您需要在它们上指定实例变量以表明您是访问哪个对象的状态:

Console.WriteLine("{0,12}\t {1}\t {2,10:C}\t {3}\t {4}", 
                   Pet.Name, 
                   Pet.Type, 
                   Pet.Price, 
                   Pet.Weight, 
                   Pet.Sound(), 
                   Pet.AKCRegistration);

并且因为您已经覆盖 Dog 中的 ToString 成员函数,您也可以这样调用它:

string petString = Pet.ToString();

当您访问 class Dog 中的对象时,这意味着您指的是调用 ToString 方法的当前对象,因此它可以在不明确指定任何实例的情况下工作,你可以把它想象成这样:

Console.WriteLine("{0,12}\t {1}\t {2,10:C}\t {3}\t {4}", 
                   this.Name, 
                   this.Type, 
                   this.Price, 
                   this.Weight, 
                   this.Sound(), 
                   this.AKCRegistration);

但我们不需要显式添加 this,因为编译器会处理它。