简体   繁体   English

在vb.net中使用IComparable比较岩石,纸张和剪刀游戏的武器(与JAVA中的Comparable相同)

[英]Using IComparable in vb.net for comparing weapons for Rock, Paper and Scissors game (same as Comparable in JAVA)

I'm new to programming and OOP so please forgive me for my lack of knowledge. 我是编程和OOP的新手,请原谅我的知识不足。

As part of my Rock, Paper and Scissors game I would like to create a abstract superclass (Weapon) which has subclasses (Rock, Paper and Scissors) in VB.NET. 作为我的Rock,Paper和Scissors游戏的一部分,我想创建一个抽象超类(武器),该类在VB.NET中具有子类(Rock,Paper和Scissors)。 I found the JAVA equivalent which is: 我发现等效的JAVA是:

 public abstract class Weapon implements Comparable<Weapon> {
    }



    public class Paper extends Weapon {

        @Override
        public int compareTo(Weapon o) {
            if (o instanceof Paper)
                return 0;
            else if (o instanceof Rock)
                return 1;
            return -1;
        }
    }

    public class Rock extends Weapon {

    @Override
    public int compareTo(Weapon o) {
        if (o instanceof Rock)
            return 0;
        else if (o instanceof Scissors)
            return 1;
        return -1;
    }
}

    public class Scissors extends Weapon {

    @Override
    public int compareTo(Weapon o) {
        if (o instanceof Scissors)
            return 0;
        else if (o instanceof Paper)
            return 1;
        return -1;
    }
}

I currently have the following: 我目前有以下内容:

Public MustInherit Class Weapons

Public Class Rock
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer


    End Function

End Class

Public Class Paper
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer

    End Function

End Class

Public Class Scissors
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer

    End Function

End Class

End Class 末级

Could someone kindly correct the code to so that can compare Rock, Paper and Scissors objects. 有人可以将代码更正为以便可以比较Rock,Paper和Scissors对象。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks 谢谢

Like this: 像这样:

Public Class Rock
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer
        If TypeOf Weapons Is Rock Then
            Return 0
        ElseIf TypeOf Weapons Is Scissors Then
            Return 1
        Else
            Return -1
        End If
    End Function
End Class

' etc.

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

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