Friday 15 July 2011

Java class inheritance and blocking superclass methods -


IM is currently making a simple plug-in program. My problem is that I plug the plug-in into all base-plugins Do not want to reach areas / methods. For example:
  Public abstract class baseline plugin {Private int x; // mysterious x public abstract void update (); // update func may require x, but it can not be preserved with the final integer getX () {return x;} // x accessor}   

and then it Until you know that there is no way to set up X,

What can I do? I want to create subclass (plug-in) is unable to change x, but read the value.

Edit: In most cases the constructor works, but for example, what will happen to me:

  public abstract class basplin {private list & lt; Int & gt; X; // mysterious x public abstract void update (); // Update Funk, which may require X, but it can not change the last preserved list & lt; Int & gt; Getx () {return x;} // X accuser public baseplugin (list & lt; int & gt; y) {x = y;}} public class plugin {public plugin (list & lt; int & gt; y) { Super (y); Y.remove (0); // Will this work? }}   

An abstract class is allowed to construct a constructor, so that you have a baseplugin ParamLired constructor for:

  Public abstract class basal plugins {Private int x; // mysterious x public baseplugin () {x = 42; } Public abstract zero updates (); // update funk, which may require x, but it can not be changed to the last integer xx {return x;} // x accessor}   

and now when one The plugin is built, X is set to 42. You do not need to make any code changes in the plug-in to use this constructor.


To answer the edited question: If x is a list and you do not want to modify the plugins then your constructor should copy it and wrap it in one of the other, otherwise, Any plugin can call getX (). Add (myObject) .

  Public baseplugin (list & lt; int & gt; y) {list & lt; Int & gt; Temp = new ArrayLight & lt; Int & gt; (); collection. Police (Temporary, Y); // This shallow copy of the list is this. X = Archive. Unmodile list (temp); }   

Now if the plugin is the constructor

  public plugin (list & lt; int & gt; y) {super (y); Y.remove (0); // Will this work? }   

There will be no effect on the list of baseplugins.

No comments:

Post a Comment