+ 1
Can you explain the setter? I know it's supposed to "set", but I don't know what each piece means and why is it in the class?
2 odpowiedzi
+ 7
we have this code :
public class Vechicle{
private String color;
Vechicle(){
this.setColor("Red");
}
Vechicle(String c){
this.setColor(c);
}
public void setColor(String c){
this.color=c;
}
}
As we know every class has its own variables and its own methods.All variables and methods can be 'public' or 'private' and some others. Public means that the variable or the method (that is public) can be accessed from another class. On the other hand private means that other classes cannot access / read it . In our example , the variable of our class (color) is private so we cannot "take" it / "read" it from another class. For that reason we create the "set" method (here setColor) . Our method is public so we can access this method from another class. This method helps us to access the variable color (from the back door) because the method is public.All set and get methods are written inside their class
0
wow superb answer