C# & Unity 面向对象补全计划 之 初识继承方法与多态

本文仅作学习笔记与交流,不作任何商业用途,作者能力有限,如有不足还请斧正

本系列旨在通过补全学习之后,给出任意类图都能实现并做到逻辑上严丝合缝

1.继承方法

C# & Unity 面向对象补全计划 之 继承(字段与属性)-CSDN博客

本文讲述继承方法与多态性,是继承的重要伴随知识

我想实现如下内如:

有一个Computer类和一个Mycomputer类作为子类

父类提供推荐配置,而子类提出我自己的配置,并打印

1.1构造函数继承

子类构造函数会默认先调用父类构造函数

举个栗子:

Computer类:

cs 复制代码
class Computer {
    // 公共字段
    public string cpu ;
    // 私有字段
    private string gpu ;
    // 公共属性
    public string gpu_name {
        get => gpu;
        set => gpu = value;
    }
    // 公共构造函数
    public Computer() {
        cpu = "7500f及以上";
        gpu_name = "Rx580及以上";
        Console.WriteLine("{0},{1}",cpu,gpu_name);
    }
    // 公共自定义方法
    public virtual void Configuration() {
       
    }
}

当我执行Computer类的对象时,会输出如下:


子类继承:

也可以用base关键词进行父类构造函数调用,用的不多故不做演示

cs 复制代码
class MyComputer : Computer {

    public MyComputer() {

        cpu = "5600";
        gpu_name = "3060";
        Console.WriteLine("{0},{1}", cpu, gpu_name);
    }
}

子类对象:
输出:

如果父类构造函数是私有的话,就无法使用子类构造函数了

Q:我可以在调用子类构造函数的时候,不调用父类构造函数吗?

A:不行,因为这是语言设计的一部分,用于确保父类的成员被正确初始化

1.2方法继承

我将Computer类添加了一个自定义方法,并将输出语句放到方法里

cs 复制代码
class Computer {
    // 公共字段
    public string cpu ;
    // 私有字段
    private string gpu ;
    // 公共属性
    public string gpu_name {
        get => gpu;
        set => gpu = value;
    }
    // 公共构造函数
    public Computer() {
        cpu = "7500f及以上";
        gpu_name = "Rx580及以上";
        
    }
    // 公共自定义方法
    public  void Configuration() {
        Console.WriteLine("{0},{1}", cpu, gpu_name);
    }
}

如果子类继承以后,写一个相同名字的方法,会输出什么?

cs 复制代码
MyComputer mycomputer = new MyComputer();
mycomputer.Configuration();
class Computer {
    // 公共字段
    public string cpu ;
    // 私有字段
    private string gpu ;
    // 公共属性
    public string gpu_name {
        get => gpu;
        set => gpu = value;
    }
    // 公共构造函数
    public Computer() {
        cpu = "7500f及以上";
        gpu_name = "Rx580及以上";
        
    }
    // 公共自定义方法
    public  void Configuration() {
        Console.WriteLine("{0},{1}", cpu, gpu_name);
    }
}

class MyComputer : Computer {

    public MyComputer() {

        cpu = "5600";
        gpu_name = "3060";
      
    }
    public void Configuration()
        {
        Console.WriteLine("{0},{1}", cpu, gpu_name);
    }
}

不会继承,所以如果不做特殊处理的话,只会调用自己的方法

所以就用到了多态,直接跳到标题3:多态下

2.多态(上)

2.1方法重载

是指可以出现同名方法,规则如下:

cs 复制代码
public class Example
{
    public void Print(int a) { }
    public void Print(int a, int b) { }
    public void Print(double a) { }
    public void Print(int a, double b) { }
    public void Print(double a, int b) { }
}

2.2运算符重载

运算符重载,一般也用的不多先用现学即可

cs 复制代码
public static Complex operator +(Complex c1, Complex c2)
{
    return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}
cs 复制代码
public static Complex operator +(Complex c1, Complex c2)
{
    return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}

3.多态(下)

3.1虚方法(可以不重写)

在C#中,虚方法(virtual method)是指在基类中声明并可以在派生类中重写的方法

虚方法使用 virtual 关键字进行声明:

cs 复制代码
 public virtual void Configuration() {
     Console.WriteLine("推荐配置为{0},{1}", cpu, gpu_name);
 }

子类可以使用override关键字进行重写,并且添加逻辑:

cs 复制代码
   public override void Configuration()
       {      
           Console.WriteLine("我的配置为{0},{1}", cpu, gpu_name);
           Console.WriteLine("打印完毕");
   }

调用:

cs 复制代码
Computer computer = new Computer();
computer.Configuration();
MyComputer mycomputer = new MyComputer();
mycomputer.Configuration();

结果:

3.2抽象方法(强制重写)

抽象方法没有方法体,只包含在抽象类之中,只有方法签名,它们必须在派生类中实现

举个栗子:电脑这一概念的确就符合抽象的概念

cs 复制代码
abstract class Computer {
    // 公共字段
    public string cpu ;
    // 私有字段
    private string gpu ;
    // 公共属性
    public string gpu_name {
        get => gpu;
        set => gpu = value;
    }
    // 公共构造函数
    public Computer() {
        cpu = "7500f及以上";
        gpu_name = "Rx580及以上";
        
    }
    // 公共自定义方法
    public abstract void Configuration();      
}

子类继承的话,则强制进行方法重写:

cs 复制代码
 public override void Configuration()
     {      
         Console.WriteLine("我的配置为{0},{1}", cpu, gpu_name);
         Console.WriteLine("打印完毕");
 }

二者区别

相关推荐
小徐不徐说3 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang3 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
Wang's Blog3 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3273 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
Dxy12393102164 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷5 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang5 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
叼烟扛炮5 小时前
[Git 实战] 学习笔记:从生成 SSH 密钥到解决 TortoiseGit 推送报错全指南
linux·运维·笔记·git·学习
nice_lcj5206 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
蒸蒸yyyyzwd6 小时前
学习笔记 八股手撕
笔记·学习