如何在 C# 中创建公共但不可继承的方法或属性?

发布于 2024-09-29 04:19:37 字数 685 浏览 8 评论 0原文

这是一个例子。我有两个类,一个是继承的,并且都有一个具有相同名称但参数不同的函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

や莫失莫忘 2024-10-06 04:19:37

这有意义吗,或者我应该保留这个功能吗?我所要求的事情可以完成吗?

当基类使用时尝试隐藏这样的公共方法是有问题的。您故意试图违反里氏替换原则

替代文字

Does this make sense, or should I keep this functionality in there? Can what I'm asking even be done?

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.

alt text

临风闻羌笛 2024-10-06 04:19:37

你不能在这里做你想做的事; 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.

悟红尘 2024-10-06 04:19:37

您可以使用显式接口实现来向继承者隐藏此方法。但是您当然需要添加一个接口,并且需要将类型转换为接口来调用您的方法:

public interface MyInterface
{
    void Copy(MyClass classToCopy)
}

public class MyClass : MyInterface
{
    void MyInterface.Copy(MyClass classToCopy)
    {
        //copy code
    } 
}

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:

public interface MyInterface
{
    void Copy(MyClass classToCopy)
}

public class MyClass : MyInterface
{
    void MyInterface.Copy(MyClass classToCopy)
    {
        //copy code
    } 
}
巷雨优美回忆 2024-10-06 04:19:37

这是不可能的。继承类继承所有公共和受保护的成员、方法和属性。使用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.

溇涏 2024-10-06 04:19:37

其他人都这么说,但如果我正确推断你的目标,那就是确保 InheritedClass 用户永远不会使用 MyClass 方法。在这种情况下,请将其从 MyClass 中排除并创建两个继承它的类。

如果 MyBaseClass 不应该被实例化(最有可能),则将其设为抽象。

(已编辑 - 您可能希望在基类中包含属于基类一部分的任何内容的复制代码)

public abstract class MyBaseClass
{
    public MyClass()
    {
        //constructor code
    }
    protected void Copy(MyBaseClass classToCopy)
    {
        //copy code
    }
    // other methods that all inherited classes can use
}

public class MyClass: MyBaseClass
{
    public MyClass():base()
    {
        //constructor code
    }
    public void Copy(MyClass myClassToCopy)
    {
        base.Copy(myClassToCopy);
        //specific copy code for this extensions in this class
    } 
}

public class InheritedClass : MyBaseClass
{
    public InheritedClass():base()
    {
        //constructor code
    }
    public void Copy(InheritedClass inheritedClassToCopy)
    {
        base.Copy(myClassToCopy);
        //specific copy code for this extensions in this class
    } 
}

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)

public abstract class MyBaseClass
{
    public MyClass()
    {
        //constructor code
    }
    protected void Copy(MyBaseClass classToCopy)
    {
        //copy code
    }
    // other methods that all inherited classes can use
}

public class MyClass: MyBaseClass
{
    public MyClass():base()
    {
        //constructor code
    }
    public void Copy(MyClass myClassToCopy)
    {
        base.Copy(myClassToCopy);
        //specific copy code for this extensions in this class
    } 
}

public class InheritedClass : MyBaseClass
{
    public InheritedClass():base()
    {
        //constructor code
    }
    public void Copy(InheritedClass inheritedClassToCopy)
    {
        base.Copy(myClassToCopy);
        //specific copy code for this extensions in this class
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文