java - How would i apply inheritance to very similar classes, like BlueScrollBar and RedScrollBar? -


i'm beginner inheritance in programming, i'm trying desgin rgb colour mixer here using inheritance design, have 3 classes, redscrollbar, greenscrollbar , bluescrollbar. tried create parent class first, called scrollbar , tried extend 3 classes. realised each class, need change variable names too, example:

class bluescrollbar {   //establish line x1 y1 , x2 y2   float bluelx1;   float bluely1;   float bluelx2;   float bluely2;    //establish box, x, y, width , height;   float bluebx;   float blueby;   float bw = 20;   float bh = 20;    boolean bluemouseover = false;   boolean blueboxlocked = false;    float blueyoffset = 0.0;    bluescrollbar(int lx1, int ly1, int lx2, int ly2) {     bluelx1 = lx1;     bluely1 = ly1;     bluelx2 = lx2;     bluely2 = ly2;      bluebx = lx1;         blueby = ly2/2;     }     void draw(){     if(mousex >=bluebx-bw/2 && mousex <=bluebx+bw/2 && mousey >=blueby-bh/2  && mousey <=blueby+bh/2 ){       fill(0);       bluemouseover = true;     } else {       fill(255);       bluemouseover = false;     }      line(bluelx1, bluely1, bluelx2, bluely2);     rect(bluebx, blueby, bw, bh);      if (blueby <= bluely1 || blueby >= bluely2) {       blueboxlocked = false;     }    }    void mousepressed(){     if(bluemouseover){       blueboxlocked = true;       blueby = mousey - blueyoffset;     } else {       blueboxlocked = false;     }   }    void mousedragged(){     if(blueboxlocked){       blueby = mousey - blueyoffset;     }   }    void mousereleased(){     blueboxlocked = false;   } } 

and redscrollbar or greenscrollbar, literally copy paste same code create new class need change variable contains word 'blue' 'red' or 'green' work. what's better way it? appreciated.

you on right track start! make class named scrollbar , have color set in constructor of class (through enum if want select set of colors). way have 1 class solve problem.

i assume know how make constructor, if don't comment on answer , show you.

edit 1:

ok, when create class in java constructor (a fancy name function used create object of class) generated if not explicitly define one. whenever call myclass test = new myclass(); chances using constructor auto generated (no arguments need passed it.)

however, going need own custom constructor this.

public class scrollbar{     color color;     //your constructor.     public scrollbar(color c){         this.color = c;     } } 

that being said i'm no sure how implementing color or framework working in take code above grain of salt.

note if create own constructor, default 1 not generated you. give error:

scrollbar test = new scrollbar(); // :( error scrollbar test = new scrollbar(red); // :) 

edit 2:

sorry mislead in way. trying keep code above general since not sure how dealing color in program. trying implement red/blue/green work method. if want 3 colors listed, pass integer constructor 0,1,2 correspond specific color want use.

ie)

public class scrollbar{      int c = 0; //default red if user gives bad value      public scrollbar(int c){         if(c >=0 && c <=2){ //check bounds             this.color = c;         }     }      public setcolor(){         if(this.color == 0){             //do red         }         else if(this.color == 1){             //do blue         }         else{             //do green         }     } } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -