简体   繁体   English

如何要求在 C# 中实现抽象 class?

[英]How to require an implementation of an abstract class in C#?

I want to build a class that would have a property, in which there is an instance of a class, which implements an abstract class.我想构建一个具有属性的 class,其中有一个 class 的实例,它实现了一个抽象的 class。 Here's and example.这是和示例。

public class MyClass {
    public MyDerivedClassA derived;
    public void mainClassUtility () {
        derived.foo();
    }
}

public abstract class MyAbstractBaseClass {
    public abstract void foo();
}

public class MyDerivedClassA : MyAbstractBaseClass {
    public override void foo(){
        return;
    }
}

public class MyDerivedClassB : MyAbstractBaseClass
{
    public override void foo()
    {
        return;
    }
}

Basically, I want to make sure the object I'm using is derived from an abstract class and implements all the methods I will need to use.基本上,我想确保我使用的 object 源自抽象 class 并实现了我需要使用的所有方法。 There will be many implementations of the abstract class and depending on the current state of the program, MyClass might be using different implementations of the ABC.抽象 class 将有许多实现,根据程序的当前 state,MyClass 可能使用 ABC 的不同实现。 I want to write the program in a way, that no matter what implementation of the ABC is currently being used, there is a way to call it's methods by MyClass.我想以某种方式编写程序,无论当前使用的是哪种 ABC 实现,都有一种方法可以通过 MyClass 调用它的方法。 What would be the best solution to this problem?这个问题的最佳解决方案是什么?

Unless I'm misunderstanding the question, you're pretty much there.除非我误解了这个问题,否则您几乎就在那里。 Have MyClass expect a property of the abstract base class and you should be all set.MyClass期望抽象基础 class 的属性,您应该准备就绪。

using System;

public class Program
{
    public static void Main()
    {
        var myClassOne = new MyClass(new MyDerivedClassA());
        var myClassTwo = new MyClass(new MyDerivedClassB());

        myClassOne.mainClassUtility();
        myClassTwo.mainClassUtility();
    }

    public class MyClass 
    {   
        public MyAbstractBaseClass Derived;

        public MyClass(MyAbstractBaseClass derived)
        {
            Derived = derived;
        }

        public void mainClassUtility () 
        {
            Derived.foo();
        }
    }

    public abstract class MyAbstractBaseClass 
    {
        public abstract void foo();
    }

    public class MyDerivedClassA : MyAbstractBaseClass 
    {
        public override void foo()
        {
            Console.WriteLine("I am MyDerivedClassA");
            return;
        }
    }

    public class MyDerivedClassB : MyAbstractBaseClass
    {
        public override void foo()
        {
            Console.WriteLine("I am MyDerivedClassB");
            return;
        }
    }
}

How to require an implementation of an abstract class in C#?如何要求在 C# 中实现抽象 class?

You can not instantiate a abstract class - and thus can not use it for most cases.您不能实例化抽象 class - 因此在大多数情况下不能使用它。 Except as variable/argument/generic type argument.除了作为变量/参数/泛型类型参数。 You need to make a concrete (non-abstract) class that inherits from it.您需要制作一个继承自它的具体(非抽象)class。 You can only use the abstract class as a variable/argument type.您只能使用抽象 class 作为变量/参数类型。 To guarantee that only stuff that inherits from it can be used there.保证只有从它继承的东西可以在那里使用。

Basically, I want to make sure the object I'm using is derived from an abstract class and implements all the methods I will need to use.基本上,我想确保我使用的 object 源自抽象 class 并实现了我需要使用的所有方法。

Then use the abstract class as type argument.然后使用抽象 class 作为类型参数。 It means only instaces of the abstract class (of wich there can be no instance) or instances of classes that inherit from it (that somebody else writes) can be used at that place.这意味着只有抽象 class 的实例(不能有实例)或从它继承的类的实例(其他人编写的)可以在那个地方使用。

Note that Abstract classes and Interfaces overlap in nearly all uses.请注意,抽象类和接口几乎在所有用途中都有重叠。 There is a miriad small differences, but I do not think they mater.有很多微小的差异,但我认为它们并不重要。 The only big difference I can see, is one of exclusivity:我能看到的唯一大区别是排他性之一:

  • a class can implement as many Interfaces as it wants. class 可以实现任意数量的接口。
  • You can only inherit from one abstract class.您只能从一个抽象 class 继承。 that means it is for a primary, exclusive purpose.这意味着它是为了一个主要的、排他的目的。 That way you prevent some dumb ideas, like someone trying to make a Windows Form that is also a DBConnection.这样你就可以防止一些愚蠢的想法,比如有人试图制作一个也是 DBConnection 的 Windows 表单。

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

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