c#多个类使用相同的方法

c#多个类使用相同的方法,c#,design-patterns,C#,Design Patterns,我有四节课 class A { private List<InputSelector> _lstA = new List<InputSelector(); private List<InputSelector> _lstB = new List<InputSelector(); private List<InputSelector> _lstC = new List<InputSelector(); publ

我有四节课

class A {
    private List<InputSelector> _lstA = new List<InputSelector();
    private List<InputSelector> _lstB = new List<InputSelector();
    private List<InputSelector> _lstC = new List<InputSelector();

    public A(){
        _lstA.Add(new InputSelector{ description = "ItemA"});
        _lstB.Add(new InputSelector{ description = "ItemB"});
        _lstC.Add(new InputSelector{ description = "ItemC"});
    }

    public List<InputSelector> getInputSelector(InputSelectorEnum input) {
        switch(type){
             case(InputSelectorEnum.A):
                 return _lstA;
             case(InputSelectorEnum.B):
                 return _lstB;
             case(InputSelectorEnum.C):
                 return _lstC;
        }
    }
}
[...]
class D {
    private List<InputSelector> _lstA = new List<InputSelector();
    private List<InputSelector> _lstB = new List<InputSelector();
    private List<InputSelector> _lstC = new List<InputSelector();

    public D(){
        _lstA.Add(new InputSelector{ description = "ItemA"});
        _lstB.Add(new InputSelector{ description = "ItemB"});
        _lstC.Add(new InputSelector{ description = "ItemC"});
    }

    public List<InputSelector> getInputSelector(InputSelectorEnum input) {
        switch(type){
             case(InputSelectorEnum.A):
                 return _lstA;
             case(InputSelectorEnum.B):
                 return _lstB;
             case(InputSelectorEnum.C):
                 return _lstC;
        }
    }
}
A类{

private List _lstA=new List您的类完全相同。它们可以合并为一个类


您几乎不需要完全相同的类。这是因为类只是对象的蓝图。两个蓝图并不比一个更有用。您在这里的意图可能是创建4个相同的对象,
A
B
C
D
,您希望稍后对它们进行变异,但您意外地发现了所有这些对象y改为创建了4个类。

你能指出这些类有什么不同吗?我看不出有什么不同。如果它们都一样,为什么不只用一个类来代替呢?这是一个非常相关的问题,你确实是对的。这两个类之间没有区别。它们完全一样。我现在觉得有点愚蠢,不得不责怪它们最近工作太多,睡眠太少。把它作为一个答案贴出来,我会给你评分的,谢谢!而且…你的课程可以实现一个字典来避免切换。谢谢!好建议!