gpt4 book ai didi

C#泛型和继承问题

转载 作者:行者123 更新时间:2023-11-30 15:47:20 24 4
gpt4 key购买 nike

嘿,我想知道我正在尝试做的事情是否可行?代码中的注释应该给出并了解我正在努力实现的目标:)

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;
}

这可能是一个设计问题,如果是的话,我愿意重新考虑:)

最佳答案

我建议将 DoTest 方法提取到 super 接口(interface),如下所示:

interface ITestable
{
bool DoTest();
}

interface ITest<T> : ITestable
{
T t { get; }
}

public class TestTest
{
ITestable myTest;
}

与此无关,不建议类名以“I”开头,属性以小写字符开头。

关于C#泛型和继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3696658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com