c# 第八章 多态与案例

51 接口interface

csharp 复制代码
using System.Diagnostics;

interface IFlyable
{
    void Fly();
    int MaxALtitude { get; set; }
}

class Bird :IFlyable
{
    private int maxAltitude;

    public void Fly()
    {
        Console.WriteLine("鸟在飞");
    }

    public int MaxALtitude
    {
        get { return maxAltitude; }
        set { maxAltitude = value; }
    }
}

public class Aaaa
{
    public static void Main()
    {
        IFlyable bird = new Bird();
        bird.Fly();
    }
} 

一个类可以实现多个接口

密封类 sealed与override 限制

使用sealed的类,不能被继承,可以被实例化,可以继承别的类

csharp 复制代码
using System.Diagnostics;

class BaseClass
{
    public virtual void Method()
    {
        Console.WriteLine("基类放发");
    }
}

class DerivedClass : BaseClass
{
    public sealed override void Method()
    {
        Console.WriteLine("派生类方法");
    }
} 

class FurtherDerivedClass : DerivedClass
{
    public override void Method()//错误
    {
        Console.WriteLine("进一步派生类方法");
    }
}

案例1

csharp 复制代码
using System.Diagnostics;
using System.Drawing;


namespace aaaa
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IDrawable[] drawables = new IDrawable[]
            {
                new Circle("红色",10,20,5.0),
            };
        }

      
    }
}
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

namespace aaaa
{
    internal class Circle: Shape,IDrawable,ICalculable
    {
        private double radius;

        public Circle(string color, int x,int y,double radius):base (color,x,y)
        {
            this.radius = radius;
        }

        public void Draw()
        {
            Console.WriteLine($"绘制图形:颜色={Color},半径={radius}");
        }

        public double GetArea()
        {
            return Math.PI * radius * radius;
        }

        public double GetPerimeter()
        {
            return 2 * Math.PI * radius;
        }

        public override void DisplayInfo()
        {
            base.DisplayInfo();
            Console.WriteLine($"类型:圆,半径:{radius}");
        }
    }
}
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

namespace aaaa
{
    abstract internal class Shape
    {
        public string Color { get; set; }
        public int X { get; set; }
        public int Y { get; set; }

        public Shape (string color,int x,int y)
        {
            Color = color;
            X = x;
            Y = y;
        }

        public virtual void DisplayInfo()
        {
            Console.WriteLine($"颜色:{Color},位置:({X},{Y})");
        }
    }
}
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

namespace aaaa
{
    internal interface ICalculable
    {
        double GetArea();
        double GetPerimeter();
    }
}
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

namespace aaaa
{
    internal interface IDrawable
    {
        void Draw();
    }
}
相关推荐
大草原的小灰灰4 小时前
Python面向对象编程
开发语言·python
知识分享小能手4 小时前
C学习教程,从入门到精通,C语言概述 —— 知识点详解(1)
c语言·开发语言·学习
Chen_harmony5 小时前
一、C++入门基础
开发语言·c++
学逆向的5 小时前
远程线程注入
开发语言·jvm·网络安全·win32
阡陌数智5 小时前
RAG 系统的召回退化:向量库、分块、重排全链路问题排查与优化实践
开发语言·人工智能·语言模型·自然语言处理·推荐算法
geovindu5 小时前
rust: Flyweight Pattern
开发语言·后端·设计模式·rust·享元模式·结构型模式
Escalating_xu5 小时前
【C 语言】字符函数和字符串函数:从 ctype 到 strtok、strerror 全面解析
java·c语言·开发语言
郝学胜-神的一滴6 小时前
C++ Templates 07:编译报错和调试手段与实战技巧
开发语言·c++·后端·软件工程·visual studio
害人终害己6 小时前
Redisson 连接 Redis 失败 报错解决方案
java·开发语言
jimy16 小时前
c++的静态类型和动态类型(二)
开发语言·c++