简体   繁体   English

不同类型的运算符重载

[英]Operator Overloading with different types

Ok. 好。 So I'm trying to figure out how to implement an overloaded operator. 因此,我试图找出如何实现重载运算符。 I've searched some tutorials but there's something I'm missing. 我已经搜索了一些教程,但是缺少某些内容。 Maybe it's one of those 'obvious' things I'm thinking too hard about. 也许这是我在努力思考的那些“显而易见”的事情之一。 I don't know. 我不知道。 Which is why I'm here. 这就是为什么我在这里。

Here's the overloaded operator: 这是重载的运算符:

public static bool operator +(Hero h, Monster m)

    {
        if (!h.IsRunningAway)
        {
            if(h.AttackSpeed > m.AttackSpeed)
            {
                m.takesDamage(h.attackValue());
                if (m.isAlive())
                {
                    h.takesDamage(m.AttackValue);
                }
            }
            else if(h.AttackSpeed < m.AttackSpeed)
            {
                h.takesDamage(m.AttackValue);
                if (h.isAlive())
                {
                    m.takesDamage(h.attackValue());
                }
            }
            else
            {
                h.takesDamage(m.AttackValue);
                m.takesDamage(h.attackValue());
            }
        }
        else
        {
            if(h.AttackSpeed <= m.AttackSpeed)
            {
                h.takesDamage(m.AttackValue);
            }              
        }
        h.IsRunningAway = false;
        return h.isAlive();
    }

Now, I'm trying to implement this in a button click event in a form. 现在,我正在尝试在表单的按钮单击事件中实现此功能。

 private void btnAttack_Click(object sender, RoutedEventArgs e)
    {

    }

Tutorials I've seen aren't being particularly clear on how to do it. 我所见过的教程在如何做到方面还不是很清楚。 Like I said, I maybe overthinking it. 就像我说的那样,我可能想得太多。 I usually do. 我通常会。

If the question can be edited to be clearer, or anything else is needed, let me know. 如果可以将问题进行编辑以使其更清楚,或者需要其他任何条件,请告诉我。 I'm glad to edit. 我很高兴编辑。

DO NOT use operator overloading here! 请勿在此处使用运算符重载!

Operator overloading should be used when it makes sense. 有意义时应使用运算符重载。 For example, the - operator can be overloaded on DateTime . 例如, -运算符可以在DateTime上重载。 When you subtract one DateTime from another, it makes sense to get a time interval: 当您从另一个减去一个DateTime时,得到一个时间间隔是有意义的:

25/4/2016 - 24/4/2016 = 1 day

But in your case, you want to do addition on a Hero and a Monster , and you want to return a bool ! 但是在您的情况下,您想对HeroMonster进行加法Monster并且还想归还bool That makes no sense at all! 那根本没有道理! Can you tell me what does this mean? 你能告诉我这是什么意思吗?

Superman + Godzilla = True

Even if you can, don't do it. 即使可以,也不要这样做。 Because others don't know unless you need to explain your code to them. 因为其他人不知道,除非您需要向他们解释您的代码。 This is a maintenance nightmare! 这是维护的噩梦!

What you should do instead, is to write a method in the Hero class. 相反,您应该在Hero类中编写一个方法。

Just from reading your code, I think you want the hero to kill the monster when you use the + operator. 仅阅读代码即可,我想您想让英雄在使用+运算符时杀死怪物。 And return whether if the hero is alive after the fight. 并返回战斗后英雄是否还活着。 So this method will probably do the job: 因此,此方法可能会完成以下任务:

///<summary>
///blah blah blah
///</summary>
///<returns>whether the hero is alive</returns>
public bool KillMonster(Monster m) {
    if (!this.IsRunningAway)
    {
        if(this.AttackSpeed > m.AttackSpeed)
        {
            m.takesDamage(this.attackValue());
            if (m.isAlive())
            {
                this.takesDamage(m.AttackValue);
            }
        }
        else if(this.AttackSpeed < m.AttackSpeed)
        {
            this.takesDamage(m.AttackValue);
            if (this.isAlive())
            {
                m.takesDamage(this.attackValue());
            }
        }
        else
        {
            this.takesDamage(m.AttackValue);
            m.takesDamage(this.attackValue());
        }
    }
    else
    {
        if(this.AttackSpeed <= m.AttackSpeed)
        {
            this.takesDamage(m.AttackValue);
        }              
    }
    this.IsRunningAway = false;
    return this.isAlive();
}

If you want to go one step further, you don't even need to return a value! 如果您想更进一步,甚至不需要返回值! Because the client code can call isAlive() to check whether he is alive, right? 因为客户端代码可以调用isAlive()来检查他是否还活着,对吗? If isAlive() is private, make it public. 如果isAlive()是私有的,则将其公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM