如何限制子类中无法访问的父类中的方法? [英] how to restrict method in parent class that can't access in the child class?
本文介绍了如何限制子类中无法访问的父类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
plz有人答复我...........
plz anybody reply me...........
推荐答案
如果您的意思是无法访问的类中的方法通过派生类,然后将其声明为private
If you mean a method in a class that can''t be accessed by derived classes, then just declare itprivate
public class myBase
{
...
private void methodA() {}
protected void methodB() {}
}
public class myDerived : myBase
{
...
private void methodC()
{
methodA(); //Illegal - compiler will complain.
methodB(); //Legal
}
}
public class IAmYourDad
{
public void Test()
{
}
}
public class IAmTheChild : IAmYourDad
{
//this is not visible to Parent
private string DoNotTellThisToMyDad()
{
return "Secret";
}
}
这篇关于如何限制子类中无法访问的父类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文