jjzjj

java - 在 getter 链之后安全地调用 setter,例如 foo.getX().getY().setZ(...);

如何在getter链之后安全地调用setter,例如foo.getX().getY().setZ(...);?例如,假设我有一个嵌套的POJO,我希望能够设置一个嵌套对象的字段。Foofoo=...foo.getX().getY().setZ(...);我希望行为是这样的,如果X和Y不存在,那么它们会自动创建;否则它会重用现有对象。换句话说,我希望它的行为等同于Foofoo=...Xx=foo.getX();if(x==null){x=newX();foo.setX(x);}Yy=x.getY();if(y==null){y=newY();x.setY(y);}y.setZ(...);

java - JAXB 在解码对象时不调用 setter

我正在使用JAXB2.0JDK6将XML实例解码为POJO。为了添加一些自定义验证,我在属性的setter中插入了一个验证调用,但尽管它是私有(private)的,但似乎解码器并没有调用setter,而是直接修改了私有(private)字段。对我来说至关重要的是,每次解码调用都会针对此特定字段进行自定义验证。我该怎么办?代码:@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name="LegalParams",propOrder={"value"})publicclassLegalParams{privatestaticfinalLogger

java - 何时使用 JavaFX 属性 setter 和 getter,而不是直接使用属性

熟悉Java,但刚开始学习JavaFX,专门学习JavaFX属性。我了解基本的设计模式,如以下Oracle示例所示:packagepropertydemo;importjavafx.beans.property.DoubleProperty;importjavafx.beans.property.SimpleDoubleProperty;classBill{//DefineavariabletostorethepropertyprivateDoublePropertyamountDue=newSimpleDoubleProperty();//Defineagetterforthepro

java - 如何在 Stream 链中调用 setter

如何在不使用forEach()的情况下调用Stream链中的setter?ListnewFoos=foos.stream().filter(foo->Foo::isBlue).map(foo->foo.setTitle("Somevalue"))//IamunabletousethisbecausealsochangingthedatatypeintoObject.collect(Collectors.toList()); 最佳答案 像这样使用peek方法。它不影响流。ListnewFoos=foos.stream().filter

java - 从 Play 中生成的 getter 和 setter 中获益!框架

Play!frameworkgeneratesgettersandsetters对于模型类的每个公共(public)字段在运行时。publicclassProduct{publicStringname;publicIntegerprice;}将转化为publicclassProduct{publicStringname;publicIntegerprice;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicIntegergetPrice(){returnprice;

java - Spring Bean 属性 'xxx' 不可写或具有无效的 setter 方法

我是一个Spring新手,有一个看似简单的Spring问题。我为此工作了几个小时,但运气不佳。这是异常,后面是代码(提前致谢):Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'graphiteWriterSession'definedinfile[/home/user/resources/jmxtrans.graphite.xml]:Errorsettingpropertyvalues;nestedexceptionisorg

java - HIbernate映射异常: PropertyNotFoundException: Could not find a setter

我有两个POJO,STOCK和STOCK_DETAILS(一对多关系)。我还有一个接口(interface)IAUDITLOG(有两种方法)。我需要用两个POJO来实现这个接口(interface),并且想在这些方法中编写一些实现。但是当我用子类“STOCKDETAILS”实现IAUDITLOG接口(interface)时,它会给出异常“你应该有setter属性”股票类别:@Entity@Table(name="stock")publicclassStockimplementsjava.io.Serializable,IAuditLog{@Id@GeneratedValue(strat

java - setter 和 getter 的单元测试

老师要我们做综合单元测试。对我来说,这将是我第一次使用JUnit。我对测试集和获取方法感到困惑。你认为我应该测试它们吗?如果答案是肯定的;这段代码是否足以进行测试?publicvoidtestSetandGet(){inta=10;ClassfirstClass=newClass();firstClass.setValue(10);intvalue=firstClass.getValue();Assert.assertTrue("Error",value==a);}在我的代码中,我认为如果有错误,我们无法知道错误是由于setter或getter而派生的。 最

java - POJO的优势是什么?

在我的项目中,我有一个小数据结构Key.publicclassKeyimplementsSerializable{privatestaticfinallongserialVersionUID=1L;publicStringdb;publicStringref;publicObjectid;protectedKey(){}publicKey(Stringdb,Stringref,Objectid){this.db=db;this.ref=ref;this.id=id;}}是的,这个类很简单,每个字段都可以公开访问。但有人建议我使用POJO样式类,但当我问为什么他们无法告诉我时。在我看来,

java - 在 setter 中使用 "this"

我看不出以下两种创建setter的方法之间有什么真正的区别,但我想知道我是不是太天真了。一个比另一个更受欢迎吗?publicvoidfooSetter(Stringbar){_bar=bar;}publicvoidfooSetter(Stringbar){this._bar=bar;} 最佳答案 在这种情况下没有语义差异,因为没有歧义。另一方面,如果您的实例字段也称为bar,则需要使用this来消除歧义:publicvoidfooSetter(Stringbar){this.bar=bar;}