多态vob

基本概念

让继承同一父类的子类们在执行相同方法时有不同的表现


运行时的多态:vob、抽象函数、接口

v: virtual 虚函数

o: override 重写

b: base 父类

class GameObject
{
    public string name;
    public GameObject(string name)
    {
        this.name = name;
    }
    //虚函数可以被子类重写
    public virtual void Atk()
    {
        Console.WriteLine("游戏对象攻击");
    }
}
class Player:GameObject
{
    public Player (string name):base(name)
    {

    }
    //重写虚函数
    public override void Atk()
    {
        //保留父类的行为,可以保留也可以不保留
        base.Atk();
        //子类行为的唯一性
        Console.WriteLine("玩家对象攻击");
    }
}
posted @ 2024-12-26 11:33  cannedmint  阅读(19)  评论(0)    收藏  举报