Results 1 to 4 of 4
  1. #1
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default classes and methods

    Hello! I have a rather complicated question...
    I am trying to make a very simple game. I have 2 classes. The first is called "Warrior" and the second "Monster". Each one, appart from the getters and setters, has a method called "doAttack()" which obviously removes life from the Warrior or the Monster.
    My question is: How can I make this method to "work" for every random Warrior class, a potential user may create? Because as of now I have to create a new warrior object in the doAttack() method in order to make it "know" which character's life should decrease. Let me give you an example.
    Let's say that a user creates a new warrior named: Warrior mywar = new Warrior(100,50,50) .
    I want the monster's doAttack() method to work like this: monster.doAttack(mywar)
    I hope you understand my question.

    Thanks in advance.
    Last edited by louboulos; 06-03-2012 at 02:54 PM.

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: classes and methods

    That is a bit hard to answer since I don't know how the game works. Maybe you can explain it thoroughly?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: classes and methods

    Think in terms of interfaces; an A attacks a B where A and B are interfaces; your concrete classes implement interface A and/or B.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default Re: classes and methods

    never mind i got it. I should have put doAttack(Warrior s) for example when defining the method.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. What classes and methods to use?
    By chyrl in forum Advanced Java
    Replies: 21
    Last Post: 06-20-2010, 01:45 PM
  3. using methods between classes
    By soccer_kid_6 in forum New To Java
    Replies: 2
    Last Post: 04-18-2010, 04:14 AM
  4. Classes and main methods?
    By CyberFrog in forum New To Java
    Replies: 17
    Last Post: 05-26-2009, 05:47 AM
  5. Classes and Methods help
    By border9 in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •