C# 抽象类和接口详解

参考视频链接:https://www.bilibili.com/video/BV13b411b7Ht?p=27\&vd_source=10065785c7e10360d831474364e0d3e3

代码的进化与重构,从基本代码的讲解到逐步抽象成抽象类和接口。

文章目录

最初定义

最初定义两个类Car 和 Truck

csharp 复制代码
namespace InterfaceAPPLication
{
    class Car
    {
        public void Run()
        {
            Console.WriteLine("Car is Running");
        }
        public void Stop() 
        {
            Console.WriteLine("Stopped");
        }
    }

    class Truck
    {
        public void Run()
        {
            Console.WriteLine("Truck is Running");
        }
        public void Stop()
        {
            Console.WriteLine("Stopped");
        }
    }
}

利用继承改进

两个类具有重复的代码Stop()方法,不符合代码不重复出现的规则,对代码进行改进,将两个类继承自一个类 Vehicle

csharp 复制代码
namespace InterfaceAPPLication
{
    class Vehcile
    {
        public void Stop()
        {
            Console.WriteLine("Stopped");
        }
    }
    class Car:Vehcile
    {
        public void Run()
        {
            Console.WriteLine("Car is Running");
        }
    }

    class Truck
    {
        public void Run()
        {
            Console.WriteLine("Truck is Running");
        }
    }
}

对方法进一步改进

继续观察Run方法,对Run方法进行改进:

csharp 复制代码
namespace InterfaceAPPLication
{
    class Vehcile
    {
        public void Stop()
        {
            Console.WriteLine("Stopped");
        }

        public void Run(string type)
        {
            if (type == "Car")
            {
                Console.WriteLine("Car is Running");
            }else if(type == "Truck")
            {
                Console.WriteLine("Truck is Running");
            }
           
        }
    }
    class Car:Vehcile
    {
        
    }

    class Truck:Vehcile
    {
       
    }
}

利用虚函数进行改进

但是当有新的类从Vehcile派生的话,就需要多Vehicle.Run(string type)函数体进行扩充,不符合设计原则中的封闭原则。替代方式,使用virtual虚函数修饰Run方法,派生类对Run方法进行重写。

csharp 复制代码
namespace InterfaceAPPLication
{
    class Vehcile
    {
        public void Stop()
        {
            Console.WriteLine("Stopped");
        }

        public virtual void Run(string type)
        {
            Console.WriteLine("Vehcile is Running");
        }
    }
    class Car:Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Car is Running");
        }
    }

    class Truck:Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Truck is Running");
        }
    }
}

利用抽象进行改进

基类Vehicle的Run方法被派生类重新实现,所以Run方法的函数体是没有意义的,所以可以修改为abstract方法,同时 Vehicle类为抽象类;

csharp 复制代码
namespace InterfaceAPPLication
{
    abstract class Vehcile
    {
        public void Stop()
        {
            Console.WriteLine("Stopped");
        }

        public abstract void Run(string type);
    }
    class Car:Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Car is Running");
        }
    }

    class Truck:Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Truck is Running");
        }
    }
}

Vehcile抽象类包含的方法不全是抽象方法,当一个抽象类全部都是抽象方法的时候,IVechile类就是一个全是抽象方法的类 。可以看到Vehcile :继承IVechile,其中重写了 Filled() 和 Stop()方法,没有重写Run(string type)方法,不需要声明,默认继承了,然后Run(string type)在Car和Truck类中实现。

csharp 复制代码
 abstract class IVechile
    {
        public abstract void Filled();
        public abstract void Run(string type);
        public abstract void Stop();
    }
    abstract class Vehcile : IVechile
    {
        public override void Stop()
        {
            Console.WriteLine("Stopped");
        }
        public override void Filled()
        {
            Console.WriteLine("Pay and Filled");
        }

    }
    class Car : Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Car is Running");
        }
    }

    class Truck : Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Truck is Running");
        }
    }

利用接口进行改进

IVechile类就是一个全是抽象方法的类 ,

csharp 复制代码
 abstract class IVechile
    {
        public abstract void Filled();
        public abstract void Run(string type);
        public abstract void Stop();
    }

抽象类的抽象方法需要被派生类实现,所以默认是public abstract, 改为接口后方法就不需要修饰符了:(接口的命名方式大写英文字母 I+name)

csharp 复制代码
 interface IVechile
    {
        void Filled();
        void Stop();
        void Run(string type);
    }

注意,在对接口中方法进行实现的时候,没有实现的方法,需要在抽象类中注明是待实现的抽象方法。

csharp 复制代码
 interface IVechile
    {
        void Filled();
        void Stop();
        void Run(string type);
    }
    abstract class Vehcile : IVechile
    {
        public  void Stop()
        {
            Console.WriteLine("Stopped");
        }
        public  void Filled()
        {
            Console.WriteLine("Pay and Filled");
        }
        abstract public void Run(string type);
    }

最后给出最终的完整代码:

csharp 复制代码
using System;
using System.Collections;
using System.Data;

namespace InterfaceAPPLication
{

    interface IVechile
    {
        void Filled();
        void Stop();
        void Run(string type);
    }
    abstract class Vehcile : IVechile
    {
        public  void Stop()
        {
            Console.WriteLine("Stopped");
        }
        public  void Filled()
        {
            Console.WriteLine("Pay and Filled");
        }
        abstract public void Run(string type);
    }
    class Car : Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Car is Running");
        }
    }

    class Truck : Vehcile
    {
        public override void Run(string type)
        {
            Console.WriteLine("Truck is Running");
        }
    }

    class Executer
    {
        static void Main(string[] args)
        {
            Vehcile car = new Car();
            car.Run("Car");
            car.Stop();

            Vehcile truck = new Truck();
            truck.Run("Truck");
            truck.Stop();

        }
    }
}

运行结果:

相关推荐
枫叶丹412 分钟前
【Qt开发】显示类控件(一)-> QLabel
开发语言·qt
Python私教25 分钟前
源滚滚Rust全栈班v1.02 无符号整数详解
开发语言·后端·rust
yBmZlQzJ41 分钟前
PyQt5 修改标签字体和颜色的程序
开发语言·python·qt
10001hours1 小时前
C语言第12讲
c语言·开发语言
努力的小帅1 小时前
C++_哈希
开发语言·c++·学习·算法·哈希算法·散列表
知彼解己2 小时前
深入理解 AbstractQueuedSynchronizer (AQS):Java 并发的排队管家
java·开发语言
User_芊芊君子3 小时前
【JavaSE】复习总结
java·开发语言·python
计算机毕业设计木哥3 小时前
计算机毕业设计 基于Python+Django的医疗数据分析系统
开发语言·hadoop·后端·python·spark·django·课程设计
橘颂TA3 小时前
【Qt】项目的创建 and 各个控件的使用
开发语言·qt
我有一颗五叶草3 小时前
线程间通信
java·开发语言