簡體   English   中英

我如何訪問從單獨的類到主類的getter和setter(在C#中向下轉換)

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

我知道如何在Java中執行此操作,但是C#無法正常工作。 我確定我缺少明顯的東西。 如您所見,如果寵物不是狗,我想將寵物降為狗。 我認為垂頭喪氣是對的,但也許並非如此。 問題是該行Console.WriteLine("{0,12}\\t {1}\\t {2,10:C}\\t {3}\\t {4}", Name, Type, Price, Weight, Sound(), AKCRegistration); 它不允許我訪問我所指的吸氣劑。

這是我的主班

        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 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());
        }
    }

我的寵物班

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  

除貓類外,我還包括其他所有內容,如果您需要查看,請告訴我。

在這種情況下,您不是使用實例變量Pet來引用它們的,因為您是從類外部訪問它們的,因此需要在它們上指定實例變量以告知您正在訪問哪個對象的狀態,例如:

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();

當您訪問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因為編譯器會解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM