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();
    }
}
相关推荐
君顾13 小时前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
香菜TTT4 小时前
大模型上下文协议(MCP):AI 应用的“USB-C”接口技术
开发语言·人工智能·经验分享
aramae4 小时前
模拟实现strlen()函数 (C语言)
c语言·开发语言·后端
Neil20135 小时前
ajax跨域请求
c#
唐青枫5 小时前
别把 Razor 当成几段 HTML:C#.NET Razor Pages、MVC 视图与实战详解
c#·.net
jimy15 小时前
readelf 查看“派生类”的vtable符号
开发语言·c++
ctlover6 小时前
排序与查找算法详解
开发语言·python
YYYing.6 小时前
【C++进阶系列 (七)】C++ RTTI 深度剖析:从 typeid 到 dynamic_cast 的底层之旅
开发语言·c++·c/c++·rtti
nvvas7 小时前
Java 入门(四):数组——数据的集合管理
java·开发语言
Y3815326628 小时前
SERP 数据清洗实战:字段标准化、日期解析与去重键
开发语言·数据库·python·python数据库