为什么虚拟元素必须是公共的,而不能在C#中得到保护?

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

    // 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 时,我不能写 受保护的 强>。 例如在

public virtual void Print() {}

写时给我一个错误

protected virtual void Print() {}

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

所以我有一个名为 Dog 的新类,它是由 base 制成的动物

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

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

有人知道为什么吗?

编译错误 :CS0507: 'function1':在覆盖'access'继承的成员'function2'时无法更改访问修饰符

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

mhc994 回答:为什么虚拟元素必须是公共的,而不能在C#中得到保护?

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

确保基本方法为public virtual,并确保扩展类(覆盖该方法)将其标记为public virtual

public class Animal
{
    public virtual void Print() { }
}

public class Cat : Animal
{
    public override void Print() { Console.WriteLine("Meow"); }
}

不支持此操作:

public class Animal
{
    protected virtual void Print() { }
}

public class Cat : Animal
{
    public override void Print() { Console.WriteLine("Meow"); } // Base is protected,so this doesn't make sense.
}
本文链接:https://www.f2er.com/2924962.html

大家都在问