嘿,我想知道我想要做甚么可能吗?在代码中的注释应该给和想法是什么,我想才达到:)C#泛型和继承问题
interface ITest<T> {
T t { get; }
bool DoTest();
}
public abstract class Test<T> : ITest<T> {
public Test (T nt) {
this.t = nt;
}
public Test() {
}
public T t {
get;
private set;
}
public abstract bool DoTest();
}
public class STest : Test<string> {
public override bool DoTest() {
return true;
}
}
public class ITest : Test<int> {
public override bool DoTest() {
return true;
}
}
public class TestTest {
// I don't want to specify type here, I'd like TestTest to be able to have
// either a ITest or a STest. But for this class it should not matter.
// I just want to use DoTest() later on. No matter what
// specialication of Test this is.
Test myTest;
}
这可能是一个设计问题,我愿意重新考虑,如果是:)
呀。当你可以称之为'StringTest' /'IntTest'时,也不建议调用'STest' /'ITest',它的可读性会提高一百万倍。 – Timwi 2010-09-12 21:36:00
我不认为特定的命名约定是这里最重要的方面。 – 2010-09-12 21:37:34
@Ondrej:没有人说它是*最重要的。但是,任何使代码更易于理解的代码只会使OP受益。 – 2010-09-12 23:23:26