I'm new to programming and OOP so please forgive me for my lack of knowledge.
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. I found the JAVA equivalent which is:
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.