C# “受保护”和“虚拟/覆盖”之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13145615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "protected" and "virtual/override"
提问by Orel Eraki
I couldn't understand the need or purpose of "protected" when i have "virtual/override" could someone explain me what do i need those 2 things if they are almost the same.
当我有“虚拟/覆盖”时,我无法理解“受保护”的需要或目的,有人可以解释一下,如果这两件事几乎相同,我需要什么。
Edit:
编辑:
Thanks for all the helpers, i now understand that "protected" is only for visibility purposes, while virtual/override is for class behavior.
感谢所有帮助者,我现在明白“受保护”仅用于可见性目的,而虚拟/覆盖用于类行为。
采纳答案by Adam
They are certainly notalmost the same.
它们当然不完全相同。
The protectedmodifier sets the visibilityof a field or method: such a member can only be accessed from the class it is defined in or from a derived class.
所述protected改性剂套能见度的字段或方法的:这样的构件只能从它在或从派生类中定义的类访问。
The virtualmodifier specifies that the method it is applied to can be overriddenin a derived class.
所述virtual修饰符指定,它被施加到该方法可以是overridden在派生类。
These modifiers can be combined: a method can be protected and virtual.
这些修饰符可以组合:一个方法可以是protected 和virtual 的。
回答by Johan Larsson
- protectedmeans private for current class and derived classes
- virtualmeans it can be used as-is but also be overridden in derived classes
Maybe it is better with some code instead of things you have probably already read, here is a little sample you can play with. Try removing the comments (//) and you can see that the compiler tells you that the properties cannot be accessed
也许用一些代码代替你可能已经读过的东西会更好,这里有一个你可以玩的小例子。尝试去掉注释(//),你可以看到编译器告诉你无法访问这些属性
[TestFixture]
public class NewTest
{
[Test]
public void WhatGetsPrinted()
{
A a= new B();
a.Print(); //This uses B's Print method since it overrides A's
// a.ProtectedProperty is not accesible here
}
}
public class A
{
protected string ProtectedProperty { get; set; }
private string PrivateProperty { get; set; }
public virtual void Print()
{
Console.WriteLine("A");
}
}
public class B : A
{
public override void Print() // Since Print is marked virtual in the base class we can override it here
{
//base.PrivateProperty can not be accessed hhere since it is private
base.ProtectedProperty = "ProtectedProperty can be accessed here since it is protected and B:A";
Console.WriteLine("B");
}
}
回答by bijayk
I think you need to understand above two things properly, since both has different purpose.
我认为您需要正确理解以上两件事,因为两者都有不同的目的。
protectedis the type or member can only be accessed by code in the same class or struct, or in a derived class.
protected是类型或成员只能由同一类或结构中的代码或派生类中的代码访问。
The virtualkeyword is for modify a method, property and allow it to be overridden in a derived class.
所述虚拟关键字是修改方法,属性,并允许它被覆盖在派生类。
回答by Charles Bretana
I can be argued that the Most importantdistinction about virtual, is that this causes the compiler, when calling a method member polymorphically, whichimplementation to bind the compiled code to. When you call a member of a class from client code where the actual type of the object is, say derived class foo, but the variable it is being called on is actually typed (declared) as some base class, say bar, Members declared as virtualwill bind to the implementation in the actual object type, (or to the most derived base class of the objects type that has an implementation). Members notdeclared as virtual will bind to the implementation in the type that the variableis declared to be.
我可以说最重要的区别virtual在于,这会导致编译器在以多态方式调用方法成员时,将编译后的代码绑定到哪个实现。当您从客户端代码调用类的成员时,对象的实际类型是派生类,例如派生类foo,但它被调用的变量实际上被类型化(声明)为某个基类,例如bar,声明为的成员virtual将绑定到实际对象类型中的实现,(或到具有实现的对象类型的最派生基类)。未声明为 virtual 的成员将绑定到声明变量的类型中的实现。
A. Virtual. then, if the member is declared as virtual, the implementation in the derived class will be executed even if the variable is declared as a base type.
A. 虚拟的。然后,如果成员被声明为虚拟的,即使变量被声明为基类型,派生类中的实现也会被执行。
public class Animal
{ public virtual Move() { debug.Print("Animal.Move()"); }
public class Bird: Animal
{ public virtual override Move() { debug.Print("Bird.Move()"); }
Animal x = new Bird();
x.Move(); // Will print "Bird.Move"
B. Not Virtual. When a member which is notdeclared as virtual, then the implementation will be chosen based on the declared type of the variable the method is executed on. So if you have a Bird Object, in variable x declared as `Animal', and you call a method that is implemented in both classes, the compiler will bind to the implementation in the Animal class, not in Bird, even though the object is really a Bird.
B.不是虚拟的。如果成员未声明为virtual,则将根据执行方法的变量的声明类型选择实现。因此,如果您有一个 Bird 对象,在变量 x 中声明为“Animal”,并且您调用在两个类中都实现的方法,编译器将绑定到 Animal 类中的实现,而不是 Bird 中的实现,即使该对象是真的是一只鸟。
public class Animal
{ public Move() { debug.Print("Animal.Move()"); }
public class Bird: Animal
{ public Move() { debug.Print("Bird.Move()"); }
Animal x = new Bird();
x.Move(); // Will print "Animal.Move"

