为什么虚拟元素必须是 public 并且在 C# 中不能被保护?
Why must virtual elements be public and can't be protected in C#?
我是 C# 新手,目前正在学习 OOP。我正在尝试了解所有这些关键字的工作原理。我正在学习 virtual 关键字,我做了这个 class :
// 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 () { }
}
我不明白的是为什么当我使用虚拟。
例如在
public virtual void Print() {}
写的时候报错
protected virtual void Print() {}
据我所知,protected 意味着 属性 只能在基 class 和所有其他由基 class 构成的 class 中访问=80=].
所以我有一个新的 class 叫做:Dog 它是由 基地 class 动物
// 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 的方法 在基地 class。但是,如果我在基础 class 中使用 protected for virtual 方法它给我一个错误。
有谁知道为什么?
编译错误:CS0507:
'function1' : 覆盖 'access' 继承成员时无法更改访问修饰符 'function2'
试图在方法覆盖中更改访问规范。
public 虚拟方法应该没有问题。
确保基本方法是 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.
}
我是 C# 新手,目前正在学习 OOP。我正在尝试了解所有这些关键字的工作原理。我正在学习 virtual 关键字,我做了这个 class :
// 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 () { }
}
我不明白的是为什么当我使用虚拟。 例如在
public virtual void Print() {}
写的时候报错
protected virtual void Print() {}
据我所知,protected 意味着 属性 只能在基 class 和所有其他由基 class 构成的 class 中访问=80=].
所以我有一个新的 class 叫做:Dog 它是由 基地 class 动物
// 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 的方法 在基地 class。但是,如果我在基础 class 中使用 protected for virtual 方法它给我一个错误。
有谁知道为什么?
编译错误:CS0507: 'function1' : 覆盖 'access' 继承成员时无法更改访问修饰符 'function2'
试图在方法覆盖中更改访问规范。
public 虚拟方法应该没有问题。
确保基本方法是 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.
}