包/名称空间的继承[C#]

时间:2017-04-29 12:18:50

标签: c# inheritance namespaces package

我希望在保持层次结构的同时重新定义子包中的包的类。

我本想做这样的事情:

namespace RPG {

  public class Actor {}

  public class Character : Actor {}
}

public class Actor : RPG.Actor {}

//kind of a diamond problem
public class Character : Actor, RPG.Character {}

我知道这是不正确的; C#不支持多重继承,但我不知道解决这种重新定义的最佳方法。

接口无法解决我的问题。我可以使用合成来做类似的事情:

public class Character : Actor {

    private RPG.Character basis;

    // and do this for each attribute and method
    public int atk { get { return basis.atk } set { basis.atk = value; } }
}

但看起来并不好。而且,我用这种方式遇到了另一个问题

namespace RPG {

    public class Actor {

        public List<Stat> stats { get; set; }
    }

    public class Stat {}
}

public class Actor {

    private RPG.Actor basis;

    //I can't do that because Stat does not inherit from RPG.Stat
    public override List<RPG.Stat> stats { get; set; }
}

public class Stat {

    private RPG.Stat basis;
}

解决问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

使用composition代替继承。

很难直接绘制一个例子,因为你没有为你的RPG演员/角色提供任何动作,但概念是你在你的类的构造函数中注入行为。

在这种情况下,“移动”就是行为:

class Fighter
{
    public int Health = 100;
    public string Name;

    private Move _move;

    public string MoveName
    {
        get
        {
            return _move.Name;
        }
    }
    public int MoveDamage
    {
        get
        {
            return _move.Damage;
        }
    }

    public Fighter(string name, Move defaultMove)
    {
        Name = name;

        _move = defaultMove;
    }

    public void Attack(Fighter defendant)
    {
        if (_move != null)
            _move.Attack(defendant);
    }

    public void SetMove(Move move)
    {
        if (move != null)
            _move = move;
    }
}

abstract class Move
{
    public int Damage { get; set; }
    public string Name { get; set; }

    protected Move(int damage,string name)
    {
        Damage = damage;
        Name = name;
    }

    public void Attack(Fighter defendant)
    {
        defendant.Health -= Damage;
    }
}

class PunchMove:Move
{
    public PunchMove() : base(5, "Punch")
    {
    }
}

class KickMove:Move
{
    public KickMove() : base(7, "Kick")
    {
    }
}

答案 1 :(得分:0)

试试这个。

var passengers = [ { name: 'Jane Doloop', paid: true, ticket: 'coach', drinks: [ticketDrinks(this.ticket, 0), ticketDrinks(this.ticket, 1)] },
                   { name: 'Dr.Evel', paid: true, ticket: 'firstclass' },
                   { name: 'Sue Property', paid: false, ticket: 'firstclass' },
                   { name: 'John Funcall', paid: true, ticket: 'coach' } ];




function ticketDrinks(ticket, location1) {
    console.log(ticket, location1);
    if (ticket == 'firstclass') {
        drink1 = 'cola';
        drink2 = 'water';
        if (location1 == 0) {
            return drink1;
        } else {
            return drink2;
        }

    } else if (ticket == 'coach') {
        drink1 = 'wine';
        drink2 = 'champagne';
        if (location1 == 0) {
            return drink1;
        } else {
            return drink2;
        }
    }
    return null;
}