简体   繁体   English

以抽象类作为参数的方法

[英]Method having an abstract class as a parameter

I have an abstract class A, where I have derived the classes B and C. Class A provides an abstract method DoJOB(), which is implemented by both derived classes. 我有一个抽象类A,在其中派生了类B和C。类A提供了一个抽象方法DoJOB(),该方法由两个派生类实现。

There is a class X which has methods inside, which need to call DoJOB(). 有一个X类,其内部具有方法,需要调用DoJOB()。 The class X may not contain any code like B.DoJOB() or C.DoJOB(). X类不能包含任何代码,例如B.DoJOB()或C.DoJOB()。

Example: 例:

public class X
{
private A foo;

public X(A concrete)
{
foo = concrete;
}

public FunnyMethod()
{
foo.DoJOB();
}

}

While instantiating class XI want to decide which derived class (B or C) must be used. 在实例化类XI时,要决定必须使用哪个派生类(B或C)。 I thought about passing an instance of B or C using the constructor of X. 我考虑过使用X的构造函数传递B或C的实例。

X kewl = new X(new C());
kewl.FunnyMethod(); //calls C.DoJOB()

kewl = new X(new B());
kewl.FunnyMethod(); // calls B.DoJOB()

My test showed that declaring a method with a parameter A is not working. 我的测试表明,声明带有参数A的方法无效。 Am I missing something? 我想念什么吗? How can I implement this correctly? 如何正确实施?

(A is abstract, it cannot be instantiated) (A是抽象的,无法实例化)

EDIT: Sorry, I forgot sth. 编辑:对不起,我忘记了。

class A is a generic abstract singleton: A类是一个通用的抽象单例:

abstract public class A<T> where T : A<T>
{
    ....
}

public sealed class B : A<B>
{
    .....
}

public sealed class C : A<C>
{
    .....
}

See the example: http://www.c-sharpcorner.com/UploadFile/snorrebaard/GenericSingleton11172008110419AM/GenericSingleton.aspx 请参阅示例: http : //www.c-sharpcorner.com/UploadFile/snorrebaard/GenericSingleton11172008110419AM/GenericSingleton.aspx

Under the head line "The solution with the Generic Singleton as an abstract class" 在标题“以通用单例作为抽象类的解决方案”下

You must have made a mistake in the test, the code works fine: 您一定在测试中犯了一个错误,代码可以正常工作:

void Main()
{
X kewl = new X(new C());
kewl.FunnyMethod(); //calls C.DoJOB()

kewl = new X(new B());
kewl.FunnyMethod(); // calls B.DoJOB()

}

public class X
{
    private A foo;

    public X(A concrete)
    {
        foo = concrete;
    }

    public void FunnyMethod()
    {
        foo.DoJOB();
    }
}

public abstract class A
{
    public abstract void DoJOB();
}

public class B : A
{
    public override void DoJOB()
    {
        Console.WriteLine("B");
    }
}

public class C : A
{
    public override void DoJOB()
    {
        Console.WriteLine("C");
    }
}

Outputs : 输出:
C C
B

Works for me. 为我工作。 I get the expected 我得到了期望

I did something interesting!
So Did I!

when I run it. 当我运行它时。

Paste this in your Visual Studio and smoke it 将此粘贴到Visual Studio中并抽烟

using System;

namespace TestDrive
{
    class Program
    {
        static void Main( string[] args )
        {
            ServiceConsumer x = new ServiceConsumer( new ConcreteService2() ) ;

            x.FunnyMethod() ;

            return ;
        }

    }

    abstract class AbstractService
    {
        public abstract void DoSomethingInteresting() ;
    }

    class ConcreteService1 : AbstractService
    {
        public override void DoSomethingInteresting()
        {
            Console.WriteLine("I did something interesting!");
            return ;
        }
    }

    class ConcreteService2 : ConcreteService1
    {
        public override void DoSomethingInteresting()
        {
            base.DoSomethingInteresting() ;
            Console.WriteLine("So Did I!");
            return ;
        }
    }

    class ConcreteService : AbstractService
    {
        public override void DoSomethingInteresting()
        {
            Console.WriteLine("Not It's my turn to do something interesting!") ;
            return ;
        }
    }

    class ServiceConsumer
    {
        private AbstractService Service ;
        public ServiceConsumer( AbstractService serviceInstance )
        {
            this.Service = serviceInstance ;
            return ;
        }
        public void FunnyMethod()
        {
            Service.DoSomethingInteresting() ;
            return ;
        }
    }
}

Cheers! 干杯!

I'm not sure I understand the question, here is my implementation and it works: 我不确定我是否理解这个问题,这是我的实现,并且可以正常工作:

namespace CSharpConsole { 命名空间CSharpConsole {

public abstract class A {
    public abstract void Test();
}
public class B : A {
    public override void Test() {
        System.Console.WriteLine("B:Test called!");
    }
}
public class C : A {
    public override void Test() {
        System.Console.WriteLine("C:Test called!");
    }
}    
class Program {
    private A _concrete;
    public Program(A concrete) {
        _concrete = concrete;
    }
    public void DoTest() {
        _concrete.Test();
    }
    static void Main(string[] args) {
        Program pb = new Program(new B());
        pb.DoTest();
        Program pc = new Program(new C());
        pc.DoTest();
    }
}

} }

For your edit: 对于您的编辑:

void Main()
{
var kewl = new X<C>(new C());
kewl.FunnyMethod(); //calls C.DoJOB()

var kewl2 = new X<B>(new B());
kewl2.FunnyMethod(); // calls B.DoJOB()

}

public class X <T> where T : A<T>
{
    private A<T> foo;

    public X(A<T> concrete)
    {
        foo = concrete;
    }

    public void FunnyMethod()
    {
        foo.DoJOB();
    }
}

public abstract class A<T> where T : A<T>
{
    public abstract void DoJOB();
}

public class B : A<B>
{
    public override void DoJOB()
    {
        Console.WriteLine("B");
    }
}

public class C : A<C>
{
    public override void DoJOB()
    {
        Console.WriteLine("C");
    }
}

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

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