为什么我们使用set方法

发布于 2024-10-02 05:04:21 字数 641 浏览 4 评论 0原文

可能的重复:
java 中 setter 和 getters 的意义是什么?< /a>

我有一个关于 set 方法的问题。如果可能,请提供一些详细信息。 我想知道为什么我们要使用带有类名的 set 方法。

public class Mymainclass {

Private class ClassC {

Private Mysecondclass sec = null;

public void setMymainclass(Mysecondclass second){

        Mymainclass.sec= second;
     }
   }
}

它只是设置 sec 变量值吗?如果是,为什么类名带有集合?

Possible Duplicate:
What is the point of setters and getters in java?

I have a question about set method. Please 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你怎么这么可爱啊 2024-10-09 05:04:21

您似乎将类中的“类构造函数”与“setter 方法”混淆了。

  • 构造函数的主要原因是初始化类变量
  • setter 方法的主要原因是访问类内部的私有变量

因此,在您的情况下,方法的名称应该是“setSec”而不是 setMainClass。
也就是说,如果您想在初始化类后修改私有变量“sec”,那么您可以选择使用 setter 方法。

另一方面,您也可以不使用 setter 方法,而只在首次创建类时初始化 sec 变量。为此,您必须创建一个构造函数。
此类的构造函数将如下所示:

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

通过这种方式,一旦创建 Mymainclass 的新实例,您就可以传递 Mysecondclass 对象。

还要尝试确保在标记类时使类名称中的每个单词的第一个字母大写,如下所示:MySecondClass 和 MyMainClass!

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!!

你不是我要的菜∠ 2024-10-09 05:04:21

首先,如果您遵循良好的实践,您的方法应该称为“setSeconds”。试想一下,如果您在班级中添加了一名会议记录成员,那将会是多么令人困惑。

对 setter 和 getter 进行编码有两个主要原因。

第一个是纯粹务实的。如果您想调用内省和 java beans 的魔力,那么您需要遵循这些约定。有几个库/API(例如 FreeMarker)绝对依赖于类中的 getter 和 setter 方法。

第二个更多地与良好的设计有关。假设您有一个名为 secondary 的公共成员。您班级的任何用户都可以通过编码来设置它。

instanceOfYourClass.seconds = 60;

这很好,除非您想对此值施加 42 秒的任意限制。要验证该值并将其设置为最长 42 秒,您现在需要一种方法来执行此操作。因此,您类的每个用户现在都必须将其代码更改为: -

instanceOfYourClass.setSeconds(60);

因此,通过从一开始就构建 getter 和 setter,您就可以灵活地在类中执行更多奇特的事情,同时提供一个稳定的接口您的班级用户不会要求他们在每次功能发生微小变化时更改代码。

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.

迷鸟归林 2024-10-09 05:04:21

我认为您感到困惑的部分原因是您给出的示例是糟糕的代码!您不会根据参数的类来命名 setter,而是根据要设置的对象的属性来命名它。例如,您的示例的规范“正确”版本是:

public class Mymainclass {

private Mysecondclass sec= null;

public void setSec(Mysecondclass second){

        this.sec= second;
   }
}

拥有映射到属性名称的设置器允许从 UI 到持久性以及介于两者之间的所有不同框架以抽象方式操作您的对象。例如,如果您告诉数据库层名为“sec”的属性映射到特定数据库列,它可以使用反射来查找名为“setSec”的方法并为您设置它!

当你有很多相同类型的属性、很多String、很多BigDecimal时,拥有很多名为“set”的方法的想法也会崩溃,任何。如果有两个标准只在可以时使用“set”并在必须时使用属性名称,那就真的很奇怪了。 (并且您会发现自己经常重构那些仅“设置”的方法。)

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.)

梦幻的心爱 2024-10-09 05:04:21

在面向对象编程中,一个好的做法是公开 getter 和 setter,以允许其他类与类内容交互,而不是将成员变量公开。

即使大多数时候,至少在类的第一个版本中,除了实际的赋值语句之外不会有更多的内容,这将允许您稍后添加其他行为:

  • 添加日志记录以了解何时以及如何添加已设置一个新值,
  • 在真正分配它之前对传递的值进行一些控制/转换(如果另一个类提供 null 会怎么样?)
  • 触发完成此新分配时可能需要的一些其他操作
  • ...

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
  • ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文