- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的场景是关于开发数学问题。作为一个IProblem
接口(interface),我认为它应该包含的两个主要属性是QuestionText
和Response
。 QuestionText
始终是字符串,但 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/
我需要使用封装打印3个变量,并且无法修改主类。 主类声明变量[汽缸、制造商、所有者],并希望使用如下参数创建和打印一个对象: System.out.println (new Vehicle (cyli
我正在研究“Beginning Ruby”这本书,在我进行封装之前一切都进行得很顺利。我明白这段代码在做什么,我只是不知道为什么要用这些方法设置它。 对于那些感兴趣的人 this is the lin
所以我一直在研究面向对象的完整开发 JavaScript 实践,并对以下示例感到好奇。 据我了解,(这对我来说很有意义)以下“ secret ”字段是“私有(private)”的: var MyObj
在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装、隐藏起来的方法。 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代
封装 被定义为 把一个或多个项目封闭在一个物理的或者逻辑的包中 。在面向对象程序设计方法论中,封装是为了防止对实现细节的访问。 抽象和封装是面向对象程序设计的相关特性。抽象允许相关信息可
今天我读了一本书,作者写道,在一个设计良好的类中,访问属性的唯一方法是通过其中一个类方法。这是一个被广泛接受的想法吗?为什么封装属性如此重要?不做会有什么后果?我早些时候在某处读到这可以提高安全性或类
今天我接受了软件工程职位的面试。我已经从 stackoverflow 中阅读了很多关于面试的内容。他们向我询问了与 OOP 相关的正常情况。但他们也问我这些: 没有继承就可以封装吗? 没有继承就可以抽
我正在制作一个简单的网站,并编写了gradle构建脚本,该脚本将执行诸如lint并最小化css / html / js之类的事情。 在这一阶段,我刚刚完成了正在运行的CSS,但是只是初始的非结构化格式
下面的幻灯片是指 C++ 语言,它说 "encapsulation is about ensuring the user uses our ADT in a safe way" 但是,如果他可以访问我
尝试设置一个 Person 类 我在尝试将信息封装在类中以使其不会被意外更改时遇到了问题。除了当我尝试使用 setter/getters 进行封装时,该类工作得非常好。我认为问题是这些方法最终会互相循
面向对象的概念:封装、数据抽象和数据隐藏是3个不同的概念,但彼此之间非常相关。所以我很难通过阅读互联网上的信息来完全理解这些概念。一个地方可用的信息与互联网上另一个地方的信息相矛盾。有人可以指导我阅读
我被封装困住了。在我看来,好像我已经按照规则做了一切,但仍然不起作用。虽然我知道我错过了一些东西。我正在使用 Eclipse。 我想要通过创建一副标准 52 张卡片来进行一些练习,并尝试我学到的新东西
在java中,要获取映射中的所有键,我们可以使用方法keySet。但我想知道为什么方法名称不只是 keys ?名称 Set 是否会泄露有关实现的详细信息? 据我了解,Java 是静态类型语言,名称中包
我正在尝试找出如何防止应用程序中的页面变量被全局定义。我想出了一些方法,但我想知道是否有人们使用的通用标准方法。 我使用这种方法得到了我的插件设计模式:http://www.virgentech.co
我有一个 tcp 连接,我想在服务器类的 HandleConnectionAsync 方法中保持打开状态。它将从客户端接收持续更新。 private async void HandleConnecti
这是我类(class)的一个小样本: #include #include using std::string; using std::vector; struct Book { string
我想要类似下面的代码: class Foo{ public: void update() { for( auto dataModifier : _dataModifierLis
这已经困扰我一段时间了,所以我问了一个同事他是否能理解它,现在我来了;) 为什么可以在依赖属性的 PropertyChangedCallback 中访问持有类的私有(private)成员? 让我通过这
我知道封装意味着一个对象不应该直接访问不同对象的成员……但我猜它与公共(public)领域有关?我假设公共(public)方法不会破坏封装..?我只是在这里不清楚,很高兴得到任何解释。 最佳答案
我就废话不多说了,大家还是直接看代码吧~ ? 1
我是一名优秀的程序员,十分优秀!