java


Casting a class to a selfmade class


I am just playing around a bit with java and tried implementing a small Dungeon game.
Ma current problem is that I have a class "Monster" and a class "Trap". Furthermore I have a class "Encounter" in which either a monster or a Trap is randomly chosen and returned to compare to the hero's stats.
I don't really have an Idea how to convert this Monster or the Trap into an encounter, so I can use it.
This is my current method for the encounter:
public Encounter generate(){
Encounter encounter = new encounter();
Trap trap1 = new Trap(random, trap1);
Monster monster1 = new Monster(random, random);
if (random > 5){
encounter = (Encounter) Monster; (?) //this is where I need help
.....
Well, I hope i explained the problem throughouly enough.. Thanks in advance for your help!
(I looked elsewhere but didnt find a slution, might not have looked hard enough though or simply not known a keywoard, please excuse that)
So you have a class Encounter which can either be a trap or a monster, the best thing would be that both trap and monster inherit from encounter (or implement an interface encounter), so they have some shared methods, then you can just cast them, but you can't cast them if they don't implement common interfaces or share a common superclass.

Related Links

Unreachable code with multiple catch statements
java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
TreeMap node is null after being assigned
Disable #EnableAutoConfiguration
Not able to move to next formatted text field using java selenium Webdriver
Calling a Jersey API from a remote location
How a single object is showing different value for the same field (Inheritance) [duplicate]
Hibernate PersistentList has data, but returns null
WSDL wsimport lower upper case issue
Run a single java package on eclipse using maven
Reverse SelectionSort to sort array
How do I test a money object method
How to prevent Linux rounding File.lastModified()? [duplicate]
How to sort strings containing multiple periods/dots in Java?
Remove an element from any position in a max heap
Android Gradle Exception: “Multiple dex files define”, duplicated classes under exploded-aar

Categories

HOME
ngrx
heroku
hp-exstream
session
platform-builder
bpmn
cplex
q
spring-cloud-stream
c#-2.0
ios-charts
tomcat6
u-sql
dynamics-crm-online
libtiff
pc
transformation
invantive-sql
code-review
contextmenu
zapier
scriptcs
mps
microsoft-r
hammerspoon
one-to-many
selectedindexchanged
info.plist
entitlements
elasticsearch-ruby
webkitspeechrecognition
lightswitch-2013
typed.js
catch-all
android-ble
plsql-psp
io-redirection
multilingual
uft-api
impersonation
streamreader
azure-ml
pdf-reactor
outlook-2013
youcompleteme
paxos
tropo
tactic
knockout-components
composite-key
sonarlint-vs
quartz-composer
rotativa
skobbler-maps
suffix-tree
ios4
mutation-observers
fouc
coveralls
android-listview
dereference
notify
rebol3
comexception
balanced-payments
meteor-velocity
mysqltuner
quantlib-swig
mqx
rdoc
frameset
word-processor
multipage
chrono
rfc1123
lang
w3wp.exe
createwindow
coda-slider
infrastructure
getresponsestream
preference
dbal
user-friendly
audio-capture
iweb
backcolor
ti-dsp
mtj
port-number

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App