gpt4 book ai didi

c# - 如何在基类中封装一个属性?

转载 作者:太空狗 更新时间:2023-10-29 19:43:01 25 4
gpt4 key购买 nike

我的场景是关于开发数学问题。作为一个IProblem 接口(interface),我认为它应该包含的两个主要属性是QuestionTextResponseQuestionText 始终是字符串,但 Response 有时可以是复杂对象(自定义 Fraction struc)或其他数据类型,如字符串、小数、整数等

    public interface IProblem
{
string QuestionText { get; set; }
object Response { get; }

bool IsComplete();
bool IsCorrect();
}

如您所见,Response 是对象。我猜这个数据类型是因为所有的问题本质上都有一个响应。由于它是一个对象,我只为将来的错误(转换问题)定义 get。

我的想法是后来,在一个具体的类中访问这个属性(Response),而不需要强制转换。检查一下?

    public abstract class Problem : IProblem
{
public string QuestionText { get; set;}
public object Response { get; protected set; }
public virtual bool IsComplete()
{
return true;
}
public abstract bool IsCorrect();
}

public class BinaryProblem : Problem
{
public decimal N1 { get; set; }
public decimal N2 { get; set; }
public decimal Response
{
get { return (decimal)base.Response; }
set { base.Response = value; }
}

public override bool IsCorrect()
{
return N1 + N2 == Response;
}
}

我在这里测试值。

    static void Main(string[] args)
{
BinaryProblem p = new BinaryProblem();
p.N1 = 2;
p.N2 = 4;

p.Response = 6;

IProblem p2 = p;
Console.WriteLine(p2.Response);
Console.WriteLine(p2.IsComplete().ToString());
}

到现在为止,它仍然有效,但我想知道我正在做的是正确的还是好的做法。我见过其他人使用 new 运算符来做到这一点。其他人不使用 base 这个词。

这样好吗?它会导致 future 的错误吗?请给我关于我的设计的反馈。

编辑:确实有必要在非通用接口(interface)中访问响应。

最佳答案

也许您正在寻找这样的东西?请注意,我遗漏了一些内容,因为它们对问题的通用解决方案部分(如 QuestionText)并不重要。我也遗漏了基类,因为它看起来只不过是一个传递,一个额外的、不必要的层。这可能不是您要找的东西,但我希望它能帮到您。

首先,这是所有东西的使用方式:
编辑:注意现在如何将它们全部视为非通用 IProblem。

private static void StackOverflowQuestion()
{
IProblem<int> problem1 = new IntProblem(2, 4);
problem1.Response = 6;

IProblem<decimal> problem2 = new DecimalProblem(5, 10);
problem2.Response = .5M;

Console.WriteLine("Problem 1 is correct: {0}", problem1.IsCorrect());
Console.WriteLine("Problem 2 is correct: {0}", problem2.IsCorrect());

List<IProblem> problems = new List<IProblem>();
problems.Add(problem1);
problems.Add(problem2);
problems.ForEach(problem => Debug.WriteLine(problem.GetResponse()));
}

编辑:这是非通用接口(interface),因此可以在列表中使用许多问题并以相同的方式处理:

public interface IProblem
{
object GetResponse();
}

界面如下:
编辑:请注意,这现在实现了非通用接口(interface)。

public interface IProblem<T> : IProblem
{
T Response { get; set; }
bool IsCorrect();
}

下面是类:
编辑:注意新的 GetResponse() 方法。

public class IntProblem : IProblem<int>
{
private int _number1 { get; set; }
private int _number2 { get; set; }

public int Response { get; set; }

public IntProblem(int number1, int number2)
{
this._number1 = number1;
this._number2 = number2;
}

public bool IsCorrect()
{
return this._number1 + this._number2 == Response;
}

public object GetResponse()
{
return this.Response;
}
}

public class DecimalProblem : IProblem<decimal>
{
private decimal _number1 { get; set; }
private decimal _number2 { get; set; }

public decimal Response { get; set; }

public DecimalProblem(decimal number1, decimal number2)
{
this._number1 = number1;
this._number2 = number2;
}

public bool IsCorrect()
{
return this._number1 / this._number2 == Response;
}

public object GetResponse()
{
return this.Response;
}
}

关于c# - 如何在基类中封装一个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10237043/

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