zoukankan      html  css  js  c++  java
  • C#的面向对象特性之多态

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace codeTest
    {
    
        class Program
        {
            static void Main(string[] args)
            {
                // C#的静态多态主要是通过overload来实现,主要有方法的重载和运算符的重载
                A _A1 = new A() { number = 11 };
                A _A2 = new A() { number = 21 };
                _A1.SaidHello();
                _A1.SaidHello(" World");
                Console.WriteLine((_A1 + _A2).number);
    
    
                //C# 的动态多态主要通过 override 重写来实现
                Human _humman1 = new Man();
                Human _humman2 = new Woman();
                _humman1.Run();
                _humman2.Run();
                Console.ReadLine();
    
            }
    
    
        }
    
        class A
        {
            public int number { get; set; }
    
            public static A operator +(A a1, A a2)
            {
                A sum = new A();
                sum.number = a1.number + a2.number;
                return sum;
            }
    
            public void SaidHello()
            {
                Console.WriteLine("Hello");
            }
    
            public void SaidHello(string toWho)
            {
                Console.WriteLine(string.Format("Hello {0}", toWho));
            }
        }
    
        class Human
        {
            public virtual void Run()
            {
                Console.WriteLine("human Run!");
            }
        }
    
        class Man : Human
        {
            public override void Run()
            {
                Console.WriteLine("Man Run!");
            }
        }
    
        class Woman : Human
        {
            public override void Run()
            {
                Console.WriteLine("Woman Run!");
            }
        }
    }
  • 相关阅读:
    react学习笔记一
    获取客户端时间差
    ts
    Linux学习笔记
    vuex 基本使用
    SQL入门
    ios 中倒计时计算,时间戳为NaN
    git归纳总结
    JS原型对象
    vue笔记
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4756379.html
Copyright © 2011-2022 走看看