簡體   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