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();
    }
}
相关推荐
ch_atu1 小时前
使用python下载modelscope上的模型
开发语言·python
Albart5752 小时前
Python爬虫请求头伪装后仍被反爬,基于代理池+随机延迟的绕过实战
开发语言·爬虫·python
Promise微笑2 小时前
回路电阻测试仪采购:多维度技术评估与深度避坑指南
开发语言·matlab·语音识别
從南走到北2 小时前
JAVA无人共享系统无人场馆预约篮球系统源码支持小程序+公众号+APP+H5
java·开发语言·小程序
码智社2 小时前
Maven 零基础完整教程
java·开发语言·maven
半夜修仙2 小时前
Spring集成邮箱功能
java·开发语言·spring boot·spring·rabbitmq·github·maven
F20226974862 小时前
西门子 PLC 与 C# 通信_项目实战
大数据·c#
码农学院2 小时前
电子产品企业泉州网络推广如何精准触达目标采购商
开发语言·php
qq_33776320193 小时前
国际期货资管系统开发方案|内外盘交易平台源码授权与二次开发支持
java·c语言·开发语言·c++·c#