繁体   English   中英

用数组初始化类属性-Java

[英]Class attributes initialization with array - java

我会知道一种更好的初始化此类的方法:

public class TestClass{
    private byte[] attribute0;
    private customClass0 attribute1;
    private customClass1 attribute2;
    ...

    public TestClass(byte[] args){
        int offset = 0;
        byte[] argsCustomClass0;
        byte[] argsCustomClass1;
        System.arraycopy(args, offset, attribute0, 0, attribute0.length);
        offset += attribute0.length;
        System.arraycopy(args, offset, argsCustomClass0, 0, argsCustomClass0.length);
        offset += attribute0.length;
        attribute1 = new CustomClass1(argsCustomClass0);
        System.arraycopy(args, offset, argsCustomClass1, 0, argsCustomClass1.length);
        offset += argsCustomClass1.length;
        attribute2 = new customClass1(argsCustomClass1);
        ...
    }

这是可行的,但是它很“脏”,有人知道使用数组初始化这些属性的另一种方法吗? 我精确地指出,我的args数组的属性编号也可能很大。

谢谢!

浮点数

使用数组数组:

public class TestClass{
    private byte[][] attributes;
    // -----^^^^^^^^

    public TestClass(byte[] args){
        int offset = 0;
        for (byte[] a : attributes) {
            System.arraycopy(args, offset, a, 0, a.length);
            offset += a.length;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM