我是编程和OOP的新手,所以请原谅我缺乏知识。
作为我的Rock,Paper和Scissors游戏的一部分,我想创建一个抽象的超类(武器),它在VB.NET中有子类(Rock,Paper和Scissors)。我找到了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;
}
}
我目前有以下内容:
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
结束班
有人可以更正代码,以便比较Rock,Paper和Scissors对象。任何帮助将不胜感激。
由于
答案 0 :(得分:0)
像这样:
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.