在其他类构造函数上使用基类 C#
我正在尝试使用 C# 来理解观察者模式,首先我有一个名为 Stock 的抽象类作为主题,然后我创建一个creteSubject 类,所以我将其称为 IBM,因为creteSubject 类将继承来自股票,所以我做了这样的事情:
class IBM : Stock
{
// Constructor
public IBM(string symbol, double price)
: base(symbol, price)
{
}
}
我不明白的是“:base(符号,价格)”为什么我应该使用它?这意味着什么?它看起来像继承了符号和价格变量,但为什么如果它们被声明为公共 IBM 函数上的参数,
我从我在以下位置找到的示例中获取此代码:
http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1
I'm trying to understand the observer pattern using C#, first I have an abstract class working as Subject called Stock, then I'm creating a concreteSubject class so I'm going to call it IBM, as the concreteSubject Class is going to inherit from Stock, so I do something like this:
class IBM : Stock
{
// Constructor
public IBM(string symbol, double price)
: base(symbol, price)
{
}
}
what I don't understand is the " : base(symbol, price) " why should I use it? what that means? it looks like its inherit the symbol and price variables, but why if they are declared as parameters on the public IBM function
I get this code from an example that I found in:
http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它调用基类 (
Stock
) 构造函数。如果您查看Stock
类代码,它看起来像这样请注意,它只是
Stock
类中的构造函数,因此您必须在所有派生类中通过base(符号,价格)
。It calls base class (
Stock
) constructor. If you look inStock
class code it looks like thisNote that it is only constructor in
Stock
class so you must call it explicit in all derived classes bybase(symbol, price)
.此构造意味着
IBM
构造函数使用相同的参数调用Stock
构造函数。IBM
构造函数中通常会有一些额外的代码。MSDN 上有一些示例 使用构造函数(C# 编程指南)
This construct means that the
IBM
constructor calls theStock
constructor using the same parameters.There would normally be some extra code in the
IBM
constructor.There's some examples on the MSDN Using Constructors (C# Programming Guide)
只需检查以下资源即可更好地理解该构造:C# 中的构造函数
Just check following resource for better understanding of that construction: Constructors in C#