cch4继承

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
• Abstract classes and functions
▫ An abstract class cannot be instantiated, whereas an abstract function does not have an implementation, and must be overridden in any non-abstract derived class.
abstract class Building {
public abstract decimal CalculateHeatingCost(); // abstract method }
▫ An abstract function is automatically virtual. ▫ If any class contains any abstract functions, that class
14/41
3. Constructors of Derived Classes
• Constructors with no parameters
15/41
3. Constructors of Derived Classes
public abstract class GenericCustomer {
class MyDerivedClass: MyBaseClass {
// functions and data members here }
public class MyDerivedClass: MyBaseClass, IInterface1, IInterface2 {
// etc. }
public struct MyDerivedStruct: IInterface1, IInterface2 {
▫ With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden.
12/41
3. Constructors of Derived Classes
abstract class GenericCustomer
{
private string name;
// lots of other methods etc.
}
class Nevermore60Customer: GenericCustomer
• Hiding methods
class HisBaseClass {
// various members } class MyDerivedClass: HisBaseClass {
public new int MyGroovyMethod() {
// some groovy implementation return 0; } }
• Structs and Classes
▫ It is not possible to code a type hierarchy of structs; however, it is possible for structs to implement interfaces.
▫ Structs are always derived from System.ValueType. They can also be derived from any number of interfaces.
3/41
1. Types of Inheritance
• Multiple inheritance
▫ C# does not support multiple implementation inheritance.
▫ It does allow types to be derived from multiple interfaces — multiple interface inheritance.
// etc. }
6/41
2. Implementation Inheritance
• Virtual methods
class MyBaseClass
{
public virtual string VirtualMethod()
{
return "This method is virtual and defined in MyBaseClass“;
}
}
class GoldAccount: CustomerAccount
{
public override decimal CalculatePrice()
{
return base.CalculatePrice() * 0.9M;
}
}
9/41
2. Implementation Inheritance
▫ Every C# class (except for Symtem.Object) has exactly one base class, and may additionally have any number of base interfaces.
4/41
1. Types of Inheritance
public abstract class GenericCustomer {
private string name; public GenericCustomer() : base() // We could omit this line without affecting the compiled code. {
is also abstract.
10/41
2. Implementation Inheritance
• Sealed classes and functions
▫ Sealed class means that you can’t inherit from that class. Sealed method means that you can’t override that method.
2/41
1. Types of Inheritance
• Interface inheritance
▫ Interface inheritance means that a type inherits only the signatures of the functions and does not inherit any implementations.
{
private uint highCostMinutesUsed;
// other methods etc.
name
}
highCostMinutesUsed
GenericCustomer customer = new Nevermore60Customer();
13/41
3. Constructors of Derived Classes
4 Inheritance
Shuai Lü 2012.8.15
College of Computer Science & Technology
JILIN UNIVERSITY
1. Types of Inheritance
• Implementation inheritance
▫ Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions.
private string name; public GenericCustomer()
zero-parameter constructor for GenericCustomer
: base() // We could omit this line without affecting the compiled code.
}
}
class MyDerivedClass: MyBaseClass
{
public override string VirtualMethod()
{
return “This method is an override defined in MyDerivedClass.”;
}
}
7/41
2. Implementation Inheritance
{
name = "<no name>";
}
class Nevermore60Customer: GenericCustomer
{
private uint highCostMinutesUsed;
// other methods etc.
}
}
16/41
3. Constructors of Derived Classes
8/n Inheritance
• Calling base versions of functions
class CustomerAccount
{
public virtual decimal CalculatePrice()
{
return 0.0M; // implementation
11/41
3. Constructors of Derived Classes
• When you create an instance of a derived class, there is actually more than one constructor at work.
• The constructor of the class you instantiate isn’t by itself sufficient to initialize the class — the constructors of the base classes must also be called.
▫ Classes are always derived from one other class of your choosing. They can also be derived from any number of interfaces.
5/41
2. Implementation Inheritance
▫ In order to use the sealed keyword on a method or property, it must have first been overridden from a base class.
▫ The .NET base class library frequently uses sealed classes to make these classes inaccessible to thirdparty developers who might want to derive their own classes from them.
• No explicit constructors
▫ Notice the order in which this happens. It’s always the base class constructors that get called first.
▫ This means that there are no problems with a constructor for a derived class invoking any base class methods, properties, and any other members that it has access to, because it can be confident that the base class has already been constructed and its fields initialized.
相关文档
最新文档