Is it bad practice to make a setter return "this"?

Is it a good or bad idea to make setters in java return "this"? public Employee setName(String name){ this.name = name; return this; } This pattern can be useful because then you can chain setters like this: list.add(new Employee().setName("Jack Sparrow").setId(1).setFoo("bacon!")); instead of this: Employee e = new Employee(); e.setName("Jack Sparrow"); ...and so on... list.

让setter返回“this”是不好的做法吗?

在java中使setter返回“this”是好的还是坏主意? public Employee setName(String name){ this.name = name; return this; } 这种模式可能很有用,因为你可以像这样链接setter: list.add(new Employee().setName("Jack Sparrow").setId(1).setFoo("bacon!")); 代替这个: Employee e = new Employee(); e.setName("Jack Sparrow"); ...and so on... list.add(e); ......但这有违符合标准惯例。 我想这可能是值得的

Design Patterns: Factory vs Factory method vs Abstract Factory

I was reading design patterns from a website There I read about Factory, Factory method and Abstract factory but they are so confusing, am not clear on the definition. According to definitions Factory - Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface. Is a simplified version of Factory Method Factory

设计模式:工厂vs工厂方法与抽象工厂

我正在从网站阅读设计模式 在那里我读到了工厂,工厂方法和抽象工厂,但他们很混乱,我不清楚这个定义。 根据定义 工厂 - 创建对象而不向客户端公开实例化逻辑,并通过公共接口引用新创建的对象。 是Factory Method的简化版本 工厂方法 - 定义创建对象的接口,但让子类决定实例化哪个类,并通过通用接口引用新创建的对象。 抽象工厂 - 提供创建相关对象系列的界面,而无需明确指定其类。 我也看了关于抽象工厂vs工厂

When would you use the Builder Pattern?

What are some common, real world examples of using the Builder Pattern? What does it buy you? Why not just use a Factory Pattern? The key difference between a builder and factory IMHO, is that a builder is useful when you need to do lots of things to build an object. For example imagine a DOM. You have to create plenty of nodes and attributes to get your final object. A factory is used whe

你什么时候使用Builder模式?

什么是使用Builder模式的常见实例? 它会给你买什么? 为什么不使用工厂模式? 建设者和工厂恕我直言之间的关键区别是,当你需要做很多事情来建立一个对象时,建造者是有用的。 例如想象一个DOM。 您必须创建大量节点和属性才能获得最终对象。 工厂可以在一个方法调用中轻松创建整个对象时使用工厂。 使用构建器的一个例子是构建一个XML文档,我在构建HTML片段时使用了这个模型,例如,我可能有一个用于构建特定类型表

Transform a triangle to another triangle

Hi i am trying to create the affine transform that will allow me to transform a triangle into another one. What i have are the coordinates for the 2 triangles. Can you help me? Following the answer by Adam Rosenfield i came up with this code in case anyone is bored to solve the equation himself : public static AffineTransform createTransform(ThreePointSystem source, ThreePointSys

将三角形转换为另一个三角形

嗨,我正在尝试创建仿射变换,这将允许我将三角形变换为另一个。 我所拥有的是2个三角形的坐标。 你可以帮我吗? 在亚当罗森菲尔德的回答之后,我提出了这个代码,以防万一有人无聊自己解决这个等式: public static AffineTransform createTransform(ThreePointSystem source, ThreePointSystem dest) { double x11 = source.point1.getX(); double x12 = source.point1.getY(); double x2

Where does the @Transactional annotation belong?

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to annotate both "layers"? I think transactions belong on the Service layer. It's the one that knows about units of work and use cases. It's the right answer if you have several DAOs inject

@Transactional注释属于哪里?

你应该将@Transactional放在DAO类和/或它们的方法中,还是最好使用DAO对象来注释正在调用的Service类? 或者是否有意义的注释这两个“层”? 我认为交易属于服务层。 这是知道工作单位和用例的人。 如果您有多个DAO注入需要在单个事务中协同工作的服务,那么这是正确的答案。 总的来说,我同意其他人的观点,即交易通常从服务级别开始(取决于您需要的粒度)。 然而,与此同时,我还开始向我的DAO层(以及其他不允许启动

How to implement a JPanel that shows/hide content depending on its width?

I'm trying to implement a JPanel that displays more or less information depending on the available size. Basically, the idea is have a default content like this: That can shrinks to this when the space is reduced: My code is like this: import net.miginfocom.swing.MigLayout; import javax.swing.*; class PanelDemo extends JPanel { private final JLabel title = new JLabel(); private final

如何实现一个显示/隐藏内容取决于其宽度的JPanel?

我试图实现一个JPanel,根据可用的大小显示更多或更少的信息。 基本上,这个想法有一个像这样的默认内容: 当空间缩小时,这可以缩小到这个范围: 我的代码是这样的: import net.miginfocom.swing.MigLayout; import javax.swing.*; class PanelDemo extends JPanel { private final JLabel title = new JLabel(); private final JLabel counter1 = new JLabel("00"); private final JLabel counter1Label = new JLabel

Modifying CSS for IOS website

Possible Duplicate: Can I over-ride CSS with user-css on Mobile Safari? I am trying to modify the CSS of the website http://www.baomoi.com for my grandmother. I'd like to modify the CSS for easier readability for her and to make it more minimalistic. I have tried using a JavaScript bookmarklet: javascript:(function(){if%20(!document.getElementById('someuniqueid')){var%20objHead%20=%20d

修改IOS网站的CSS

可能重复: 我可以在Mobile Safari上使用user-css替代CSS吗? 我正在尝试为我的祖母修改网站http://www.baomoi.com的CSS。 我想修改CSS以便于阅读,并使其更简约。 我曾尝试使用JavaScript书签: javascript:(function(){if%20(!document.getElementById('someuniqueid')){var%20objHead%20=%20document.getElementsByTagName('head');%20if%20(objHead[0]){if%20(document.createElementNS%20&&%20objHead[0].tag

constructor have include a void return type?

for example class A { void A() //where A is constructor and which return type is void,it is valid { } public static void main(string arg[]) A a = new A(); } A constructor does not have a result / return type. If you add a result / return type to a "constructor" you turn it into a method whose name is the same as the class name. Then new won't be able to use it, an

构造函数有一个void返回类型?

例如 A级{ void A()//其中A是构造函数,返回类型是void,它是有效的 { } public static void main(string arg []) A a = new A(); } 构造函数没有结果/返回类型。 如果将结果/返回类型添加到“构造函数”中,则将其转换为名称与类名相同的方法。 然后new将无法使用它,并且方法中的任何this(...)或super(...)调用都将是语法错误。 对于你的例子,你不会真的得到一个错误。 这是因为Java会为A ...添加

purpose of String args[] in main method in java

This question already has an answer here: What is “String args[]”? parameter in main method Java 14 answers The main method has only one because it's a form for standardisation. Oracle does not know how many arguments a programmer will need and what types of them. For this reason, with the args[] of type String you can pass N arguments to your program. You can then parse it to any pri

java中主要方法中String args []的用途

这个问题在这里已经有了答案: 什么是“字符串参数[]”? main方法中的参数Java 14答案 主要方法只有一个,因为它是标准化的一种形式。 Oracle不知道程序员需要多少参数以及它们的类型。 出于这个原因,对于类型为String的args[] ,您可以将N参数传递给您的程序。 然后可以将它解析为Java中的任何基元类型。 以下是使用Java将参数传递给应用程序的示例: java MyApp arg1 arg2 arg3 arg4 ... argN 每个传递的值都用空

Clone a single tone object

Here is the listing below: public class Car { private static Car car = null; private void car() { } public static Car GetInstance() { if (car == null) { car = new Car(); } return car; } public static void main(String arg[]) throws CloneNotSupportedException { car = Car.GetInstance

克隆单个音色对象

以下是清单: public class Car { private static Car car = null; private void car() { } public static Car GetInstance() { if (car == null) { car = new Car(); } return car; } public static void main(String arg[]) throws CloneNotSupportedException { car = Car.GetInstance();