Tag: getter

如何使用reflection定义动态setter和getter?

我在资源包的循环中列出了一个字符串,字段名称列表。 我创建一个对象然后使用循环我想为该对象设置值。 例如,对于对象 Foo f = new Foo(); 使用参数param1,我有字符串“param1”,我想以某种方式将“set”与“set”+“param1”连接起来,然后将其应用于f实例: f.setparam1(“value”); 对于吸气剂也一样。 我知道反思会有所帮助,但我无法做到。 请帮忙。 谢谢!

Java setter和getters

很长一段时间以来,我一直在努力解决java中的setter和getter问题。 例如,如果我想用适当的set和get方法编写一个包含名称,性别,年龄等信息的类。 然后在另一个类中,我想以此为例测试我的集合和getter: personInfo = myInfo() = new Personinfo(“Anna”, “female”, “17”); 我怎么做? 我知道我可以打印出像: public void printout() { System.out.printf(“Your name is: ” + getName() + ” and you are a ” + getSex()); }

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

我熟悉Java,但刚开始学习JavaFX,并专门了解JavaFX属性。 我理解Oracle的以下示例中显示的基本设计模式: package propertydemo; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; class Bill { // Define a variable to store the property private DoubleProperty amountDue = new SimpleDoubleProperty(); // Define a getter for the property’s value public final double getAmountDue(){return amountDue.get();} // Define a setter for the property’s value public final void setAmountDue(double value){amountDue.set(value);} // Define a getter […]

如何在getter链中跟踪NullPointerException

如果我在这样的调用中得到NullPointerException: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); 我得到一个相当无用的exception文本,如: Exception in thread “main” java.lang.NullPointerException at package.SomeClass.someMethod(SomeClass.java:12) 我发现很难找到实际上调用returend null,经常发现自己将代码重构为这样的东西: Foo ret1 = someObject.getSomething(); Bar ret2 = ret1.getSomethingElse(); Baz ret3 = ret2.getAnotherThing(); Bam ret4 = ret3.getYetAnotherOject(); int ret5 = ret4.getValue(); 然后等待更具描述性的NullPointerException,告诉我要查找哪一行。 你们中的一些人可能会认为,连接getter是一种糟糕的风格,无论如何都应该避免,但我的问题是:我可以在不更改代码的情况下找到错误吗? 提示:我正在使用eclipse,我知道调试器是什么,但我不知道如何将它应用于问题。 我对答案的结论是: 一些答案告诉我,我不应该一个接一个地连接吸气剂,一些答案显示我如何调试我的代码,如果我不赞成这个建议。 我已经接受了一个答案,它教会了我什么时候连锁吸气剂: 如果他们不能返回null,只要你喜欢就链接它们。 无需检查!= null,无需担心NullPointerExceptions(请注意链接仍然是demeter的法则,但我可以忍受 ) 如果它们可能返回null,则不要永远不会链接它们,并对每个可能返回null的值执行空值检查 这使得对实际调试的任何好建议毫无用处。