Java getter和setter的使用
可能重复:Java getter和setter的使用,java,Java,可能重复: 我已经看到contsructor有一段时间了,我仍然不知道这类东西是如何工作的,我对java知之甚少,我知道它们是getter和setter,但它们在我的代码中到底执行哪个函数,因为它似乎只是指它自己,而不是执行特定的代码块 我只需要一个非常简单的解释,而不是寻求辩论 public class RSSItem { // All <item> node name String _title; String _link; String _d
我已经看到contsructor有一段时间了,我仍然不知道这类东西是如何工作的,我对java知之甚少,我知道它们是getter和setter,但它们在我的代码中到底执行哪个函数,因为它似乎只是指它自己,而不是执行特定的代码块 我只需要一个非常简单的解释,而不是寻求辩论
public class RSSItem {
// All <item> node name
String _title;
String _link;
String _description;
String _pubdate;
String _guid;
// constructor
public RSSItem(){
}
// constructor with parameters
public RSSItem(String title, String link, String description, String pubdate, String guid){
this._title = title;
this._link = link;
this._description = description;
this._pubdate = pubdate;
this._guid = guid;
}
/**
* All SET methods
* */
public void setTitle(String title){
this._title = title;
}
public void setLink(String link){
this._link = link;
}
public void setDescription(String description){
this._description = description;
}
public void setPubdate(String pubDate){
this._pubdate = pubDate;
}
public void setGuid(String guid){
this._guid = guid;
}
/**
* All GET methods
* */
public String getTitle(){
return this._title;
}
public String getLink(){
return this._link;
}
public String getDescription(){
return this._description;
}
public String getPubdate(){
return this._pubdate;
}
public String getGuid(){
return this._guid;
}
}
公共类RSSItem{
//所有节点名称
字符串标题;
字符串链接;
字符串描述;
字符串_pubdate;
字符串guid;
//建造师
公共RSSItem(){
}
//带参数的构造函数
公共RSSItem(字符串标题、字符串链接、字符串描述、字符串发布日期、字符串guid){
这个._title=标题;
这个。_link=link;
这个。_description=描述;
这个。_pubdate=pubdate;
这是.\u guid=guid;
}
/**
*所有集合方法
* */
公共无效集合标题(字符串标题){
这个._title=标题;
}
公共无效设置链接(字符串链接){
这个。_link=link;
}
公共void集合描述(字符串描述){
这个。_description=描述;
}
public void setPubdate(字符串pubDate){
这个。_pubdate=pubdate;
}
公共void setGuid(字符串guid){
这是.\u guid=guid;
}
/**
*都得到了方法
* */
公共字符串getTitle(){
返回此标题;
}
公共字符串getLink(){
返回此链接;
}
公共字符串getDescription(){
返回此项。\u说明;
}
公共字符串getPubdate(){
将此返回。_pubdate;
}
公共字符串getGuid(){
返回此文件。\u guid;
}
}
如果您以上述方式调用构造函数,那么您正在设置变量的值;因此,您创建的setter方法没有任何意义。如果要使setter方法有用,请创建一个空构造函数,然后使用这些函数 Getter
和Setter
是实现封装的面向对象原则的一种方法
-封装在其最基本的形式中就像拥有私有字段
和公共Getter和setter
-使用Getter和Setter
的最重要原因是对传递到字段的数据进行验证
例如:
在下面的示例中,没有getter和setter,因此可以将狗的重量设置为无效值-10
public class Dog{
int weight;
public static void main(String[] args){
new Dog().weight = -10;
}
}
例如:
我现在使用Setter和Getter验证字段权重
public class Dog{
int weight;
public void setWeight(int i){
if(i < 0){ // Validation
System.out.println("Invalid weight");
}
else{
this.weight = i;
}
}
public int getWeight(){
return this.weight;
}
public static void main(String[] args){
new Dog().setWeight(50);
}
}
公共级狗{
整数权重;
公共空隙净重(int i){
如果(i<0){//验证
系统输出打印项次(“无效重量”);
}
否则{
这个重量=i;
}
}
公共整数getWeight(){
返回此值。重量;
}
公共静态void main(字符串[]args){
新狗().设定重量(50);
}
}
在这种情况下,您不需要getter/setter,但使用它们总是很好的。如果您稍后注意到要在设置时检查边界。或者当你得到s.th。您可能需要执行一些数据转换
另一个简单的例子:您只想在构建元素的过程中设置一个值,所以您不需要实现setter,而是要实现getter(同样,始终将这些成员变量设置为private,否则setter/getter是毫无意义的)
因此,在大多数情况下,您可能不需要它们,但始终使用Setters/getter(imo)参数化构造函数初始化成员变量仍然是一个好主意
- 字符串标题李>
- 字符串链接李>
- 字符串描述李>
- 字符串_pubdate李>
- 字符串guid李>
getter和setter的作用是从这些成员变量读取和写入值。它们更像是一个需要遵循的标准,因为它在许多基于Java的框架作品中被广泛使用 构造函数在一个原子操作中执行对象的初始化。当您调用构造函数时,在返回时,您拥有一个完全创建的对象。将其与一个简单的无参数构造函数(后跟一系列setter)进行比较。在这种情况下,您可以轻松地不完全创建对象
您是否应该使用智能构造函数来获取参数并完全构建对象,而不是使用一系列setter?一般来说,是的。原因如下:
该操作是原子的,将为您提供一个完整、正确的对象(我假设您验证输入)
您可以提供覆盖以从字段、字符串/流等创建对象
通过使用final
字段,可以创建不可变对象。这对于确定可靠性(尤其是wrt.threads)和调试问题非常有用
通常,我认为setter/getter集是糟糕的OO设计。在最基本的情况下,它们只是暴露内部场。您可以提供验证等,但仍有可能公开实现
我宁愿使用构造函数实例化对象,然后让它使用定义良好的方法为我做一些事情,而不是通过getter提取数据并自己做。这是OO的总体目标-告诉对象为您做事情,而不是要求他们提供数据并自己做。getter和setter提供保护,防止访问您的变量。您可以对不同的用户组使用具有不同可见性级别的getter和setter,并在代码中提供更大的灵活性。这并不意味着每个变量都需要一个getter和setter。更多地考虑他们可以访问的级别
public class Sample {
public static final int DEFAULT_QTY = 8;
/**Create object with qty = 0*/
public Sample(){
setQty(DEFAULT_QTY);
//this.qty = DEFAULT_QTY;//here makes no difference
}
/**Create object with qty if qty > 0*/
public Sample(int qty){//drops confusion created by this.qty vs qty
//provides built-in range protection through the constructor's use of setter
setQty(qty);
}
/**@param qty The qty to set for this object, must be > 0*/
public void setQty(int qty){
if(qty > 0) {
this.qty = qty;
else {
informUser(QTY_MUST_BE_>_0);
//setQty(0); or getValidInput();
}
}
/*@param forSureQty The pre-verified value to set for qty*/
protected void setQtyNow(int forSureQty) {
this.qty = forSureQty;
}
/**@return Returns the qty if qty < qtyAvailable, returns -1 otherwise*/
public int getQty(){
//Avoid problems with the computer selling more than you have available
if(getQtyAvailable < this.qty) {
//informUser(QTY_AVAILABLE = x QTY_NEEDED = >x);
return -1;
}
return this.qty;
}
/*@return Returns the value of qty for this object*/
protected getQtyNow()
return this.qty;
}
private int qty;
}