继承

RTTI

RTTI

概念 RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型。

相关资源

C#高级编程(第11版) Professional C# 7 and .NET Core 2.0

For code comments and issues please check Professional C#'s GitHub Repository

Please check my blog csharp.christiannagel.com for additional information for topics covered in the book.

ObjectOrientation 章节中有介绍,

但是有些内容没有细讲, 比如纯虚函数的子类如何 在全局变量声明,如何在函数中定义,使用


c#

父类,子类

1.VirtualMethods


// fish tail, lungs
// humam limbs(arms and legs), lungs
// bird wings, lungs
// public abstract class Organisms
{
public Int32 age{get;} public virtual void GetAge()
{
WriteLine($"moves with {limbs}");
} public abstract void Move( ); } public class Human: Organisms
{
public Human()
: base() { } public override void Move( ) // must override
{
WriteLine($"Human: moves with legs (using limbs when baby age)");
} } public class Kingkong: Organisms
{
public Human()
: base() { } public override void Move( ) // must override
{
WriteLine($"Kingkong: moves with limbs");
} } public class Fish: Organisms
{
public Fish()
: base() { } public override void Move( ) // must override
{
WriteLine($"Fish: moves with wings and tail");
}
} public class Horse: Organisms //horse zebra
{
public Horse()
: base() { } public override void Move( ) // must override
{
WriteLine($"Horse: moves with legs");
}
}
using System;

namespace VirtualMethods
{
class Program
{
//var _obj = null; //var 无法在全局变量 声明
Organisms _Obj =null; static void Main()
{
var obj = new Human();
obj.age = 23;
obj.GetAge();
obj.Move(); }
static void Init()
{
_Obj = new Fish();
_Obj.age = 2;
_Obj.GetAge();
_Obj.Move();//父类有abstract function. 所有子类函数必须override,
} static void FuncA()
{
Fish fish = _Obj as Fish;//利用全局变量 _Obj
fish.age = 1;
fish.GetAge();
fish.Move();
} }
}

2.InheritanceWithConstructors

//Shape.cs
using System; namespace InheritanceWithConstructors
{
public class Position
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString() => $"X: {X}, Y: {Y}";
} public class Size
{
public int Width { get; set; }
public int Height { get; set; }
public override string ToString() => $"Width: {Width}, Height: {Height}";
} public class Shape
{
public Shape(int width, int height, int x, int y)
{
Size = new Size { Width = width, Height = height };
Position = new Position { X = x, Y = y };
} public Position Position { get; }
public Size Size { get; } public virtual void Draw() => Console.WriteLine($"Shape with {Position} and {Size}"); public virtual void Move(Position newPosition)
{
Position.X = newPosition.X;
Position.Y = newPosition.Y;
Console.WriteLine($"moves to {Position}");
}
}
}
//ConcreteShapes.cs

using System;

namespace InheritanceWithConstructors
{
public class Rectangle : Shape
{
public Rectangle(int width, int height, int x, int y)
: base(width, height, x, y) { } public Rectangle()
: base(width: 0, height: 0, x: 0, y: 0) { } public override void Draw() =>
Console.WriteLine($"Rectangle with {Position} and {Size}"); public override void Move(Position newPosition)
{
Console.Write("Rectangle ");
base.Move(newPosition);
} public void PrintRectangleFuncA( )//只有Rectangle 才有的函数,其他类没有声明和实现
{
Console.Write("Rectangle FuncA ");
}
} public class Ellipse : Shape
{
public Ellipse(int width, int height, int x, int y)
: base(width, height, x, y) { } public Ellipse()
: base(width: 0, height: 0, x: 0, y: 0) { }
}
}
//Program.cs

namespace InheritanceWithConstructors
{
class Program
{
Shape _sharp = null;//全局变量
static void Main(string[] args)
{
var r = new Rectangle();
r.Position.X = 33;
r.Position.Y = 22;
r.Size.Width = 200;
r.Size.Height = 100;
r.Draw();
DrawShape(r); r.Move(new Position { X = 120, Y = 40 });
r.Draw(); Shape s1 = new Ellipse();
DrawShape(s1);
}
public staic void Init()
{
_sharp = new Rectangle();//如果_sharp 是全局变量, 让其他地方也可使用此对象 怎么办?
}
public staic void FuncA()
{
Rectangle rect= _sharp as Rectangle;
rect.PrintRectangleFuncA(); //只有Rectangle 才有的函数,其他类没有声明和实现,可以这样调用
} public static void DrawShape(Shape shape) => shape.Draw();
}
}

3.UsingInterfaces

//基本上和VirtualMethods 类似,但是所有接口类的 子类, 要通用, 子类必须

//IBankAccount.cs
namespace Wrox.ProCSharp
{
public interface IBankAccount
{
void PayIn(decimal amount);//子类必须有实现
bool Withdraw(decimal amount);//子类必须有实现
decimal Balance { get; }//子类必须有实现
}
}
//ITransferBankAccount.cs
namespace Wrox.ProCSharp
{
public interface ITransferBankAccount : IBankAccount
{
bool TransferTo(IBankAccount destination, decimal amount);
}
}
//JupiterBank.cs

using System;

namespace Wrox.ProCSharp.JupiterBank
{
public class GoldAccount : IBankAccount
{
private decimal _balance; public void PayIn(decimal amount) => _balance += amount; public bool Withdraw(decimal amount)//Withdraw撤回
{
if (_balance >= amount)
{
Console.WriteLine($"GoldAccount(IBankAccount): Withdraw(){_balance}");
_balance -= amount;
return true;
}
Console.WriteLine("GoldAccount(IBankAccount): Withdraw attempt failed.");
return false;
} public decimal Balance => _balance; public override string ToString() =>
$"GoldAccount(IBankAccount): Balance = {_balance,6:C}";
} public class CurrentAccount : ITransferBankAccount
{
private decimal _balance; public void PayIn(decimal amount) => _balance += amount; public bool Withdraw(decimal amount)
{
if (_balance >= amount)
{
_balance -= amount;
return true;
}
Console.WriteLine("Withdrawal attempt failed.");
return false;
} public decimal Balance => _balance; public bool TransferTo(IBankAccount destination, decimal amount)
{
bool result = Withdraw(amount);
if (result)
{
destination.PayIn(amount);
}
return result;
} public override string ToString() =>
$"Jupiter Bank Current Account: Balance = {_balance,6:C}";
}
}
//VenusBank.cs

using System;

namespace Wrox.ProCSharp.VenusBank
{
public class SaverAccount : IBankAccount
{
private decimal _balance; public void PayIn(decimal amount) => _balance += amount; public bool Withdraw(decimal amount)
{
if (_balance >= amount)
{
_balance -= amount;
return true;
}
Console.WriteLine("Withdrawal attempt failed.");
return false;
} public decimal Balance => _balance; public override string ToString() =>
$"Venus Bank Saver: Balance = {_balance,6:C}";
}
}
//Program.cs
namespace UsingInterfaces
{
class Program
{
IBankAccount _golbal_obj =null;//全局变量
static void Main()
{
IBankAccount venusAccount = new SaverAccount();
IBankAccount jupiterAccount = new GoldAccount(); venusAccount.PayIn(200);
venusAccount.Withdraw(100);
Console.WriteLine(venusAccount.ToString()); jupiterAccount.PayIn(500);
jupiterAccount.Withdraw(600);
jupiterAccount.Withdraw(100);
Console.WriteLine(jupiterAccount.ToString()); } public static void Init()
{
_golbal_obj = new SaverAccount();//用子类去new
} public static void FunA()
{
if(_golbal_obj!=null)
{
_golbal_obj.PayIn(100);//直接调用SaverAccount 子类的 实现,所以这也是为什么需要子类全部都要实现,
_golbal_obj.Withdraw(100);//直接调用SaverAccount 子类的 实现
}
} }
}

c# 面向对象/继承关系设计的更多相关文章

  1. OC面向对象继承关系和组合关系笔记

    继承关系是描述类和类之间的关系,两个类分别称为子类和父类,子类继承了父类,子类就拥有了父类的属性和方法: 继承的关系特点描述出来就是:** “是” **  (例如:学生类 是 人类) 组合关系描述的语 ...

  2. js面向对象继承

    前言 最近看到js面向对象这章节了,主要学习了原型和面向对象继承关系,为了梳理自己的知识逻辑,特此记录. js的面向对象 先说说我目前了解的js创建对象方法 1.写一个函数,然后通过new创建对象 2 ...

  3. 面向对象设计之------Is-A(继承关系)、Has-A(合成关系,组合关系)和Use-A(依赖关系)(转)

    原文url:http://blog.csdn.net/loveyou128144/article/details/4749576 @Is-A,Has-A,Use-A则是用来描述类与类之间关系的.简单的 ...

  4. 对象的继承关系在数据库中的实现方式和PowerDesigner设计

    原文:对象的继承关系在数据库中的实现方式和PowerDesigner设计 在面向对象的编程中,使用对象的继承是一个非常普遍的做法,但是在关系数据库管理系统RDBMS中,使用的是外键表示实体(表)之间的 ...

  5. Java实验项目三——编程实现Person类,学生类的设计及其继承关系

    Program: 编程实现Person类,学生类的设计及其继承关系 代码如下: 定义抽象类Person 1 /* 2 * Description:建立抽象类 3 * 4 * Written By:Ca ...

  6. JavaScript面向对象编程(9)高速构建继承关系之整合原型链

    前面我们铺垫了非常多细节.是为了让大家更加明晰prototype的使用细节: 如今能够将前面的知识整合起来,写一个函数用于高速构建基于原型链的继承关系了: function extend(Child, ...

  7. Java面向对象 继承(下)

     Java面向对象   继承(下) 知识概要:               (1)抽象类 1.1 抽象类概述                            1.2 抽象类的特点       ...

  8. day25 面向对象继承,多态,

    这两天所学的都是面向对象,后面还有几天也是它,面向对象主要有三个大的模块,封装,继承,多态,(组合),昨天主要讲了面向对象的命名空间,还有组合的用法,今天是讲的继承还有继承里面所包括的钻石继承,以及多 ...

  9. 游戏编程之Unity常用脚本类的继承关系

    前言学习Unity开发引擎的初学者会接触大量的脚本类,而这些类之间的关系往往容易被忽略.本文对Unity引擎开发中的一些常用类及其关系进行了简单的归纳总结. 博文首发地址:http://tieba.b ...

随机推荐

  1. Java面向对象6 (AA - AE)

    整理音乐(SDUT 2053) import java.util.*; public class Main { public static void main(String[] args) { Sca ...

  2. MySQL 聚集拼接

    GROUP_CONCAT()函数 示例: 假设现在有这样一个表结构: 其中`student`.`school_id`是逻辑外键 想要检索出所有学校,其中学校下的学生名需要拼接在一起,作为结果集的字段 ...

  3. 在 Arch 上Yaourt 使用这些替代品

    1. aurman aurman 是最好的 AUR 助手之一,也能胜任 Yaourt 替代品的地位.它有非常类似于 pacman 的语法,可以支持所有的 pacman 操作.你可以搜索 AUR.解决包 ...

  4. [CMS漏洞]EmpireCMS_V7.5的一次审计【转载】

    i春秋作家:Qclover 原文来自:EmpireCMS_V7.5的一次审计 0x01 概述 最近在做审计和WAF规则的编写,在CNVD和CNNVD等漏洞平台寻找各类CMS漏洞研究编写规则时顺便抽空对 ...

  5. SSM框架的配置Spring+Springmvc +Mybatis

    ssm框架是由spring mvc +spring+mybatis组成 快速阅读 通过spring的配置文件spring.xml,在servlet中指定spring mvc的配置文件spring-mv ...

  6. DCL:管理用户

    1. 管理用户 (1) 查询用户 MySQL把用户的数据存放在 "mysql" 数据库的 "user" 表中. SELECT * FROM user; (2) ...

  7. java面向对象-进度2

    1.面向对象的五个基本原则 三个基本元素: 1. 封装: 封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面.面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的对象, ...

  8. try 和 catch 的用法

    try块是什么? 一个try块就是程序尝试去执行一段代码,try块后面会有几个异常处理块,如果try块中发生了异常,程序执行流就会进入相应的异常处理块中. 以下程序会帮助理解这个概念 #include ...

  9. [RK3399] Jack server installation not found

    CPU:RK3399 系统:Android 7.1 服务器上第一次编译 Android 7.1 的代码,提示 Jack server 没有安装. Jack server installation no ...

  10. rpm包和deb分别是什么?

    一.RMP 是 LINUX 下的一种软件的可执行程序,你只要安装它就可以了.这种软件安装包通常是一个RPM包(Redhat Linux Packet Manager,就是Redhat的包管理器),后缀 ...