简体   繁体   中英

How to define child static class which extends parent abstract?

I need a parent public abstract class which will have multiple child public static classes. I need only one instance of a Weapon class, a few static classes Bow, Sword< etc which extend the parent and can exist also only in one instance (at least I need to be able to get their static fiels). How to do it properly?

public abstract  class Weapon {
    ...
}
public static class Bow extends  Weapon { 
      static String type = "Bow ";
      static  int damage = 10;
}

public static class Sword extends  Weapon { 
      static String type = "Sword";
      static int damage = 10;
}

Getting error:

Illegal modifier for the class Sword; only public, abstract & final are permitted

I removed static and it worked:

public final class   Sword extends  Weapon { 
      static String type = "Sword";
      static int hitPointsTaken = 10;
}

I can compile this System.out.println( Sword.type ); but not this System.out.println( Weapon.Sword.type ); . Does it mean that I cannot access child through parent abstract?

If you absolutely have to implement this model, you're looking at nesting:

public abstract  class Weapon {

    public static class Bow extends  Weapon { 
          static String type = "Bow ";
          static  int hitPointsTaken = 10;
    }
}

In Java, only nested classes can be static, and it doesn't mean what you think it means, then, either. What you really want to do if something should only have one instance is use a Singleton pattern: http://en.wikipedia.org/wiki/Singleton_pattern#Example

You cannot declare a public static class out of the parent class scope. Try doing like this

public abstract class Weapon {
    public abstract void usage();

    public static class Sword extends Test{

        @Override
        public void usage() {
            // TODO Auto-generated method stub

        }

    }
}

One instance you can achieve it by making the constructor of Weapon class as private and have a getInstance() method to create a single instance of the class. This is known as Singleton pattern, but please make it singleton if your design really needs else don't go for that because it will make your code tough to write tests and also for future changes.

If an Object can only exist in one instance (ie I only have one sword in my werehouse), it doesn't mean it should be a static Class . It should still be an object. What if by some cosmic event your best friend the blacksmith would give you a free Sword tommorow? What would you do then?

If you want to access a single instance of the Bow class statically, you can make the Bow class have a non-public constructor and initialize a public static instance of it. ( public static Bow THE_BOW = new Bow(...); ) This should take care of your static and inheritance desires. If this doesn't make sense let me know and I'll provide a code example.

I think you are miss understanding the meaning of the static keyword. I am new to Java so I had to go look it up myself. (that is one of the reasons I surf this site so much... I learn a lot too). I found this: Secrets of Static in Java with Example site in my bookmarks that explains the static keyword in detail. Good luck with your project.

Isn't this an enum pattern?

public enum Weapon {
  Bow(10),
  Sword(10);

  private int damage;

  Weapon (int damage) {
    this.damage = damage;
  }

  public int getDamage () {
    return damage;
  }
}

Added ... or more dramatically

public enum Weapon {
  Bow(10) {
    @Override
    public void strike(Warrior warrior) {
      // Inflict the normal damage.
      super.strike(warrior);
      // Sometimes also sicken.
      if ( Math.random() > .95 ) {
        warrior.sicken(Illness.ArrowInTheKnee);
      }
    }
  },
  Sword(10) {
    @Override
    public void strike(Warrior warrior) {
      // Inflict the normal damage.
      super.strike(warrior);
      // Sometimes also sicken.
      if ( Math.random() > .75 ) {
        warrior.sicken(Illness.StabInTheBack);
      }
    }
  };

  // Normal damage inflicted.
  private int damage;

  Weapon(int damage) {
    this.damage = damage;
  }

  public int getDamage() {
    return damage;
  }

  public void strike(Warrior warrior) {
    warrior.inflict(damage);
  }

  // Placeholder
  public interface Warrior {
    public void inflict(int damage);
    public void terrorise(int fear);
    public void sicken (Illness illness);
  }

  enum Illness {
    StabInTheBack,
    ArrowInTheKnee;
  }
}

The best way to do it is use Singleton pattern for your 'class Weapon'.

Make your constructor private and use getInstance() method to retrieve your Weapon instance. After this create your subclasses for Bow and Sword.

you can use subclass as static class like below

public class Bow extends  Weapon {

  private static Bow object = null;

  public Bow(){
    object = this;
  }

  public static getObjectAsStatic(){
    return object;
  }

}

Now you can call any class member with "Bow.getObjectAsStatic()" like Bow.getObjectAsStatic().classMembers

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM