C# 虚方法多态、抽象类多态、接口

2023-05-16

C# 虚方法多态、抽象类多态
虚方法:希望重新父类中的某个方法时,使用虚方法
抽象类:有多个规定的处理方式,但实际实现的方式不同,使用抽象类

抽象类就是为了设立规范,为了互相统一和方便。

在一个类型下,某种方法,有无数种变化。
例如:不同种类的鸭子(class)有不同的叫法(方法),使用父类设计一个鸭子类,在子类中重写它的叫法。

在无数种类型下,固定几种方式,方式实现的具体都不一样。
例如:出现多种形状的物体,求矩形和面积,使用一个抽象类,固定矩形和面积两种方式,让其他类去具体写这两种方式。

虚方法多态

using System.ComponentModel;
using System.Xml.Linq;

namespace ConsoleApp1
{


    internal class Class1
    {
        static void Main(string[] args)
        {
            // 鸭子叫:真的 嘎嘎、木头 吱吱、 橡皮 叽叽、
            // 使用多态的虚方法,实现多种变化 
            // 父类关键字:virtual、子类关键字:override、
            ReaIDuck rd = new ReaIDuck();
            MuDuck md = new MuDuck();
            XPDuck xd = new XPDuck();

            ReaIDuck[] ducks = {rd, md, xd};

            for (int i = 0; i < ducks.Length; i++)
            {
                ducks[i].Bark();
            }

        }

    }
    // 父类 
    public class ReaIDuck
    {
        public virtual void Bark()
        {
            Console.WriteLine("真的鸭子 嘎嘎叫");
        }
    }
    // 子类
    public class MuDuck: ReaIDuck
    {
        public override void Bark()
        {
            Console.WriteLine("木头鸭子 吱吱叫");
        }
    }
    // 子类
    public class XPDuck: ReaIDuck
    {
        public override void Bark()
        {
            Console.WriteLine("橡皮鸭子 叽叽叫");
        }
    }
}

抽象类多态

using System.ComponentModel;
using System.Xml.Linq;

namespace ConsoleApp1
{


    internal class Class1
    {
        static void Main(string[] args)
        {
            // 使用多态:求矩形的面积与周长、求圆的面积和周长、
            // 此时,应该使用抽象类方法,将面积与周长的方法做一个抽象,因为具体的实现方法不一样。

            // 实例化
            // 圆的面积与周长
            Shape shape = new Circle(5);

            double area = shape.GetArea();
            double perimeter = shape.GetPerimeter();
            Console.WriteLine("圆 面积:{0:0.00},周长:{1:0.00}", area, perimeter);

            // 矩形的面积与周长
            Shape shape1 = new Square(6, 7);
            double area1 = shape.GetArea();
            double perimeter1 = shape.GetPerimeter();
            Console.WriteLine("矩形 面积:{0:0.00},周长:{1:0.00}", area1, perimeter1);

        }
    }
    // 抽象类 关键字:abstract
    public abstract class Shape
    {
        // 求面积
        public abstract double GetArea();
        // 求周长
        public abstract double GetPerimeter();
    }

    // Ait + Shift + F10 快捷键
    // 圆形类
    public class Circle : Shape
    {
        // 字段
        private double _r;
        // 字段保护
        public double R
        {
            get { return _r; }
            set { _r = value; }
        }
        // 构造函数
        public Circle(double r)
        {
            this.R = r;
        }
        // 求面积
        public override double GetArea()
        {
            return Math.PI * this.R * this.R;
        }
        // 求周长
        public override double GetPerimeter()
        {
            return 2 * Math.PI * R; 
        }
    }
    // 矩形类
    public class Square : Shape
    {
        // 字段 长
        private double _height;
        // 字段保护
        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }
        // 字段 宽
        private double _width;
        // 字段保护
        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }
        // 构造函数
        public Square(double height, double width)
        {
            Height = height;
            Width = width;
        }
        // 矩形 面积
        public override double GetArea()
        {
            return this.Height * this.Width;
        }
        // 矩形周长
        public override double GetPerimeter()
        {
            return (this.Height + this.Width) * 2;
        }

    }
}

接口

namespace ConsoleApp1
{
    internal class Class1
    {
        static void Main(string[] args)
        {
            // 接口可以多继承
            // 接口可以继承接口,不可继承类
            // 类可以继承1个类,多个接口

            // 我是动物运动会主办方,不知道有多少种动物,不知道他们都会什么
            // 在设计时,只能设计比赛项目,例如游泳比赛的各种行为细节,以及评价标准等。

            // 假设 有一类动物,他许多比赛都可以参加,此时可以用多继承来增加它的比赛项目

            // 真的鸭子会游泳、木头鸭子不会游泳、橡皮鸭子会游泳、

            ISwimming swim = new ReaIDuck();
            swim.Swim();

            ISwimming swim1 = new XPDuck();
            swim1.Swim();

            Console.WriteLine("");

        }
    }

    public class ReaIDuck:ISwimming
    {
        public void Swim()
        {
            Console.WriteLine("真的鸭子会直接游泳");
        }

    }
    public class MuDuck
    {
        // 木头鸭子不会游泳
        
        
    }


    public class XPDuck: ISwimming
    {
        public void Swim()
        {
            Console.WriteLine("橡皮鸭子靠橡皮游泳");
        }
    }

    // 接口关键字:interface
    // 命名规范:I开头 
    public interface ISwimming
    {
        // 接口内部 不允许写:字段、具有方法体的函数、
        void Swim();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 虚方法多态、抽象类多态、接口 的相关文章

随机推荐

  • Ubuntu安装谷歌浏览器

    Ubuntu安装谷歌浏览器 1 wget https dl google com linux direct google chrome stable current amd64 deb 然后再输入 xff1a 2 sudo dpkg i g
  • Caused by: java.lang.NumberFormatException: For input string: ““

    数据格式不对 xff0c 当数据转换时 xff0c 比如由string转为int Sting有值还好 当string的值为null或者 时 就会转换报错 所以最好在转换之前加一个 span class token selector if x
  • 抽象类和普通类的区别-Gtcxy

    另外我写的不一定是对的 xff0c 学会自己思考 xff0c 如有不对之处 xff0c 请前辈指教 抽象类和普通类主要有三点比较大的区别 xff1a 1 首先应该知道 xff0c 抽象类是不能被实例化的 xff0c 就是不能用new调出构造
  • 【闲暇时间整活】给 Windows 命令行窗口 “改头换面”

    目录 前言正文内容安装软件自定义样式自定义设置主题设置背景图片 总结 前言 也快要到开学的日子了 xff0c 是时候整理一下东西准备迎接新学期了 xff0c 整理归类进行时 看看桌面 xff0c 嗯不错 xff0c 简单干净 xff08 利
  • JS-作用域,数据结构,语句,箭头函数

    文章目录 1 作用域 xff1a 全局作用域 xff1a js预解析 变量提升作用域链 xff0c 闭包 xff1a this 指向call apply bind用法与区别 xff1a 实际应用 xff1a 实现多重继承 垃圾回收 xff1
  • Android studio无法启动

    Android Studio无法启动问题 不管是因为升级了AS xff0c 还是覆盖安装 xff0c 都有可能出现这种问题 xff0c 双击图标启动不了 xff0c 管理员身份运行也无法启动 本篇文章不能直接确定问题的原因 xff0c 但可
  • pandas.read_csv()函数读取文件时,关于“header=None”

    对于一个没有字段名标题的数据 xff0c 如data csv 1 获取数据内容 pandas read csv data csv 默认情况下 xff0c 会把数据内容的第一行默认为字段名标题 所以我们要给它加列名或者让它以为没有列索引 im
  • STM32F7--->FMC(可变存储控制器) Flexible Memory Controller

    本文为STM32F7的FMC xff0c 为个人摘取与总结笔记 xff0c 详见 STM32F7 中文参考手册 第 13 章 xff08 286 页 xff09 的相关介绍 FMC可变存储控制器 概述框图主要用途外部器件地址映射 概述 FM
  • jsp上传图片失败,总是java.lang.NullPointerException: null

    检查jsp中是否指定 name属性 检查form表单是否指定entype 文件夹拒绝访问 xff0c 检查代码逻辑
  • Linux下如何安装Java环境的详细教程

    1 下载Linux版本的JDK 下载JDK地址 xff1a https www oracle com technetwork java javase downloads jdk8 downloads 2133151 html 2 使用Xft
  • Springboot整合RabbitMQ实现广播模型

    生产者代码 package com example newrabbitmq span class token punctuation span span class token function import span org junit
  • opencv 视频自动截图-小工具

    采用 xff1a python多进程 实测 cpu i7 9700 92秒 13456张 xff0c 平均约每秒146张 span class token keyword import span multiprocessing span c
  • halcon基础语法

    判断 xff1a if else 与 switch 与 and 或 or 非 not 条件 其中1个成立 xff0c 则为真 xff0c 其他情况均为假 xor dev open window span class token punctu
  • C# 字符串各种操作

    C 字符串 xff1a 字符串 转 char类型的数组字符串 批量合成字符串大小写转换字符串分割字符串替换字符串 是否包含字符串 比较字符串截取字符串 是否 以什么开头 结尾 字符串 第一个 最后一个 字符串 去除空格字符串 空与null
  • C# 里氏转换

    子类可以赋值给父类 子类可以转换为父类 protected 修饰符 是让这个字段 xff0c 在子类中也可以访问 设定访问权限 span class token keyword using span span class token nam
  • Node.js — 内置API模块

    文章目录 0 JS 与 Node js的理解 内置API模块1 导入 fs 模块 xff0c 导入文件系统模块2 导入 path 模块 xff0c 读取文件 xff0c 路径处理模块3 http xff08 创建web服务器的 xff09
  • C# 列表:ArrayList、字典:Hashtable、增删改查

    添加单个 对象或多个 删除指定单个 范围 清空 是否 包含 不包含 索引直接修改 列表 xff1a ArrayList span class token comment 集合 xff08 我称之为列表 xff09 span span cla
  • C# path类:操作路径、File类:操作文件、文件流读写

    路径操作 span class token class name span class token keyword string span span str span class token operator 61 span span cl
  • C# 列表:list 字典:dict

    列表 list 增删改查 与数组转换 span class token comment 创建泛型集合 span span class token class name List span class token punctuation lt
  • C# 虚方法多态、抽象类多态、接口

    C 虚方法多态 抽象类多态 虚方法 xff1a 希望重新父类中的某个方法时 xff0c 使用虚方法 抽象类 xff1a 有多个规定的处理方式 xff0c 但实际实现的方式不同 xff0c 使用抽象类 抽象类就是为了设立规范 xff0c 为了