using base classes on other class constructor C#
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, pric开发者_如何学Pythone) " 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
It calls base class (Stock
) constructor. If you look in Stock
class code it looks like this
public class Stock {
private string _symbol;
private double _price;
public Stock(string symbol, double price) // this constructor is called
{
this._symbol = symbol;
this._price = price;
}
}
Note that it is only constructor in Stock
class so you must call it explicit in all derived classes by base(symbol, price)
.
This construct means that the IBM
constructor calls the Stock
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)
Just check following resource for better understanding of that construction: Constructors in C#
精彩评论