- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当我从 XSD 生成 JAXB 类时,具有 maxOccurs="unbounded"
的元素会获得为它们生成的 getter 方法,但没有 setter 方法,如下所示:
/**
* Gets the value of the element3 property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the element3 property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getElement3().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Type }
*
*
*/
public List<Type> getElement3() {
if (element3 == null) {
element3 = new ArrayList<Type>();
}
return this.element3;
}
方法注释清楚地说明了如何使用它,但我的问题如下:
为什么 JAXB 不只是生成一个 setter,遵循 Java Beans 规则? 我知道我可以自己编写 setter 方法,但是生成的 getter 方法中建议的方法有什么优势吗?
这是我的 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/DoTransfer/" targetNamespace="http://www.example.org/DoTransfer/">
<element name="CollectionTest" type="tns:CollectionTest"></element>
<complexType name="CollectionTest">
<sequence>
<element name="element1" type="string" maxOccurs="1" minOccurs="1"></element>
<element name="element2" type="boolean" maxOccurs="1" minOccurs="1"></element>
<element name="element3" type="tns:type" maxOccurs="unbounded" minOccurs="1" nillable="true"></element>
</sequence>
</complexType>
<complexType name="type">
<sequence>
<element name="subelement1" type="string" maxOccurs="1" minOccurs="1"></element>
<element name="subelement2" type="string" maxOccurs="1" minOccurs="0"></element>
</sequence>
</complexType>
</schema>
最佳答案
这是来自 JAXB 规范的理由 - 第 60 页。
Design Note – There is no setter method for a List property. The getter returns the List by reference. An item can be added to the List returned by the getter method using an appropriate method defined on java.util.List. Rationale for this design in JAXB 1.0 was to enable the implementation to wrapper the list and be able to perform checks as content was added or removed from the List.
因此,如果 List 的实现覆盖了 add/remove 以执行验证,那么用(例如)ArrayList 替换那个“特殊”的 List 会破坏这些检查。
关于java - 为什么 JAXB 不为列表生成 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13913000/
因此,我需要获取在为其赋值时调用的 setter 的名称。像这样: var b = {}; var a = { set hey(value) { b[] = value; } } 我希望 se
我是 Android 编程的新手(~ 2 个月)有必要为几十个不同的变量设置 getter 吗? 例如—— //Yes I realise that this isn't 'dozens' publi
import Control.Lens import Control.Lens.TH data Foo = Foo { _bar, _baz :: Int } makeLenses ''
我有点困惑:我可以覆盖 setter/getter 但仍然使用 super setter/getter 吗?如果是 - 怎么办? 用例: class A { void set value(num
我有一个接收消息的应用程序。消息中存在可编辑的字段。当字段更改时,应将其保存到数据库中。不幸的是,setter 仅在 setter 的范围内更改给定字段的值。知道为什么会发生这种情况吗?这是 gett
C# 中有没有一种方法可以让 setter 从“某物”继承,这样每次为特定基类及其继承者调用 setter 时,我都可以运行一些代码? 我想做的是在我的基类上有一个名为 IsValid 的 bool
可能是一个我无法解决的非常简单的问题 - 我从 C# 开始,需要使用 getter/setter 方法向数组添加值,例如: public partial class Form1 : Form {
这两个属性实现有什么区别? public override string A { get { return "s"; } set { } } public override strin
是否可以使用 abc.abstractproperty 创建一个具体的 getter 但将 setter 抽象为每个继承类的不同。我为每个子类处理不同的 val 设置。 例如。 @abstractpr
我在某处看到类似下面的内容,想知道它是什么意思。我知道他们是getter和setter,但是想知道为什么字符串Type是这样定义的。谢谢你帮助我。 public string Type { get;
Public class Example { private int number; public Example(int number){ this.number =
假设我有这样的代码: public response MyMethod(Request req) { String id = req.getFirst().geId(); } 我已经模拟了主对
允许这样做: public int Age { get; set; } 但是应用程序是否为变量创建/分配空间?我经常这样做 private int age = 0; public int Age {
我有一个条件,我构造字符串 (finalValue) 的方式是基于我在输入中获得的非空值的数量。所以我想知道是否可以用一个不同的参数为字符串 (finalValue) 重载 setter 方法并根据我
例如,在这段代码中 var o = { set a(value) {this.b = value}, get a() {return this.b} } 是否有可能获得对 o.a 的 sett
我一直在努力了解 getter 和 setter,但没有深入了解。我读过 JavaScript Getters and Setters和 Defining Getters and Setters只是没
我想在我的类中添加一个 getter 和 setter。然而,setter 应该接收一个 querySelector,但 getter 返回一个新类型 pageSections。 我的问题是 gett
使用有什么好处: private var _someProp:String; public function set someProp(value:String):void { _somePr
当从域类调用它时,我想在我的 setter 中执行一些操作,而不是从 hibernate 中调用它时。此外,我正在使用 session 工厂,因此我无法使用 @PostLoad 来触发标志! 有人对此
人员类别: public class Person { private String firstName; private String lastName; public Pe
我是一名优秀的程序员,十分优秀!