第12节、C#多态

多态

简述:多态性是面向对象中最重要概念之一,是指对同一个对象进行形同的操作,而产生不同的结果(多样性的表现)。

1、实现多态第一种方式:虚方法和虚方法重写(virtual-override)

修饰符 Virtual 方法()
{
}

修饰符 Override 方法()
{
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class 多态
    {
        public static void Main(string[] org)
        {
            基类 pl = new 基类("父类","东西");
            Console.WriteLine("使用(virtual-override)基类可以实例化");
            pl.eat();

            Console.WriteLine();  Console.WriteLine("第二种调法");
            dog dog = new dog();
            dog.eat();
            cat cat = new cat();
            cat.eat();

            Console.WriteLine(); Console.WriteLine("第二种调法");
            基类[] jl = { new dog(), new cat() };
            foreach (基类 j in jl)
            {
                j.eat();
            }
            Console.ReadLine();
        }
    }
    public class 基类
    {
        public string name;
        public string Name { get; set; }
        public string footName;
        public string FootName { get; set; }
        public 基类(){ }
        public 基类(string name, string footName) { this.name = name; this.footName = footName;}
        public virtual void eat()
        { 
            Console.WriteLine("动物:{0},正在吃:{1}", this.name, this.footName);
        }
    }
    public class dog : 基类
    {
        public override void eat()
        {
            this.name = "小狗";
            this.footName = "骨头";
            base.eat();
        }
    }
    public class cat : 基类
    {
        public override void eat()
        {
            this.name = "小猫";
            this.footName = "咸鱼";
            base.eat();
        }
    }
}
完整代码

 多态第二种实现方式:抽象方法和抽象方法重写(abstract-override)

修饰符 abstract class  类名
{
        修饰符 abstract 返回类型 方法(); //无方法体的方法
}

修饰符 override 返回类型 方法名()
{
  //有方法体
}

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     public class 多态
10     {
11         public static void Main(string[] org)
12         {
13             Console.WriteLine();  Console.WriteLine("第二种调法");
14             dog dog = new dog();
15             dog.eat();
16             cat cat = new cat();
17             cat.eat();
18 
19             Console.WriteLine(); Console.WriteLine("第二种调法");
20             基类[] jl = { new dog(), new cat() };
21             foreach (基类 j in jl)
22             {
23                 j.eat();
24             }
25             Console.ReadLine();
26         }
27     }
28     public abstract class 基类
29     {
30         public string name;
31         public string Name { get; set; }
32         public string footName;
33         public string FootName { get; set; }
34         public 基类(){ }
35         public 基类(string name, string footName) { this.name = name; this.footName = footName;}
36         public abstract void eat();
37       
38     }
39     public class dog : 基类
40     {
41         public override void eat()
42         {
43             this.name = "小狗";
44             this.footName = "骨头";
45             Console.WriteLine("动物:{0},正在吃:{1}", this.name, this.footName);
46         }
47     }
48     public class cat : 基类
49     {
50         public override void eat()
51         {
52             this.name = "小猫";
53             this.footName = "咸鱼";
54             Console.WriteLine("动物:{0},正在吃:{1}", this.name, this.footName);
55         }
56     }
57 }
完整代码

错误注意:

  • 基类有抽象方法,则基类也要抽象类。
  • 基类的抽象方法是无方法体。
  • 抽象类不能实例化对象。 
  • 非抽象子类必须实现继承类的所有抽象方法

错误如图所示:

原文地址:https://www.cnblogs.com/liuzz/p/14472091.html