Inheritance and polymorphism

简介:   sealed keyword. base keyword. protected keyword. Contain/delegate model(has-a relationship): Class A { } Partial cl...

 

sealed keyword.

base keyword.

protected keyword.

Contain/delegate model(has-a relationship):

Class A

{

}

Partial class B

{

  protected A a=new A();

}

public partial class B

{

  protected A a=new A();

  public A AProperty

  {

    set{ return a; }

    get{ a=value; }

  }

}

 

Nested type definition:

public class OuterClass

{

  public class PublicInnerClass(){}

  private class PrivateInnerClass(){}

}

 

virtual and override keyword

 

Class B: A

{

  public override sealed void GetPropertyX()

  {

    ...

   }

}

 

Abstract class(cannot new)

Abstract method only can define in abstract class.

 

Member shadowing:

Class A

{

  public string shapeName;

  public void Draw()

  {

      //doing something that we cannot modify

  }

}

 

Class B: A

{

  protected new string shapeName;

  public new void Draw()

  {

      //doing what we want

   }

}

 

Base/derive converting rule:

Object o=new A();// "is-a" relationship

Baseclass bc=new Derivedclass();

 

"as" and "is" keyword

 

"as": Convert when running,

object is not compatible will return null,

Object is null will throw NullReferenceException;

"is": not compatible will return false;

目录
打赏
0
0
0
0
20
分享
相关文章
|
4月前
|
多态(Polymorphism)
多态(Polymorphism)
37 1
|
5月前
|
java继承和多态详解
java继承和多态详解
72 5
|
10月前
|
C++
C++面向对象编程中的 纯虚函数 与 抽象类
C++面向对象编程中的 纯虚函数 与 抽象类
|
C++
C++ 面向对象特征4 多态(Polymorphism)
C++ 面向对象特征4 多态(Polymorphism)
52 0
C++ 中的多态性(Polymorphism)
C++ 中的多态性(Polymorphism)
93 0
类的继承多态
类的继承多态
109 0
Java继承和多态
Java继承和多态
123 0
继承(Inheritance)
继承(Inheritance)
107 0