如何在 C# 中创建公共但不可继承的方法或属性?
这是一个例子。我有两个类,一个是继承的,并且都有一个具有相同名称但参数不同的函数:
public class MyClass
{
//public class members
public MyClass()
{
//constructor code
}
public void Copy(MyClass classToCopy)
{
//copy code
}
}
public class InheritedClass : MyClass
{
//public class members
public InheritedClass():base()
{
//constructor code
}
public void Copy(InheritedClass inheritedClassToCopy)
{
//copy code
}
}
我的问题是如何使基类的复制方法 (MyClass.Copy) 在 InheritedClass 中不可继承或不可见?我不想能够做到这一点:
MyClass a;
InheritedClass b;
b.Copy(a);
这有意义吗,或者我应该保留这个功能吗?我所要求的事情可以完成吗?
Here is an example. I have two classes, one inherited, and both have a function with the same name, but different arguments:
public class MyClass
{
//public class members
public MyClass()
{
//constructor code
}
public void Copy(MyClass classToCopy)
{
//copy code
}
}
public class InheritedClass : MyClass
{
//public class members
public InheritedClass():base()
{
//constructor code
}
public void Copy(InheritedClass inheritedClassToCopy)
{
//copy code
}
}
My question is how do I make the base class' copy method (MyClass.Copy) non-inheritable or non-visible in InheritedClass? I don't want to be able to do this:
MyClass a;
InheritedClass b;
b.Copy(a);
Does this make sense, or should I keep this functionality in there? Can what I'm asking even be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当基类使用时尝试隐藏这样的公共方法是有问题的。您故意试图违反里氏替换原则。
Trying to hide a public method like this when used by a base class is problematic. You're purposely trying to violate the Liskov substitution principle.
你不能在这里做你想做的事; C# 不允许继承成员出现负方差。 (实际上,几乎没有语言真正这样做)
不过,您可能根本不想要这里的继承类;您可能真正想要的是一个界面。或者...你这里的两个类可能没有正确的关系;也许他们都应该是第三类的共同兄弟姐妹,即他们的父母。
You can't do what you are wanting to do here; C# does not allow negative variance in inherited members. (almost no languages truly do, actually)
It may be that you don't want an inherited class here at all, though; what you may really want is an interface. Or... your two classes here may not have the correct relationship; perhaps they should both instead be common siblings of a third class, which is their parent.
您可以使用显式接口实现来向继承者隐藏此方法。但是您当然需要添加一个接口,并且需要将类型转换为接口来调用您的方法:
You can use explicit interface implementation to hide this method from the inheritor. But you will need to add an interface of course and you will need to cast your type to the interface to call your method:
这是不可能的。继承类继承所有公共和受保护的成员、方法和属性。使用
sealed
修饰符使其不可重写,但仍可供继承的类访问。This is not possible. An inherited class inherits all public and protected members, methods and properties. Using the
sealed
modifier with make it non-overridable, but still accessible to your inherited class.其他人都这么说,但如果我正确推断你的目标,那就是确保 InheritedClass 用户永远不会使用 MyClass 方法。在这种情况下,请将其从 MyClass 中排除并创建两个继承它的类。
如果 MyBaseClass 不应该被实例化(最有可能),则将其设为抽象。
(已编辑 - 您可能希望在基类中包含属于基类一部分的任何内容的复制代码)
What everyone else said, but if I am inferring your goal correctly, it is to make sure that InheritedClass users never use the MyClass method. In that case, exclude it from MyClass and make two classes that inherit it.
Make MyBaseClass abstract if it should not be instantiated (most likely).
(Edited -- you probably would want to include copy code for anything that's part of the base class in the base class)