简体   繁体   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#)

I know how to do this in Java, but C# is not working right. 我知道如何在Java中执行此操作,但是C#无法正常工作。 I'm sure I am missing something obvious. 我确定我缺少明显的东西。 I want to downcast Pet to Dog if the Pet is other than a Dog, as you can see. 如您所见,如果宠物不是狗,我想将宠物降为狗。 I think the downcast is right, but maybe it is not. 我认为垂头丧气是对的,但也许并非如此。 The problem is that this line Console.WriteLine("{0,12}\\t {1}\\t {2,10:C}\\t {3}\\t {4}", Name, Type, Price, Weight, Sound(), AKCRegistration); 问题是该行Console.WriteLine("{0,12}\\t {1}\\t {2,10:C}\\t {3}\\t {4}", Name, Type, Price, Weight, Sound(), AKCRegistration); it does not allow me to access the getters that I am referring to. 它不允许我访问我所指的吸气剂。

This is my Main 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);
                }
            }

This is my Dog 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());
        }
    }

My Pet 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  

I included everything except my cat class, let me know if you need to see it. 除猫类外,我还包括其他所有内容,如果您需要查看,请告诉我。

You are not referencing to them using the instance variable Pet in this case as you are accessing them from outside the class you need to specify the instance variable on them to tell that you are accessing the state of which object like: 在这种情况下,您不是使用实例变量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);

and as you are alreading overriding the ToString member function in Dog you can also call it like: 当您已经在Dog覆盖了ToString成员函数时,您也可以像这样调用它:

string petString = Pet.ToString();

When you are accessing those within the class Dog it means you are referring to the current object whichever is calling the ToString method so it works without specifying any instance explicitly, you can think of it that one like: 当您访问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);

but we don't need to explicitly add this as the compiler takes care of that. 但是我们不需要显式添加this因为编译器会解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM