首页 > 解决方案 > 不实现所有虚方法的多态性

问题描述

使用多态性时,在所有派生类中实现虚拟方法是否被认为是一种不好的编程习惯?

假设我有一个 Enemy 类和几个派生类。所有的敌人类型都有一个 Attack() 方法,但只有一部分能够跳跃。在这种情况下,除了兽人之外的所有敌人都有跳跃技能。

using System;

namespace Polymorphism
{
    class Program
    {
        static void Main()
        {
            Enemy enemy1 = new Spider();
            Enemy enemy2 = new Goblin();
            Enemy enemy3 = new Orc();
        }
    }

    abstract class Enemy
    {   
        //I'm making this abstract because all derived classes will implement this anyway
        public abstract void Attack();

        //This is virtual because not all derived enemy types can jump
        public virtual void Jump()
        {
            //nothing here
        }
    }
   

    class Spider : Enemy
    {
        public override void Attack()
        {
            Console.WriteLine("Bite attack!");
        }

        public override void Jump()
        {
            Console.WriteLine("Spider jump animation");
        }
    }

    class Goblin : Enemy
    {
        public override void Attack()
        {
            Console.WriteLine("Arrow attack!");
        }

        public override void Jump()
        {
            Console.WriteLine("Goblin jump animation");
        }
    }

    class Orc : Enemy
    {
        public override void Attack()
        {
            Console.WriteLine("Axe attack!");
        }

        //no jump method here! 
    }
}

即使兽人不能跳跃,基类的 Jump() 方法仍然可以调用,即使在兽人的情况下它完全没有任何作用。我发现 Intellisense 仍然“推荐”跳转按钮有点误导,对我来说这似乎也不是一个好的编程习惯。这是一个不好的方法吗?如果我真的想能够,比如说,把我所有的敌人对象放在同一个数组中,还有什么选择?据我所知,您不能真正访问派生类中的非覆盖方法,并且强制转换听起来很麻烦。

标签: c#polymorphismoverridingvirtual

解决方案


您可以访问除私有之外的基类中的任何方法。尝试这个

abstract class BaseEnemy
{
    //I'm making this abstract because all derived classes will implement this anyway
    public abstract void Attack();
    
}

abstract class AdvancedEnemy:BaseEnemy
{
    
    //This is virtual because not all derived enemy types can jump
    public virtual void Jump()
    {

    }

}


class Spider : AdvancedEnemy
{
    public override void Attack()
    {
        Console.WriteLine("Bite attack!");
    }

    public override void Jump()
    {
        Console.WriteLine("Spider jump animation");
    }
}

class Goblin :  AdvancedEnemy
{
    public override void Attack()
    {
        Console.WriteLine("Arrow attack!");
    }

    public override void Jump()
    {
        Console.WriteLine("Goblin jump animation");
    }
}

class Orc : BaseEnemy
{
    public override void Attack()
    {
        Console.WriteLine("Axe attack!");
    }

    

    //no jump method here! 
}

推荐阅读