简体   繁体   English

C#子类或接口?

[英]C# subclass or interface?

Pretty new to C# -- I was instructed to create a subclass to do a variation of the base class. C#的新手-指示我创建一个子类来对基类进行变体。 Never having done sub classes before, I read a bit and then tried it out. 以前从未做过子类,所以我读了一些然后尝试了一下。 I could only access the private property of MyClass from the sub class after inheriting from the base class. 从基类继承后,只能从子类访问MyClass的私有属性。 What have I done here? 我在这里做了什么? Is this a true sub class, or some sort of nested inheriting one? 这是一个真正的子类,还是某种嵌套的继承类?

    public class MyClass
{
     private string connString;
     // exec stored procedure 1

     public class MySubClass : MyClass
     {
            otherClass o = new otherClass(connString);
            // exec stored procedure 2
     }
}

Whatever I did, it seems to work. 无论我做什么,它似乎都有效。 Also, while on the topic of sub classes, is there a common way to name them? 另外,在子类的主题上,是否有一种通用的命名方式? eg MyClass_SubClass, or _MyClass etc? 例如MyClass_SubClass或_MyClass等?

Thanks much! 非常感谢!

Edit: Thanks again, everyone! 编辑:再次谢谢大家! I think I was looking for this answer here . 我想我在这里寻找这个答案。 Regardless, I realized that I'd misunderstood the task -- no need for nesting or inheritance at all! 无论如何,我意识到我会误解任务-根本不需要嵌套或继承! Just created a separate similar class. 刚刚创建了一个单独的类似类。

You have nested your classes, which is something you should probably avoid until you understand C# and OOP better (and then you inherit, which is just weird). 您已经嵌套了类,这可能是您应该避免的事情,直到您对C#和OOP有了更好的了解(然后再继承,这很奇怪)。

Subclassing or derivation, is accomplished through inheritance . 子类化或派生是通过继承完成的。 In your example: 在您的示例中:

public class MyClass
{
     private string connString;
     // exec stored procedure 1
}

public class MySubClass : MyClass
{
      otherClass o = new otherClass(connString);
      // exec stored procedure 2
}

To allow MySubClass to access a member of MyClass , that member needs to be marked as protected or higher. 要允许MySubClass访问MyClass成员,需要将该成员标记为protected或更高。 private members cannot be accessed outside of the containing class. private成员不能在包含类之外访问。

public class MyClass
{
     protected string connString;
     // exec stored procedure 1
}

MySubClass is really MyClass.MySubClass. MySubClass实际上是MyClass.MySubClass。 Normally, a plain subclass cannot access private members. 通常,普通子类无法访问私有成员。

A normal subclass would be in a separate file as a standalone class. 普通子类将作为独立类在单独的文件中。 If the subclass is supposed to be permitted access to the base class members, then you would declare them as "protected" rather than "private". 如果应该允许子类访问基类成员,则应将其声明为“受保护”而不是“私有”。

One the subject of naming, usually the name represents the type, regardless of base class. 命名的主题之一,通常名称代表类型,而与基类无关。 For example, if you have a base class of Animal, it's reasonable that you have subclasses called Dog and Cat. 例如,如果您有动物的基类,则有一个名为“狗和猫”的子类是合理的。 Or in Windows, you could have a base class called Control and subclasses called Button, TextBox, Label and so on. 或在Windows中,可能有一个称为Control的基类和一个名为Button,TextBox,Label等的子类。

Your code is valid, it's not exactly implementing what you were instructed to implement. 您的代码是有效的,它并没有完全实现您所指示的实现。 You can declare a class within a class. 您可以在一个类中声明一个类。 With your code you can instantiate both classes as follows. 使用您的代码,您可以实例化这两个类,如下所示。

class Program
{
    static void Main(string[] args)
    {
        MyClass myClass = new MyClass();
        MyClass.MySubClass mySubClass = new MyClass.MySubClass();
    }
}

This is pretty unusual, it's not uncommon to have a class declared within a class but normally you would not declare MySubClass as public, you would declare it as private or protected. 这是非常不寻常的,在一个类中声明一个类并不少见,但是通常您不会将MySubClass声明为公共,而是将其声明为私有或受保护。 Like so.... 像这样...

public class MyClass
{
    private string connString;
    private MyClassHelper mySubClass = new MyClassHelper();

    // exec stored procedure 1

    protected class MyClassHelper
    {
        object o = new object();
        // exec stored procedure 2
    }
}

...So MyClassHelper exists only within the scope of MyClass. ...因此MyClassHelper仅存在于MyClass范围内。 I've never seen code in which an inner class inherits the outer class, it compiles perfectly, you would just never do it, you could easily create circular references. 我从未见过这样的代码:内部类继承了外部类,它可以完美地编译,您永远也不会做,可以轻松地创建循环引用。

BradleyDotNET's answer tells you how to implement a subclass of MyClass. BradleyDotNET的答案告诉您如何实现MyClass的子类。 This is also called inheritance read more about it here... http://msdn.microsoft.com/en-us/library/ms173149(v=vs.80).aspx 这也称为继承。有关更多信息,请参见此处。http://msdn.microsoft.com/zh-cn/library/ms173149(v=vs.80).aspx

As a word of warning I wouldn't go too crazy with class inheritance, there are many types of relationships between classes, in comparison class inheritance is quite restrictive . 提醒一下,我对类继承不会太疯狂,类之间存在许多类型的关系,相比之下, 类继承是相当严格的

The title of your question is subclass or interface. 您的问题的标题是子类或接口。 Interfaces are something you should be looking at and understanding, they are very important especially for implementing SOLID code. 接口是您应该查看和理解的东西,它们对于实现SOLID代码非常重要。 In my code classes inherit interfaces far more commonly than they inherit classes. 在我的代码中,类继承接口的方式比它们继承类的要普遍得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM