OOP - Signs

Discussion in 'Plugin Development' started by Ati_444, Jun 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Ati_444

    Sign.class
    Code:java,
    1. package me.ati.signsplus;
    2.  
    3. import java.lang.reflect.Array;
    4.  
    5. public class Sign {
    6.  
    7. private String signType;
    8. private String signPermission;
    9. private String signIdentifier;
    10. private Array[] lines = new Array[2];
    11.  
    12. /* Getters and Setters */
    13.  
    14. public String getSign1() {
    15. return signType;
    16. }
    17. public void setSign(String sign) {
    18. this.signType = sign;
    19. }
    20. public String getPermission() {
    21. return signPermission;
    22. }
    23. public void setPermission(String permission) {
    24. this.signPermission = permission;
    25. }
    26. public String getIdentifier() {
    27. return signIdentifier;
    28. }
    29. public void setIdentifier(String identifier) {
    30. this.signIdentifier = identifier;
    31. }
    32. public Array[] getLines() {
    33. return lines;
    34. }
    35. public void setLines(Array[] lines) {
    36. this.lines = lines;
    37. }
    38.  
    39. public Sign(String sign, String identifier, String permission) {
    40. this.signType = sign;
    41. this.signPermission = permission;
    42. this.signIdentifier = identifier;
    43. }
    44. }


    SignHeal.class
    Code:java,
    1. package me.ati.signsplus.signs;
    2.  
    3. import me.ati.signsplus.Sign;
    4.  
    5.  
    6. public class SignHeal {
    7.  
    8. private Sign heal = new Sign("Heal", "test", "test");
    9.  
    10.  
    11. }


    How do I access the getters and setter for SignHeal? I've tried extending but it says an implicit super constructor in undefined.
    Any help would be greatly appreciated.
    P.S. I've recently got into Object Oriented Programming, sorry for newb mistakes !:p
     
  2. Offline

    1Rogue

    You would need a SignHeal object:

    Code:java
    1. SignHeal sh = new SignHeal();


    Then you can define methods in the SignHeal class which you would call on sh, for example if you defined a method named "getSign()":

    Code:java
    1. sh.getSign();
     
  3. Offline

    TheHandfish

    ^ Oh, is that what he meant?

    I was kind of confused.
     
  4. Offline

    Ati_444

    Does SignHeal need to extend Sign?
     
  5. Offline

    ZodiacTheories

    Ati_444

    Yes, then make a Constructor.
     
  6. Offline

    Ati_444

    When i extend Sign, it says I need a implicit constructor. I don't know how to create it.
     
  7. Offline

    Wizehh

    Why are you using the Array class? It'd be easier just to do this:
    Code:
    String[] lines = new String[2]
    But if the former works for you, then I guess that's okay too.
    Anyway: yes your SignHeal class should extend your Sign class. Just create a parameterless constructor in your Sign class to fix the error.
    Edit:
    Code:
    public SignHeal("", "", "") {
    Put that in your SignHeal class, and fill in the blanks.

    Oh, wait, sorry: that's the wrong syntax.
    Use this:
    Code:
    public SignHeal() {
        super(..); //etc  
    }
    
     
  8. Offline

    Rocoty

    Ati_444 If SignHeal is supposed to be a kind of Sign, yes.

    EDIT: NVM. Got double ninjaed.

    Ati_444 Basically, you need to create a constructor in your SignHeal class, that calls super(/*Something here*/). This assuming you want SignHeal to be a kind of Sign.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    Ati_444

    This is what I got for the SignHeal class :
    Code:java
    1. package me.ati.signsplus.signs;
    2.  
    3. import me.ati.signsplus.Sign;
    4.  
    5.  
    6. public class SignHeal extends Sign {
    7.  
    8. public SignHeal(String signType, String identifier, String permission) {
    9. super(signType, identifier, permission);
    10. }
    11.  
    12. private Sign sign = new Sign("Heal", "test", "test");
    13. }
    14.  


    The thing is I don't know why/how the super constructor works. Can anybody explain this?
     
  10. Offline

    Codex Arcanum

    Ati_444
    In a nutshell, what you are doing is calling the constructor with the arguments (String, String, String) in the superclass (Sign) when the subclass (SignHeal) is constructed. For more details, here is a link.
     
  11. Offline

    Zupsub

    Well, that's all very confusing....


    You have a class SignHeal, which extends Sign, but doesn't extrude it... why?

    In your SignHeal class, you have an Sign object, so basically a Sign (a specific on, a HealSign) contains a Sign (a 'normal one')... whut?


    Don't want to be rude, but it looks like you don't know Java basics, or OOP basics.

    You probably just wanted to do
    heal.setLines(...) or heal.getLines() / heal.(...)


    Please tell us first -before you're trying this (for me) stupid construct- what your aim is. Then we can tell you how (not what) you should do it.
     
  12. Offline

    Ati_444

    Zupsub
    I have the Sign Class:
    Code:java
    1. package me.ati.signsplus;
    2.  
    3. import java.lang.reflect.Array;
    4.  
    5. public class Sign {
    6.  
    7. private String signType;
    8. private String signPermission;
    9. private String signIdentifier; //E.G. [HEAL] top line of sign
    10. private Array[] lines = new Array[2];
    11.  
    12. /* Getters and Setters */
    13.  
    14. public String getSign1() {
    15. return signType;
    16. }
    17. public void setSign(String sign) {
    18. this.signType = sign;
    19. }
    20. public String getPermission() {
    21. return signPermission;
    22. }
    23. public void setPermission(String permission) {
    24. this.signPermission = permission;
    25. }
    26. public String getIdentifier() {
    27. return signIdentifier;
    28. }
    29. public void setIdentifier(String identifier) {
    30. this.signIdentifier = identifier;
    31. }
    32. public Array[] getLines() {
    33. return lines;
    34. }
    35. public void setLines(Array[] lines) {
    36. this.lines = lines;
    37. }
    38.  
    39. public Sign(String signType, String identifier, String permission) {
    40. this.signType = signType;
    41. this.signPermission = permission;
    42. this.signIdentifier = identifier;
    43. }
    44. }
    45.  

    From this I want to create signs such as heal, feed etc. I know this may not be the best way to but i'm doing it to practice.
    If someone could create a class for a heal sign, so I can see what you did and learn from it, it would be much appreciated.
    The problem I have is when I create a sign object in another class, I cant use the getter and setters.

    Thanks

    P.S. The getters work in main(String[] args) but no where else.
     
  13. Offline

    Zupsub

    Sorry, I still don't get what a 'HealSign' is.

    Ask yourself: Is HealSign just a normal sign, which only uses the methods of Sign => create an object

    Is HealSign a normal Sign, but adds more functions, then create a class for this, that extend Sign.

    So what's wrong with
    Sign heal = new Sign("bla", "blu", "blub");
    heal.setSign("awesome"), // or whatever you want
     
  14. Offline

    Ati_444

    Zupsub
    Have you got skype? It will be faster to communicate on there
     
  15. Offline

    AoH_Ruthless

    Ati_444
    You really aren't inputting effort into this. Firstly, you tend to ignore key points that developers like Zupsub make. You also clearly make no attempt to search for your mistakes yourself. You began this thread quoting your eclipse error; has it occurred to you to use google? You could also look up what 'super' keyword means in Java. You also want people to just give you the HealSign class. Lastly, while not really a big point, skype is a horrible place to ask help for development questions:
    1. Sharing code becomes harder.
    2. You don't have access to the huge Bukkit community of developers who probably know what they are talking about.
    Back to your questions:
    I have no idea what you are trying to do with that whole class. If you want to make a "HealSign", that heals players, this is what I would do:

    1. Make a base class; call it SignBase or something... doesn't really matter.
    2. Make a constructor requiring 4 string parameters (or string array, which is your approach). I would do 4 strings personally because it's easier to see what you are doing in my opinion. Note that these strings would be better as global variables that are initialized in aforementioned constructor.
    3. Make a getter or setter value for each string (it would definitely be shorter if your constructor parameter was an array)
    4. Now to make your HealSign class. Have it extend your SignBase and implement Listener.
    5. You'll want to have a constructor with 4 parameters, which is what your heal sign lines should be. Invoke the constructor of SignBase class with super. ... etc. Again, these 4 parameters (strings) should just serve as a super. You don't need to initialize them, because when you invoke super, you are initializing them in SignBase.java
    6. Register events. We are implementing Listener because you could Listen for a PlayerInteractEvent when a player clicks the sign to actually heal them to keep everything in one class .. just what I would do.
    You could do a whole lot more .. I am just giving you basics in a very quick way that you might not understand very much, but I hope it helped a bit. You should really look into what extending a class really means.
     
Thread Status:
Not open for further replies.

Share This Page