zoukankan      html  css  js  c++  java
  • 什么是多态

     一,什么是多态?

    1》多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能" 。

    2》同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,这就是多态

     二,实现多态的三种方式:抽象,接口,虚方法实现多态。

    1》虚方法实现多态

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DuoTai
    {
    
        //多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。
        //同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,这就是多态
        //抽象,接口,虚方法实现多态
    
    
        //虚方法实现多态
        class Say
        {
            public virtual void Spreak()
            {
                Console.WriteLine("父类");
            }
        }
        class Chinese : Say
        {
            public override void Spreak()
            {
                Console.WriteLine("中文");
            }
        }
        class English : Say
        {
            public override void Spreak()
            {
                Console.WriteLine("英文");
            }
        }
        class Program
        {
    
            static void Main(string[] args)
            {
                //同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果
                Say s = new Say();
                s.Spreak();
                English en = new English();
                en.Spreak();
                Chinese ch = new Chinese();
                ch.Spreak();
            }
        }
    }

     2》接口实现多态

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DuoTai2
    {
        class Program
        {
            //二,接口实现多态
            public interface Say
            {
                void Speak();
            }
    
            public class English : Say
            {
                public void Speak()
                {
                    Console.WriteLine("英文");
                }
            }
    
            public class Chinese : Say
            {
                public void Speak()
                {
                    Console.WriteLine("中文");
                }
            }
            static void Main(string[] args)
            {
                //同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果
                Say en = new English();
                en.Speak();
                Say ch = new Chinese();
                ch.Speak();
            }
        }
    }

     3》抽象实现多态

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DuoTai3
    {
        class Program
        {
            // 抽象实现多态
    
            public abstract class Say
            {
                public abstract void Speak();
            }
    
            public class English:Say
            { 
              public override void Speak()
              {
                  Console.WriteLine("英文");
              }
            }
    
            public class Chinese :Say
            {
                public override void Speak()
                {
                    Console.WriteLine("中文");
                }
            }
            static void Main(string[] args)
            {
                Say en = new English();
                en.Speak();
                Say ch = new Chinese();
                ch.Speak();
            }
        }
    }

     综上:都是,同一操作作用于不同的对象,不同的解释,产生不同的执行结果

  • 相关阅读:
    题解-FJOI2014 树的重心
    题解-CF1307G Cow and Exercise
    题解-SHOI2005 树的双中心

    【转载】SVN使用教程总结
    Fastcgi、CGI 是什么
    通过js或jq增加的代码,点击事件或其他一些事件不起作用时
    js闭包讲解
    PHP 程序员危机(转载)
    浏览器 User-Agent相关知识
  • 原文地址:https://www.cnblogs.com/May-day/p/6387469.html
Copyright © 2011-2022 走看看