步步为营-02-多态

面向对象三大特征:封装.继承.多态.下面简单介绍一下多态的实现.

把不同的子类对象都当作父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化.

实现多态的方法:1 通过虚方法;2 通过抽象方法;3 通过接口

天地生人,有一人当有一人之业,现实生活中每个人都已各自的工作,我们以此为例

每一个人都需要汇报自己的工作,SayJob,但是不同的子类的工作内容不同,有的是教师,有的是工人...如何屏蔽子类之间的差异,调用一个方法实现呢?

1 通过虚方法实现多态,

1.1 定义基类People,包含一个方法-汇报自己的工作SayJob---虚方法

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

namespace Polymorphic
{
   public class People
    {
       public virtual void SayJob()
       {
             Console.WriteLine("工作是:");
       }
    }
}
People

1.2 定义子类Teacher类,重写父类方法

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

namespace Polymorphic
{
   public class Teacher:People
    {
       public override void SayJob() {
           Console.WriteLine("我是教师");
       }
    }
}
Teacher

1.3 定义子列Programmer类

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

namespace Polymorphic
{
   public class Programmer:People
    {
       public override void SayJob() {
           Console.WriteLine("我是程序员,但是不编程!");
       }
    }
}
Programmer

1.4 Main方法类

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

namespace Polymorphic
{
    class Program
    {
        static void Main(string[] args)
        {
            
            People [] pers = new People [3]{new Worker(),new Teacher(),new Programmer()};
            foreach (var item in pers)
            {
                item.SayJob();
            }
            Console.Read();
        }
    }
}
Main

 People [] pers = new People [3]{new Worker(),new Teacher(),new Programmer()};
            foreach (var item in pers)
            {
                item.SayJob();
            }

虽然只有一个item.SayJob()方法. 但父对象可以根据子对象的不同,调用各自的方法.实现多态

2:通过抽象方法 

2.1 把基类改为抽象类,并添加抽象方法

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

namespace Polymorphic
{
   public abstract class People
    {
       public virtual void SayJob()
       {
             Console.WriteLine("工作是:");
       }

       public abstract void SaySalary();
    }
}
View Code

2.2 子类实现抽象方法

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

namespace Polymorphic
{
   public class Teacher:People
    {
       public override void SayJob() {
           Console.WriteLine("我是教师");
       }

       public override void SaySalary()
       {
           Console.WriteLine("600");
       }
    }
}
Teacher
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polymorphic
{
   public class Programmer:People
    {
       public override void SayJob() {
           Console.WriteLine("我是程序员,但是不编程!");
       }

       public override void SaySalary()
       {
           Console.WriteLine("500");
       }
    }
}
Programmer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polymorphic
{
   public class Worker:People
    {
       public override void SayJob() {
           Console.WriteLine("我是工人!");
       }

       public override void SaySalary()
       {
           Console.WriteLine("300");
       }
    }
}
worker

2.3 运行效果

 3 通过接口实现编程

3.1 定义接口(职责)

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

namespace Polymorphic
{
   public interface IDuty
    {
        void SayDuty();
    }
}
IDuty

3.2 一些类实现接口--注意,先继承(父类)后实现(接口),单继承,多实现

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

namespace Polymorphic
{
   public class Worker:People,IDuty
    {
       public override void SayJob() {
           Console.WriteLine("我是工人!");
       }

       public override void SaySalary()
       {
           Console.WriteLine("300");
       }

       public new void SayDuty()
       {
           Console.WriteLine("建设祖国!");
       }
    }
}
Worker
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polymorphic
{
   public class Programmer:People,IDuty

    {
       public override void SayJob() {
           Console.WriteLine("我是程序员,但是不编程!");
       }

       public override void SaySalary()
       {
           Console.WriteLine("500");
       }

       public new void SayDuty()
       {
           Console.WriteLine("永无Bug!");
       }
    }
}
Programmer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polymorphic
{
   public class Teacher:People,IDuty
    {
       public override void SayJob() {
           Console.WriteLine("我是教师");
       }

       public override void SaySalary()
       {
           Console.WriteLine("600");
       }

       public new void SayDuty()
       {
           Console.WriteLine("教书育人!");
       }
    }
}
Teacher

3.3 Main方法

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

namespace Polymorphic
{
    class Program
    {
        static void Main(string[] args)
        {
            
            People [] pers = new People [3]{new Worker(),new Teacher(),new Programmer()};
            foreach (var item in pers)
            {
                item.SayJob();
                item.SaySalary();               
            }
            IDuty[] dutys = { new Worker(), new Teacher(), new Programmer()};
            for (int i = 0; i < dutys.Length; i++)
            {
                dutys[i].SayDuty();
            }
            Console.Read();
        }
    }
}
View Code

3.4 运行效果

ok

原文地址:https://www.cnblogs.com/YK2012/p/6698805.html