C# 中的“受保护”方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/930190/
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
"protected" methods in C#?
提问by Sherif
What are the benefits to defining methods as protected
in C#?
protected
在 C# 中定义方法有什么好处?
like :
喜欢 :
protected void KeyDemo_KeyPress( object sender, KeyPressEventArgs e )
{
// some code
}
As compared to something like this:
与这样的事情相比:
private void FormName_Click( object sender, EventArgs e )
{
//some code
}
I've seen such examples in many books and I don't understand why and when do they use private
vs protected
?
我在很多书中看到过这样的例子,我不明白他们为什么以及什么时候使用private
vs protected
?
采纳答案by Philippe Leybaert
Protected methods can be called from derived classes. Private methods can't.
可以从派生类调用受保护的方法。私有方法不能。
That's the one and only difference between private and protected methods.
这是私有方法和受保护方法之间唯一的区别。
回答by Josh
Some aspects of .NET such as ASP.NET create subclasses of your code-behind class at runtime. So an ASP.NET Page class for example inherits from its codebehind class. By making the method protected, the dynamically generated page class can easily hook up a button click event to a protected method in the base class that handles it.
.NET 的某些方面(例如 ASP.NET)在运行时创建代码隐藏类的子类。例如,一个 ASP.NET Page 类继承自它的代码隐藏类。通过使方法受保护,动态生成的页面类可以轻松地将按钮单击事件连接到处理它的基类中的受保护方法。
回答by Game_Overture
If you have an inherited form (or any class for that matter), you would be able to invoke this function from within the sub-class.
如果您有一个继承的形式(或任何与此相关的类),您将能够从子类中调用此函数。
回答by Bruce
Often 'protected' is used when you want to have a child class override an otherwise 'private' method.
当您希望子类覆盖其他“私有”方法时,通常使用“受保护”。
public class Base {
public void Api() {
InternalUtilityMethod();
}
protected virtual void InternalUtilityMethod() {
Console.WriteLine("do Base work");
}
}
public class Derived : Base {
protected override void InternalUtilityMethod() {
Console.WriteLine("do Derived work");
}
}
So we have the override behavior we know and love from inheritance, without unnecessarily exposing the InternalUtilityMethod to anyone outside our classes.
因此,我们从继承中获得了我们熟悉和喜爱的覆盖行为,而不必将 InternalUtilityMethod 暴露给我们类之外的任何人。
var b = new Base();
b.Api(); // returns "do Base work"
var d = new Derived();
d.Api(); // returns "do Derived work"
回答by Babak Naffas
- Protected methods can be accessed by inheriting classes where as private methods cannot.
- Keeping in mind that .aspx and .ascx file inherit from their code behind classes (default.aspx.cs), the protected methods can be accessed from within the .aspx/.ascx
- 可以通过继承类访问受保护的方法,而私有方法则不能。
- 请记住,.aspx 和 .ascx 文件继承自它们的类背后的代码(default.aspx.cs),可以从 .aspx/.ascx 中访问受保护的方法
Keep this in mind too: If you have a button and that button's OnClick is set to Button_Click
也请记住这一点:如果您有一个按钮并且该按钮的 OnClick 设置为 Button_Click
<asp:Button id="btn" runat="server" OnClick="Button_Click" />
then the Button_Click method needs to have at least protected visibility to be accessible by the button.
那么 Button_Click 方法至少需要具有受保护的可见性才能被按钮访问。
You could get around this by added the following to you Page_Load method:
您可以通过向 Page_Load 方法添加以下内容来解决此问题:
btn.Click += new EventHandler(Button_Click);
回答by Vinay Chanumolu
Protected Methods are just like private methods. They could be accessed only by the members of the class. Only difference is unlike private members, protected members could be accessed by the derived classes as well.
受保护的方法就像私有方法一样。它们只能由类的成员访问。唯一的区别是与私有成员不同,受保护的成员也可以被派生类访问。