面向对象多态案例

案例:有一台有品牌的电脑他有2个功能开机和关机,它可以插2个USB接口(实现 读,写)。 一个是U盘,一个是硬盘

类图:

class Computer
    {
        private string brand;
        public IUsb IUsb_1;
        public IUsb IUsb_2;

        public string Brand
        {
            get { return brand; }
            set { brand = value; }
        }

        public Computer(string brand)
        {
            this.brand = brand;
        }

        public void Start()
        {
            Console.WriteLine("{0}品牌的电脑开机中。。。。",Brand);
        }

        public void End()
        {
            Console.WriteLine("{0}品牌的电脑关机中。。。。", Brand);
        }

    }

Computer

 interface IUsb
    {
        void Read();
        void Write(string content);
    }

IUsb

 class UBase:Computer,IUsb
    {
        private string content;
        public string Content
        {
            get { return content; }
            set { content = value; }
        }

       public UBase(string brand) : base(brand)
        {

        }

        public void Read()
        {
            Console.WriteLine("{0}读取数据{1}",Brand,content);
        }

        public void Write(string content)
        {
            this.content += content ;
            Console.WriteLine("{0}写入数据{1}",Brand,content);
        }

    }

UBase

class UzileiA:UBase
    {
        private int content;//存储空间
        public UzileiA(string content) : base(content)
        {

        }
    }

U盘子类

class HardB:UBase
    {
        private int content;//存储空间
        public HardB(string content) : base(content)
        {

        }
    }

Hard子类

  class Program
    {
        static void Main(string[] args)
        {
            UBase uBase1 = new UzileiA("金士顿32G"); //里氏替换   子类可以赋值给父类
            //有一台电脑他有2个接口可以插USB 和 硬盘
            Computer cp = new Computer("华硕");
            cp.Start();
            cp.IUsb_1 = uBase1;
            cp.IUsb_1.Write("你好。");
            cp.IUsb_1.Read();

            cp.End();
            Console.ReadKey();
        }
    }

调用

时间: 2024-08-01 05:51:25

面向对象多态案例的相关文章

09.面向对象多态的概述及其代码体现

09.01_面向对象(多态的概述及其代码体现) A:多态(polymorphic [,p?l?'m??f?k])概述 事物存在的多种形态 B:多态前提 a:要有继承关系. b:要有方法重写. c:要有父类引用指向子类对象. C:案例演示 代码体现多态 案例: class Demo1_Polymorphic { public static void main(String[] args) { Cat c = new Cat();//猫是一只猫 c.eat(); Animal a = new Cat

面向对象多态简析

C#&C++多态问题简析 在学习C#面向对象时,对封装和继承基本上还是理解的,但是碰到多态,基本上就是记住了个父类引用指向子类对象,在很长时间内也是一直比较困惑.学习c++时,基本上算是有了一定了解.下面结合代码解释多态问题 首先是c#代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Duo

转:OC面向对象—多态

OC面向对象—多态 一.基本概念 多态在代码中的体现,即为多种形态,必须要有继承,没有继承就没有多态. 在使用多态是,会进行动态检测,以调用真实的对象方法. 多态在代码中的体现即父类指针指向子类对象. Animal类的声明 Animal类的实现 Dog类继承自Animal类 Dog类的实现 测试程序: 二.使用注意 代码分析: Dog *d=[[Animal  alloc] init];   动物是一条狗?语义正确吗? NSString *str=[Dog  new];     狗是一个字符串?

OC面向对象-多态

OC面向对象-多态 一.基本概念 1>多态在代码中的体现,即为多种形态,没有继承就没有多态 2>在使用多态时,会进行动态检测,以调用真实的对象方法 3>多态在代码中的体现即父类指针指向子类对象 Animal类的声明 #import <Foundation/Foundation> //声明一个Animal类,该类继承自NSObject @interface Animal : NSObject //在Animal类中声明一个eat的对象方法 -(void) eat; @end A

面向对象多态之接口

申明下:我也是新手,以下如有不对或不合适的地方望指出,一起交流O(∩_∩)O哈! 好了,转入正题 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Interface 7 { 8 class InterfaceTest 9 { 10 static void Main(string[] args) 11 { 12 #region 测试

多态案例

多态案例 例一 . class Test1Demo { .         public static void main(String[] args) { .             Fu f = new Zi(); . //f.method(); //编译看左边,运行看右边,左边没有method方法,报错, .             f.show();//编译看左边,运行看右边,父类有show,编译没问题,输出Zishow,如果Zi类没有show方法,则运行时发现子类没有去重写,则直接调用

面向对象编程案例02--显示地调用父类的__init__()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #面向对象编程案例02--显示地调用父类的__init__() ''' 继承是面向对象的重要特征之一,继承是2个类或多个类之间的父子关系,子类继承父类的所有共有实例变量和方法. 继承实现了代码的重用,减少代码的编写量 python在类名后用圆括号来表示继承关系,括号中的类表示父类 如果父类有init方法,则子类会显示地调用其父类的init方法,不需要重写定义.如果子类需要拓展父类的init方法,则可以父类的

黑马程序猿-面向对象-多态

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>.<a href="http://edu.csdn.net"target="blank">.Net培训</a>.期待与您交流! ---------------------- 一多态的前提:(1)要有继承关系(或

黑马程序员-面向对象-多态

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>.<a href="http://edu.csdn.net"target="blank">.Net培训</a>.期待与您交流! ---------------------- 一多态的前提:(1)要有继承关系(或