开发者

why we use set method [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What is the point of setters and getters in java?

I have a question about set method. Pl开发者_JAVA百科ease provide some details if possible. I want to know why do we use set method with class name.

public class Mymainclass {

Private class ClassC {

Private Mysecondclass sec = null;

public void setMymainclass(Mysecondclass second){

        Mymainclass.sec= second;
     }
   }
}

Is it just setting the sec variable value ? if yes why there is class name with set?


It seems like you are confusing a "class constructor" with a "setter method" in a class.

  • The primary reason for a constructor is to intialize the class variables
  • The primary reason for a setter method is to access private variables inside the clas

So in your case the name of the method should be "setSec" rather than setMainClass. That is if you would like to modify the private variable "sec" after you have initialized the class then you can choose to use a setter method.

On the other hand you can also not use a setter method and just have the sec variable be initialized when the class is first created. To do that you will have to create a constructor. Your constructor for this class will look like this:

 Mymainclass(Mysecondclass sec){
     this.sec = sec;
 }

This way you can pass is a Mysecondclass object as soon as you create a new instance of Mymainclass.

Also try to make sure when you label classes to make each word in the class name to have its first letter capital like this: MySecondClass and MyMainClass!!


Firstly your method should be called "setSeconds" if you follow good practice. Just think how confusing it would be if you added a minutes member to your class.

There are two main reasons for coding setters and getters.

The first is purly pragmatic. If you want to invoke the magic of introspection and java beans then you need to follow these conventions. There are several libraries/APIs like FreeMarker that absolutly depend on haveing getter and setter methods in your class.

The second has more to do with good design. Consider thet you have a public member called seconds. Any user of you class could set this by coding.

instanceOfYourClass.seconds = 60;

This is just fine except maybe you want to impose an arbitary limit of 42 seconds on this value. To validate the value and set it a max of 42 seconds you now need a method to do this. So every user of you class must now change thier code to:-

instanceOfYourClass.setSeconds(60);

So by building in getters and setters from the start you are building in both the flexibilty to do more exotic things within your class, while at the same time providing a stable interface to your class users which wont rquire them to change thier code every time there is a small change in functionality.


I think part of the source of your confusion is that the example you gave is bad code! You don't name a setter based on the class of its argument, you named it based on the property of the object that you're setting. e.g., the canonically 'correct' version of your example would be:

public class Mymainclass {

private Mysecondclass sec= null;

public void setSec(Mysecondclass second){

        this.sec= second;
   }
}

Having setters that map to property names allows all kinds of different frameworks from UI to persistence and all in between to manipulate your objects in an abstract way. If you tell, for example, your database layer that the property named 'sec' maps to a particular database column, it can use reflection to find the method named "setSec" and set it for you!

The idea of having lots of methods just named 'set' also breaks down when you have lots of properties of the same type, lots of Strings, lots of BigDecimals, whatever. It would be really wierd if there were two standards to only use 'set' when you can and use the property name when you have to. (and you'd find yourself refactoring away those 'set' only methods awfully often.)


In object oriented programming, a good practice is to expose getters and setters to allow other class to interact with a class content instead of making member variables public.

Even if most of the time, at least in the very first version of the class, there won't be much more there than the actual assignment statement, this will allow you to add other behaviors later:

  • add logging traces to know when and how a new value has been set
  • do some controls/transformations on the value that is passed before really assign it (what if the other class provided null ?)
  • trigger some other actions that could be necessary whent this new assignment is done
  • ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜