C#/Unity------abstract classes and interfaces (an article to understand thoroughly...)

Abstract class/interface and the difference

SUMMARY:

Concrete class → abstract class → interface: more and more abstract, less and less internal implementation

1. Abstract class

An abstract class is a class that does not fully implement logic

Abstract classes are born for reuse and are specially used as base classes

Encapsulate deterministic, open indeterminate, defer implementation to appropriate subclasses

Members of an abstract class can be private, protected, internal

using UnityEngine;
using System;

public class OCPandObstract : MonoBehaviour
{
    private void Start()
    {
        Vehicle1 vehicle1 = new();
        vehicle1.Run();

        //无法实例化抽象类,只能实现其子类
        Vehicle2 v = new Car2();
        v.Stop();
    }
}
/// <summary>
/// 一个汽车类,一个卡车类,里面的方法都是一样的,重新写两个类就很繁琐
/// </summary>
class Car
{
    public void Run()
    {
        Console.WriteLine("Car is running ..");
    }
    public void Stop()
    {
        Console.WriteLine("Stopped...");
    }
}
class Truck
{
    public void Run()
    {
        Console.WriteLine("Car is running ..");
    }
    public void Stop()
    {
        Console.WriteLine("Stopped...");
    }
}

/// <summary>
/// 简化方法1:使用虚函数进行重写,符合开闭原则
/// </summary>
class Vehicle1
{
    public void Stop()
    {
        Console.WriteLine("Stopped...");
    }
    public virtual void Run()
    {
        Console.WriteLine("Vehicle is running...");
    }
}
class Car1:Vehicle1
{
    public override void Run()
    {
        Console.WriteLine("Car1 is running...");
    }
}
class Truck1:Vehicle1
{
    public override void Run()
    {
        Console.WriteLine("Truck1 is running...");
    }
}

/// <summary>
/// 简化方法2:,使用抽象类,抽象函数,也就是纯虚方法
/// 此时抽象类无法实例化
/// </summary>
abstract class Vehicle2
{
    public  void Stop()
    {
        Console.WriteLine("Stopped...");
    }
    /// <summary>
    /// Run需要重写,因此作为抽象函数,没有实现部分
    /// </summary>
    public abstract void Run();
}
class Car2 : Vehicle2
{
    public override void Run()
    {
        Console.WriteLine("Car2 is running...");
    }
}
class Truck2 : Vehicle2
{
    public override void Run()
    {
        Console.WriteLine("Truck2 is running...");
    }
}

The above code introduces step by step why to use abstract classes and how to use abstract functions

abstract method aka pure virtual method

2. Interface

Interfaces are "classes" that do not implement logic at all

The interface is a "pure virtual class", with only member functions and all members are public

Interfaces are born for decoupling, high cohesion, low coupling

A class that inherits from an interface must implement the functions in the interface, and cannot implement it itself

Interfaces can have properties, methods, events, indexes

Interface members are public by default without any modifiers

using UnityEngine;

public class OCPandInterface : MonoBehaviour
{
    
}
/// <summary>
/// 纯抽象类
/// </summary>
abstract class AnimalBase
{
    abstract public void Eat();
    abstract public void Sleep();
    abstract public void Walk();
}
/// <summary>
/// 抽象类,继承自纯抽象类
/// </summary>
abstract class Animal : AnimalBase
{
    public override void Eat()
    {

    }
    public override void Sleep()
    {

    }
}
/// <summary>
/// 具体类
/// </summary>
class Dog:Animal
{
    public override void Walk()
    {

    }
}
/// <summary>
/// 具体类
/// </summary>
class Duke : Animal
{
    public override void Walk()
    {

    }
}

/// <summary>
/// 引入interface,它成员变量默认为抽象且公开
/// </summary>
interface IAnimalBase
{
    void Eat();
    void Sleep();
    void Walk();
}
abstract class Animal1 : IAnimalBase
{
    /// <summary>
    /// 继承接口以后,也变成了具体方法
    /// </summary>
    public void Eat()
    {
        
    }

    public void Sleep()
    {

    }

    public abstract void Walk();
}
class Dog1 : Animal1
{
    public override void Walk()
    {

    }
}
class Duke1 : Animal1
{
    public override void Walk()
    {

    }
}

The above code describes how to introduce the interface, and how the interface is defined

Guess you like

Origin blog.csdn.net/leikang111/article/details/129328838