Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using Anonymous Classes

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Using Anonymous Classes

    I was wondering if this made sense. I want to have a player Ship who has a skill called "Drain Bullet". This skill fires a bullet and will drain the health of a ship that the bullet hits.
    		Skill slowBullet = new Skill(10, 1, 5, "Drain Bullet") {
    			public void affect(Ship shooter) {
    				final Ship player = shooter;
    				Bullet bullet = new Bullet(BulletType.PLAYER, shooter, 10, -1, new Skill(0, 0, 0, "Drain Bullet") {
    					public void affect(Ship target) {
    						target.takeDamage(10);
    						player.heal(10);
    					}
    				});
    				Game.addBullet(bullet);
    			}
    		};
    I will be polling for for bullet collision in the Game class, and I want it so when it calls
    bullet.affect(enemy); // enemy is the ship that gets hit by the bullet
    the Player will heal, and the enemy will take damage.
    Does this code actually make sense? Seems really confusing and I don't know if it will work.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using Anonymous Classes

    Neither do we. That is why there is this concept called "testing".
    Improving the world one idiot at a time!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using Anonymous Classes

    You're nesting an anonymous class inside an anonymous class... that looks pretty smelly to me.

    Regardless of if it works or not I would consider re-thinking the code structure because it seems that Drain Bullet is a property/skill of the shooter, and there should be some sort of use/attack method which could take a target.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    EOSD598 (August 28th, 2013)

Similar Threads

  1. question about Anonymous object and Wrapper classes?
    By sciences in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2013, 09:00 AM
  2. Help with Anonymous Inner class
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 4th, 2013, 01:01 AM
  3. Anonymous class Example : What's wrong with my code?? :(
    By JavaEnthusiast in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 03:25 PM
  4. Anonymous is here
    By anonymous in forum Member Introductions
    Replies: 2
    Last Post: March 1st, 2010, 03:10 AM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM