当前位置: 代码迷 >> J2SE >> 求教下c语言中的结构体怎么在java中怎么实现
  详细解决方案

求教下c语言中的结构体怎么在java中怎么实现

热度:123   发布时间:2016-04-23 19:47:10.0
求教下c语言中的结构体如何在java中如何实现
先附上需要转换的代码:
type
  Header = packed record
    Width:          byte;
    length:         byte;
    No_use:        byte;
    CN:               byte;
    No_use2:      byte;
    Signature:     array[0..5] of char; 
    UD:               byte;
    DOW:            byte;
    DOL:             byte;
  end;


语言其实是pascal语言,但是这个结构应该是类似于c中的结构体。用java实现后用处主要有2个:
1) 后面程序需要用到里面定义的变量,如Width=Header.Width
2) 可以读取到这个结构体(即头部)的长度,如用sizeof(header)可以读到这个头部占14个字节

应该是用类实现,或者加上内部类,但是lz初学Java,不知道怎么实现,求大神赐教!

------解决思路----------------------
学语言,最忌讳的就是,去新学的语言里面找原来的语言的对应功能了。
你也别闹那个心了,你把你的需求说明白,别只说读,你说说读完了你想干嘛,省得别人回答你了,你又觉得人家回答的不对路子,到时话说的挺难听的,你也不安宁,回答的人也不爽。
------解决思路----------------------
	public class Header {
private byte width;
private byte length;
private byte noUse;
private byte cn;
private byte noUse2;
private char[] signature;
private byte ud;
private byte dow;
private byte dol;

public byte getWidth() {
return width;
}

public void setWidth(byte width) {
this.width = width;
}

public byte getLength() {
return length;
}

public void setLength(byte length) {
this.length = length;
}

public byte getNoUse() {
return noUse;
}

public void setNoUse(byte noUse) {
this.noUse = noUse;
}

public byte getCn() {
return cn;
}

public void setCn(byte cn) {
this.cn = cn;
}

public byte getNoUse2() {
return noUse2;
}

public void setNoUse2(byte noUse2) {
this.noUse2 = noUse2;
}

public char[] getSignature() {
return signature;
}

public void setSignature(char[] signature) {
this.signature = signature;
}

public byte getUd() {
return ud;
}

public void setUd(byte ud) {
this.ud = ud;
}

public byte getDow() {
return dow;
}

public void setDow(byte dow) {
this.dow = dow;
}

public byte getDol() {
return dol;
}

public void setDol(byte dol) {
this.dol = dol;
}
}
  相关解决方案