简体   繁体   English

抽象类中的嵌套抽象类以及如何实现它

[英]Nested abstract class in an abstract class and how to implement it

I have an abstract class A and a abstract method with a parameter which is again abstract class B defined in the same abstract class A. When I extended this abstract class A as apart of another class C how can I implement the method with parameter of nested abstract class. 我有一个抽象类A和一个带有参数的抽象方法,该参数又是在同一个抽象类A中定义的抽象类B.当我将这个抽象类A扩展为另一个类C的一部分时,我如何用嵌套参数实现该方法抽象类。

public abstract class A<T, V>
{
    public abstract int GetObject(T t, V v);
    public abstract int GetAnotherObject(B b);
    public abstract class B{}
}

This class is extended by another class C 这个类由另一个C类扩展

public class C: A<ABC, DEF>
{
        public C()
        {

        }
        public override int GetObject(ABC abc, DEF def)
        {
            return 10;
        }

        public override int GetAnotherObject(B b)
        {
            return 15;
        }
}

How to implement class B with some properties and pass in GetAnotherObject method. 如何使用一些属性实现B类并传入GetAnotherObject方法。 Could someone please help me. 有人可以帮帮我吗

From ECMA: 来自ECMA:

Any class nested inside a generic class declaration or a generic struct declaration (§25.2) is itself a generic class declaration, since type parameters for the containing type shall be supplied to create a constructed type. 嵌套在泛型类声明或泛型结构声明(第25.2节)中的任何类本身都是泛型类声明,因为应提供包含类型的类型参数以创建构造类型。

So, you cannot implement nested B without providing type arguments for A . 因此,如果不为A提供类型参数,则无法实现嵌套B

void Main()
{
    var c = new C();
    var result = c.GetAnotherObject(new BImpl<string, int>());
}

public class BImpl<T, V> : A<T, V>.B
{
    public override int BM()
    {
        return 1;
    }
}

// Or you can supply type arguments right here
//public class BImpl : A<string, int>.B
//{
//  public override int BM()
//  {
//      return 1;
//  }
//}

public abstract class A<T, V>
{
    public abstract int GetObject(T t, V v);
    public abstract int GetAnotherObject(B b);
    public abstract class B
    {
        public abstract int BM();
    }
}

public class C : A<string, int>
{
    public C()
    {

    }

    public override int GetObject(string abc, int def)
    {
        return 10;
    }

    public override int GetAnotherObject(B b)
    {
        return b.BM();
    }
}

You're very close already. 你已经非常接近了。

public class C<ABC, DEF> : A<ABC, DEF>
{
    public C()
    {

    }
    public override int GetObject(ABC abc, DEF def)
    {
        return 10;
    }

    // since B is a nested class of A, it has no scope outside of A
    // outside of the definition of A, it must always be referred to as A.B
    public override int GetAnotherObject(A<ABC,DEF>.B b)
    {
        return 15;
    }
}

public class D : A<ABC,DEF>.B
{
    // implementation of A.B
}

Keep in mind that C will always take exactly AB . 请记住, C始终采用完全AB You will never be able to define an implementation of AB (let's call it D ) and have C 's method signature refer to that in the override. 你将永远无法定义AB的实现(让我们称之为D )并且让C的方法签名引用覆盖中的那个。 GetAnotherObject is defined in A as taking an AB and must therefore be implemented to accept any AB , not some specific implementation of AB . GetAnotherObject处于定义为采用的AB ,因此必须实现接受任何 AB ,而不是一些具体实施AB

RE: your comment on how to implement AB inside C RE:您对如何在C实现AB评论

There is no point to implementing AB inside C . C实现AB是没有意义的。 C will still have to have AB in its method signature. C仍然必须在其方法签名中使用AB But if you really must, for some reason. 但如果你真的必须,出于某种原因。

public class C<ABC, DEF> : A<ABC, DEF> 
{
    // C's implementation of A

    public override int GetAnotherObject(A<ABC,DEF>.B b)
    {
        return 15;
    }

    public class D : A<ABC,DEF>.B
    {
        // implementation of A.B
    }
}

Note that GetAnotherObject still takes an AB , not a D . 请注意, GetAnotherObject仍然采用AB而不是D

How about 怎么样

public class C<ABC, DEF> : A<ABC, DEF>
{
    public C()
    {

    }
    public override int GetObject(ABC abc, DEF def)
    {
        return 10;
    }

    public override int GetAnotherObject(B b)
    {
        return 15;
    }
}

Just postfix the class with the generics. 只需使用泛型对类进行后缀即可。

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

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