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

Better way to add the dependency projects to the classpath?
Asynch component call from Java webservice
Comparing object to ArrayList in Dictionary Program
Live streaming with red5 server not working on my live site
Load ApplicationContext using spring in session in Swing Window Application
Jar created from ant not able to find external jars
Server not getting data from client - JAVA
How to reactivate TLS1.0 in JRE8u60
Hibernate criteria for a foreign key list
How to set Amazon S3 bucket presigned URL expiration time in 1 year from current date
How to move shapes in JavaFX?
Counter in Java (non GUI)
Trying to make my own version of a Paint Program — not able to have more than one tool
Changing a string to match a char array [duplicate]
OptionalInt vs Optional<Integer>
Displaying some query records in random order (Parse.com)

Categories

HOME
wso2
atom-editor
gremlin
session
react-router
malloc
fingerprint
convolution
azure-media-services
uber-api
append
medical
gorm
fancybox-3
imacros
collectd
windows-azure-storage
messages
pc
facebook-page
connection-string
predix
kryo
dcevm
autosys
virtualdub
social-media
applozic
typo3-6.2.x
ping
compatibility
bower-install
http-digest
vxworks
xenforo
greenrobot-eventbus
trim
dism
broadcastreceiver
ws-security
ensembles
eclipse-scout
quadratic-programming
reactive-cocoa-5
qcombobox
websphere-mq-fte
rainbowtable
atomicity
aurelia-validation
nstextview
brightcove
forever
slickedit
sqlbulkcopy
imanage
minimization
log4c
login-control
pagedlist
moveit
pickadate
fadeout
iis-arr
embedded-code
sankey-diagram
web-controls
clipperlib
csquery
offloading
tld
sgen
doskey
kgdb
bundles
android-2.2-froyo
gnu-smalltalk
ccss
runtime.exec
ora-00911
django-nonrel
android-screen-support
ember-app-kit
pvrtc
javaspaces
osql
pendrive
netdna-api
hamachi
django-tagging
servicehost
w3wp.exe
h.323
gallio
gamequery
castle-monorail
ncqrs
nintendo-ds
adrotator
privilege
anti-piracy
mdac
3gp
uiq3

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App