为什么虚拟元素必须是公共的,并且不能在C#中得到保护? [重复]

问题描述 投票:1回答:1

我是C#的新手,目前正在学习OOP。我正在尝试学习所有这些关键字的工作方式。我现在正在学习virtual关键字,并完成了该课程:

    // Animal class
    public class Animal
    {
        // Class propreties
        public string name { get; set; }
        public int age { get; set; }
        public double happiness { get; set; }

        public static int AnimalCounter = 0;

        // Class constructor
        public Animal(string name_ = "Spotty", int age_ = 4, double happiness_ = 3)
        {
            this.name = name_;
            this.age = age_;
            this.happiness = happiness_;
        } 

        // Virtual methods
        public virtual void Print() { }
        public virtual void Woof () { }
        public virtual void Meow () { }
    }

我不理解的是为什么当我使用virtual时不能写protected而不是public。例如在

public virtual void Print() {}

我写时给我一个错误

protected virtual void Print() {}

据我所知,protected意味着只能在基类以及由该基类构成的所有其他类中访问该属性。

所以我有一个新类,叫做:DogbaseAnimal

组成
// Dog class
    public class Dog : Animal
    {
        /* Class Propreties */
        protected double SpotCount_ { get; set; }
        protected double woof_happinessIncrease_ { get; set; }

        // SpotCount = > get and set // GET returns SpotCount // SET 20 if bigger or equal to 20 . SET 0 if less or equal to 0 . otherwise set it to the given value
        public double SpotCount
        {
            get
            {
                return SpotCount_;
            }
            set
            {
                if(value >= 20)
                {
                    SpotCount_ = 20;
                }
                else if(value <= 0)
                {
                    SpotCount_ = 0;
                }
                else
                {
                    SpotCount_ = value;
                }
            }
        }
        // woof_happinessIncrease = > get and set // GET returns woof_happinessIncrease // SET 100 if bigger or equal to 100. SET 0 if less or equal to 0 . otherwise set it to the given value
        public double woof_happinessIncrease
        {
            get
            {
                return woof_happinessIncrease_;
            }
            set
            {
                if(value >= 100)
                {
                    woof_happinessIncrease_ = 100;
                }
                else if(value <= 0)
                {
                    woof_happinessIncrease_ = 0;    
                }
                else
                {
                    woof_happinessIncrease_ = value;
                }
            }
        }

        /* Class Constructor */
        public Dog(string name_ = "Spotty", int age_ = 4, double happiness_ = 3, double SpotCount_=3, double woof_happinessIncrease_=2) : base(name_, age_, happiness_)
        {
            this.SpotCount_ = SpotCount_;
            this.woof_happinessIncrease = woof_happinessIncrease_;
        }

        /* Class Override Methods */
        public override void Print()
        {
            Console.WriteLine(String.Format("Name : {0}", this.name.ToString()));
            Console.WriteLine(String.Format("Age : {0}", this.age.ToString()));
            Console.WriteLine(String.Format("Happiness : {0}", this.happiness.ToString()));
            Console.WriteLine(String.Format("Spot count : {0}", this.SpotCount.ToString()));

            Animal.AnimalCounter++; // Increase the animal counter size by 1 every time we have a new instance of the class Dog.
        }
        public override void Woof()
        {
            // Woof and increase happiness
            this.happiness += woof_happinessIncrease;
            Console.WriteLine(String.Format("{0} woofed. His happiness increased by {1}. His happiness now is : {2}", this.name.ToString(), this.woof_happinessIncrease.ToString(), this.happiness.ToString()));
        }
    }

我用override覆盖了基类中virtual的方法。但是,如果我在基类中对virtual方法使用protected,则会给我一个错误。

有人知道为什么吗?

COMPILE ERROR:CS0507:'function1':在覆盖'access'继承的成员'function2']时无法更改访问修饰符

试图在方法重写中更改访问规范。

我是C#的新手,目前正在学习OOP。我正在尝试学习所有这些关键字的工作方式。我现在正在学习虚拟关键字,并且我上了这个课://动物课公共课...

c# overriding virtual public protected
1个回答
1
投票

公共虚拟方法应该没有问题。

© www.soinside.com 2019 - 2024. All rights reserved.