C#学习笔记之多态中的虚方法和抽象类使用判定

如果父类中的方法有默认的事项,并且父类并且父类要被实例化,这是考虑将父类定义成一个普通类考虑用虚方法实现多态
 当父类中的方法没有默认的实现,也不需要实例化,这是可以将该类定义为抽象类
 当父类的不知道怎么实现用抽象类关键字 abstract 抽象方法不写方法体,连{}也不写因为{}叫空方实现不叫没有方法体

 抽象类不能实例化(因为调用不到)抽象类与虚方法的区别是虚方法里有实现而抽象类中没有实现

**虚方法可以有方法体(即可以有实现),虚方法谁用谁重写,虚方法可以写成空方法体(即{})而抽象类中的抽象方法不能有方法体连空方法体也不能有,还有抽象类一旦被继承那么子类必须重写抽象类中所有抽象成员

1、用抽象类

例如:狗狗旺旺叫 猫咪喵喵叫用多态实现    分析: 我们从这两个类中找出共有的成员写一个父类,在由于父类中没有实现,也不知道怎么实现这是我们就考虑把父类写成抽象类,

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

namespace _01200402抽象类与虚方法之使用抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //狗狗旺旺叫  猫咪喵喵叫   我们不知道父类如何实现用抽象类
            Animal a1 = new Dog();
            Animal a2 = new Cat();
            a1.Buck();
            a2.Buck();
            Console.ReadKey();
        }
    }
    public abstract class Animal
    {
        public abstract void Buck();
        
    }
    public class Dog : Animal
    {
        public override void Buck()  //重写(实现)父类的抽象成员
        {
            Console.WriteLine("狗狗旺旺叫");
        }
       
    }
    public class Cat : Animal
    {
        public override void Buck()  //重写(实现)父类的抽象成员
        {
            Console.WriteLine("猫咪喵喵叫");
        }
    }
}

运行结果:


2、用虚方法

例如:  狗狗旺旺叫 狗狗有生命 猫咪喵喵叫 猫咪有生命 动物有生命 动物会叫    分析:我们发现前两个类共有的成员写成的类也有实现,所以此时我们可以考虑用虚方法;

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


namespace _01200402抽象类与虚方法之使用虚方法
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //动物有生命 动物会叫  狗狗旺旺叫 狗狗有生命 猫咪喵喵叫   知道我主类怎么实现用虚方法
            Animal a1 = new Animal();
            Animal a2 = new Dog();
            Animal a3 = new Cat();
            Animal[] an = { a1, a2, a3 };
            for (int i = 0; i < an.Length; i++)
            {
                an[i].Buck();
                an[i].Life();
            }
            Console.ReadKey();
        }
    }
    public class Animal
    {
        public virtual void Life()    //父类虚方法
        {
            Console.WriteLine("动物有生命");
        }
        public virtual void Buck()    //父类虚方法
        {
            Console.WriteLine("动物会叫");
        }


    }
    public class Dog : Animal
    {
        public override void Life()   //重写父类的虚方法
        {
            Console.WriteLine("狗狗有生命");
        }
        public override void Buck()  //重写父类的虚方法
        {
            Console.WriteLine("狗狗旺旺叫");        
        }
    }
    public class Cat : Animal
    {
        public override void Life()  //重写父类的虚方法
        {
            //base.Life();
            Console.WriteLine("猫咪有生命");
        }
        public override void Buck()  //重写父类的虚方法
        {
            //base.Buck();
            Console.WriteLine("猫咪喵喵叫");
        }
    }
}

运行结果:

3、虚方法和抽象类共同实现(虚方法谁用谁重写虚方法可以写成空方法体)

例如:狗狗旺旺叫  猫咪喵喵叫  狗狗有生命//分析不知道父类如何实现用抽象类,但只能抽象一个动物叫的抽象成员而表生命的方法不能写成抽象方法,因为子类没有全部实现父类中的抽象方法,这时把表生命的方法写成虚方法,

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

namespace _01200403抽象类与虚方法之二者结合使用
{
    class Program
    {
        static void Main(string[] args)
        {
            //狗狗旺旺叫  猫咪喵喵叫  狗狗有生命//分析不知道父类如何事项抽象类但只能抽象一个动物叫的抽象成员
             //而有生命只有狗狗有不能把它抽象成抽象方法(因为继承抽象类的类需要把所有的抽象成员全部重写(实现)),用虚方法()
            Animal a1 =new Dog();
            Animal a2 = new Cat();
            Animal[] an = { a1, a2 };
            for (int i = 0; i < an.Length; i++)
            {
                an[i].Buck();
                an[i].Life();
                
            }
            Console.ReadKey();
        }
    }
    public abstract class Animal
    {
        public abstract void Buck();//抽象方法
        public virtual void Life()//虚方法谁用谁重写
        {

        }
    }
    class Dog : Animal
    {
        public override void Buck()//重写父类抽象方法
        {
            Console.WriteLine("狗狗旺旺叫");
        }
        public override void Life()//重写父类中的虚函数
        {
            Console.WriteLine("狗狗有生命");
        }
    }
    public class Cat : Animal
    {
        public override void Buck()//重写父类的抽象方法
        {
            Console.WriteLine("猫咪喵喵叫");
        }
    }
    
}

运行结果:


猜你喜欢

转载自blog.csdn.net/boy_of_god/article/details/80215821
今日推荐