+-
在java对象中添加和删除属性
我怎样才能在 java中实现这一点.我有一个具有属性的对象.

public class Object {

    private final Credentials Credentials;
    private final int PageSize;
    private final int PageStart;
    private final int DefaultFilterId;

    public Object(Credentials Credentials, int PageSize, int PageStart, 
int DefaultFilterId) {
            this.Credentials = Credentials;
            this.PageSize = PageSize;
            this.PageStart = PageStart;
            this.DefaultFilterId = DefaultFilterId;
       }

    }

现在我正在形成这样一个对象

Object obj = new Object(args);

在某些时候,我需要相同的对象,添加了新的属性,但删除了一些.

我在javascript中做了类似的事情.

var myCars=new Array();
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";

delete myCars[1]; or myCars.splice(1,1);
最佳答案
public class Object {

    private Credentials credentials;
    private int PageSize;
    private int PageStart;
    private int DefaultFilterId;

    public Object(Credentials credentials, int PageSize, int PageStart, int DefaultFilterId) {
        this.credentials = credentials;
        this.PageSize = PageSize;
        this.PageStart = PageStart;
        this.DefaultFilterId = DefaultFilterId;
    }

    // do that for the properties you want to be able to modify
    public void setCredentials(Credentials newCredentials) {
         this.credentials = newCredentials;
    }

}

你用它:

object.setCredentials(yourNewCredentials)

此外,您不应将对象命名为“Object”,它是Java中所有类的基类.

点击查看更多相关文章

转载注明原文:在java对象中添加和删除属性 - 乐贴网