如何在Java中创建只读数据类
我正在构建一个模拟器,我有一个名为emulator的类,它包含与字体相关的数据和一组稍后放入CPU的指令。然而,我希望有一个包含所有这些信息的容器类,以便Emulator更短、更可读。这是面向对象的吗?什么是好方法如何在Java中创建只读数据类,java,containers,readonly,Java,Containers,Readonly,我正在构建一个模拟器,我有一个名为emulator的类,它包含与字体相关的数据和一组稍后放入CPU的指令。然而,我希望有一个包含所有这些信息的容器类,以便Emulator更短、更可读。这是面向对象的吗?什么是好方法 final int[] fontSet = { 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 0x20, 0x60, 0x20, 0x20, 0x70, // 1 0xF0, 0x10, 0xF0, 0x80,
final int[] fontSet = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
private List<Instruction> initInstructions() {
List<Instruction> instructions = new ArrayList<>(INSTRUCTIONS_NUM);
add0Instructions(instructions);
add1Instructions(instructions);
add2Instructions(instructions);
add3Instructions(instructions);
add4Instructions(instructions);
add5Instructions(instructions);
add6Instructions(instructions);
add7Instructions(instructions);
add8Instructions(instructions);
add9Instructions(instructions);
addAInstructions(instructions);
addBInstructions(instructions);
addCInstructions(instructions);
addDInstructions(instructions);
addEInstructions(instructions);
addFInstructions(instructions);
return instructions;
}
private void add0Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Clears Display
public void execute(final CPU cpu) {
cpu.getMemoryMap().getMemory(MemoryType.DISPLAY).clear();
Platform.runLater(new Runnable() { // TODO: NO SE SI HACE FALTA
@Override
public void run() {
display.paint(cpu.getMemoryMap().getMemory(MemoryType.DISPLAY));
}
});
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.get() == 0x00E0;
}
});
instructions.add(new Instruction() { //Return from a subroutine
public void execute(CPU cpu) {
cpu.getInstPointer().set(cpu.getStack().pop() + 2);
}
public boolean validate(OpCode opCode) {
return opCode.get() == 0x00EE;
}
});
}
private void add1Instructions(List<Instruction> instructions) { // Set InstPointer to KKK // CHEQUEADA
instructions.add(new Instruction() {
public void execute(CPU cpu) {
cpu.getInstPointer().set(cpu.getOpCode().get() & 0x0FFF);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x01;
}
});
}
private void add2Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Call subroutine at nnn
public void execute(CPU cpu) {
cpu.getStack().push(cpu.getInstPointer().get() & 0x0000FFFF);
cpu.getInstPointer().set(cpu.getOpCode().get() & 0x0FFF);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x02;
}
});
}
private void add3Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx = kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
if(cpu.getRegistry(position).get() == data)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x03;
}
});
}
private void add4Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx != kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByte(cpu.getOpCode().get(), 1);
if (cpu.getRegistry(position).get() != data)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x04;
}
});
}
private void add5Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx = Vy
public void execute(CPU cpu) {
int position1 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int position2 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 2);
if(cpu.getRegistry(position1).equals(cpu.getRegistry(position2)))
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x05 && opCode.getNibble(3) == 0x00;
}
});
}
private void add6Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
cpu.getRegistry(position).set(data);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x6;
}
});
}
private void add7Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = Vx + kk
public void execute(CPU cpu) {
int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
cpu.getRegistry(position).set(Bitwise.getByteAsInt(cpu.getRegistry(position).get() + data, 1));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x07;
}
});
}
private void add8Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = Vy
public void execute(CPU cpu) {
int position1 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
int position2 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 2);
cpu.getRegistry(position1).set(cpu.getRegistry(position2).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x00;
}
});
instructions.add(new Instruction() { //MUCHAS OPERACIONES LOGICAS
public void execute(CPU cpu) { //Set Vx = Vx OR Vy
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).or(cpu.getRegistry(position2));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x01;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx AND Vy
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).and(cpu.getRegistry(position2));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x02;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx XOR Vy
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).xor(cpu.getRegistry(position2));
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x03;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx + Vy, set VF = carry
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).add(cpu.getRegistry(position2));
cpu.getRegistry(0xF).set(cpu.getRegistry(position1).get() > 255 ? 0x1 : 0x0);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x04;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx - Vy, set VF = NOT borrow
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
cpu.getRegistry(position1).sub(cpu.getRegistry(position2));
cpu.getRegistry(0xF).set(cpu.getRegistry(position1).get() > 0 ? 0x1 : 0x0);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x05;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx SHR 1
int position = cpu.getOpCode().getNibble(1);
cpu.getRegistry(0xF).set(cpu.getRegistry(position & 0x0001).get());
cpu.getRegistry(position).set(cpu.getRegistry(position).get() / 2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x06;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vy - Vx, set VF = NOT borrow
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
int sub = cpu.getRegistry(position2).get() - cpu.getRegistry(position1).get();
cpu.getRegistry(position1).set(sub);
cpu.getRegistry(0xF).set(sub > 0 ? 0x1 : 0x0);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x07;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = Vx SHL 1
int position = cpu.getOpCode().getNibble(1);
cpu.getRegistry(0x0F).set((cpu.getRegistry((position & 0x8000) == 0x8000 ? 1 : 0).get()));
cpu.getRegistry(position).set(cpu.getRegistry(position).get() * 2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x0E;
}
});
}
private void add9Instructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if Vx != Vy
public void execute(CPU cpu) {
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
if(!cpu.getRegistry(position1).equals(cpu.getRegistry(position2)))
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x09;
}
});
}
private void addAInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set I = nnn
public void execute(CPU cpu) {
cpu.getRegisterI().set(cpu.getOpCode().get() & 0x0FFF);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0A;
}
});
}
private void addBInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Jump to location nnn + V0
public void execute(CPU cpu) {
int sum = Bitwise.getByteAsInt(cpu.getRegistry(0x0).get(), 1) + (cpu.getOpCode().get() & 0x0FFF);
cpu.getInstPointer().set(sum);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0B;
}
});
}
private void addCInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Set Vx = random byte AND kk
public void execute(CPU cpu) {
int position = cpu.getOpCode().getNibble(1);
int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
data = Bitwise.and(data, rand.nextInt(255 + 1)); // TODO: TAMBIEN NECESITA UN RAND
cpu.getRegistry(position).set(data);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0C;
}
});
}
private void addDInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision
public void execute(final CPU cpu) {
int position1 = cpu.getOpCode().getNibble(1);
int position2 = cpu.getOpCode().getNibble(2);
int height = cpu.getOpCode().getNibble(3);
int x = Bitwise.getByteAsInt(cpu.getRegistry(position1).get(), 1);
int y = Bitwise.getByteAsInt(cpu.getRegistry(position2).get(), 1);
cpu.getRegistry(0x0F).set(0);
for(int offsetY = 0; offsetY < height; offsetY++) {
int line = cpu.getMemoryMap().getMemory(MemoryType.RAM).get(cpu.getRegisterI().get() + offsetY);
for(int offsetX = 0; offsetX < 8; offsetX++) {
int pixel = line & (0x80 >> offsetX);
if(pixel != 0) {
int totalX = x + offsetX;
int totalY = y + offsetY;
int index;
totalX = totalX % 64;
totalY = totalY % 32;
index = (totalY * 64) + totalX;
if(cpu.getMemoryMap().getMemory(MemoryType.RAM).get(index) == 1)
cpu.getRegistry(0x0F).set(1);
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(index, cpu.getMemoryMap().getMemory(MemoryType.RAM).get(index) ^ 1);
}
}
}
cpu.getInstPointer().add(2);
//screen.setNeedRedraw(true); //ESTE BOOLEAN TIENE QUE IR EN ALGUN LADO
Platform.runLater(new Runnable() {
@Override
public void run() {
display.paint(cpu.getMemoryMap().getMemory(MemoryType.DISPLAY));
}
});
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0D;
}
});
}
private void addEInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() { //Skip next instruction if key with the value of Vx is pressed
public void execute(CPU cpu) {
int position = cpu.getOpCode().getNibble(1);
int data = cpu.getRegistry(position).get();
if(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(data) == 1)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0E && (opCode.get() & 0x00FF) == 0x9E;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Skip next instruction if key with the value of Vx is not pressed
int position = cpu.getOpCode().getNibble(1);
int data = cpu.getRegistry(position).get();
if(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(data) == 0)
cpu.getInstPointer().add(2);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0E && (opCode.get() & 0x00FF) == 0xA1;
}
});
}
private void addFInstructions(List<Instruction> instructions) {
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set Vx = delay timer value
int position = cpu.getOpCode().getNibble(1);
cpu.getRegistry(position).set(cpu.getDelayTimer().get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x07;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Wait for a key press, store the value of the key in Vx
int position = cpu.getOpCode().getNibble(1);
int keyboardSize = cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).size();
for (int i = 0; i < keyboardSize; i++) {
if (cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(i) == 1) {
cpu.getRegistry(position).set(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(i));
cpu.getInstPointer().add(2);
cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).clear();
return;
}
}
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x0A;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set delay timer = Vx
int position = cpu.getOpCode().getNibble(1);
cpu.getDelayTimer().set(cpu.getRegistry(position).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && ((opCode.get() & 0x00FF) == 0x15);
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set sound timer = Vx
int position = cpu.getOpCode().getNibble(1);
cpu.getSoundTimer().set(cpu.getRegistry(position).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x18;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set I = I + Vx
int position = cpu.getOpCode().getNibble(1);
cpu.getRegisterI().set(cpu.getRegisterI().get() + cpu.getRegistry(position).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x1E;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Set I = location of sprite for digit Vx
int position = cpu.getOpCode().getNibble(1);
int character = cpu.getRegistry(position).get();
cpu.getRegisterI().set(0x0050 + character*5);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x29;
}
});
instructions.add(new Instruction() { //Store BCD representation of Vx in memory locations I, I+1, and I+2
public void execute(CPU cpu) {
int position = cpu.getOpCode().getNibble(1);
int data = cpu.getRegistry(position).get();
int hundreds = (data - (data % 100)) / 100;
int tens;
data -= hundreds * 100;
tens = (data - (data % 10)) / 10;
data -= tens * 10;
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get(), (byte)hundreds);
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + 1, (byte)tens);
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + 2, (byte)data);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x33;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Store registers V0 through Vx in memory starting at location I
int index = cpu.getOpCode().getNibble(1);
for(int i=0; i <= index; i++)
cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + i, (byte)cpu.getRegistry(i).get());
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x55;
}
});
instructions.add(new Instruction() {
public void execute(CPU cpu) { //Read registers V0 through Vx from memory starting at location I
int index = cpu.getOpCode().getNibble(1);
for(int i=0; i <= index; i++)
cpu.getRegistry(i).set(cpu.getMemoryMap().getMemory(MemoryType.RAM).get(cpu.getRegisterI().get() + i));
cpu.getRegisterI().set(cpu.getRegisterI().get() + index + 1);
cpu.getInstPointer().add(2);
}
public boolean validate(OpCode opCode) {
return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x65;
}
});
}
如果将数据存储在变量中: 方法1: 让它们成为最终的 示例:公共静态final FieldType MYFIELD 方法2: 您可以将所有变量设置为私有,并为它们编写getter 例2:
private static FieldType myField;
public static FieldType getMyField() {
return myField;
}
如果将数据存储在变量中: 方法1: 让它们成为最终的 示例:公共静态final FieldType MYFIELD 方法2: 您可以将所有变量设置为私有,并为它们编写getter 例2:
private static FieldType myField;
public static FieldType getMyField() {
return myField;
}
我不确定我是否正确地理解了你,但在我看来,你在谈论在你的源代码中定义一组常量。这是一个错误-数据不应该存在于源代码中。它应该存在于数据文件中 我不确定我是否正确地理解了您的意思,但在我看来,您正在谈论在源代码中定义一组常量。这是一个错误-数据不应该存在于源代码中。它应该存在于数据文件中 不变性是您要寻找的关键词。使数据容器模型类不可变始终是一个好的设计
有效Java项目第15项对此进行了深入探讨。不变性是您要寻找的关键词。使数据容器模型类不可变始终是一个好的设计
有效的Java项目第15项对此进行了深入讨论。是的,这是Java中的常见做法。创建如下所示的类:
public class Data {
private String property1;
public Data(String property1) {
this.property1 = property1;
}
public String getProperty1() {
return property1;
}
}
是的,这是Java中的常见做法。创建如下所示的类:
public class Data {
private String property1;
public Data(String property1) {
this.property1 = property1;
}
public String getProperty1() {
return property1;
}
}
老实说,我不完全确定您试图从发布的代码中实现什么。然而,作为对实际标题的回应,您应该研究如何在Java中创建只读数据类 从本质上讲,枚举将使您能够在编译时创建一次数据集,并提供一系列帮助器方法来读取所需的数据
我不完全确定它是否会帮助您的用例,但它肯定会允许您将数据分离为只读样式格式。老实说,我不完全确定您试图从发布的代码中实现什么。然而,作为对实际标题的回应,您应该研究如何在Java中创建只读数据类 从本质上讲,枚举将使您能够在编译时创建一次数据集,并提供一系列帮助器方法来读取所需的数据
我不完全确定它是否会对您的用例有所帮助,但它肯定会允许您将数据分离为只读格式。您能给出数据不应存在于源代码中的原因吗?不,它们不都是常量。事实上,CPU指令是作为匿名类列表添加的,因为该指令是一个具有两种方法的接口:boolean validateopCode和void execute。在这种情况下,将它们放在一个文件中是没有任何意义的。@kcochibili主要的原因是它不灵活。很难进行实时交换,而创建一个新的源代码比创建一个简单的例子要困难得多:考虑一个简单的例子:你有一个带有字符串字符串的程序。你想把这个项目国际化。使用以任何合理格式(XML、JSON等)在文本文件中定义的映射或者使用定义为可执行代码的映射更容易做到这一点吗?如果你想编写一个实用程序,让一个母语为英语的人进行翻译,你愿意发出一个数据文件还是java源代码?@user3452637没有你的程序在我面前,我不能肯定,但听起来你可能在这方面走了很长的路。不管怎样,这是你的五分钱,你可以搭便车。你能给出一个数据不应该存在于源代码中的原因吗?不,它们不都是常量。事实上,CPU指令是作为匿名类列表添加的,因为该指令是一个具有两种方法的接口:boolean validateopCode和void execute。在这种情况下,将它们放在一个文件中是没有任何意义的。@kcochibili主要的原因是它不灵活。很难进行实时交换,而创建一个新的源代码比创建一个简单的例子要困难得多:考虑一个简单的例子:你有一个带有字符串字符串的程序。你想把这个项目国际化。使用以任何合理格式(XML、JSON等)在文本文件中定义的映射或者使用定义为可执行代码的映射更容易做到这一点吗?如果您想编写一个实用程序,让母语为英语的人进行翻译,您愿意发出数据文件还是java源代码?@user3452637没有您的
在我面前的程序,我不能肯定,但听起来你可能会在这个问题上走很长的路。不管怎么说,这是你的五分钱,你可以搭便车。-1建议字段和方法是静态的。@DiogoSantana,为什么这不合适?因为在过程语言中,静态就像全局变量。看看这个链接:我现在就要读了。您是否建议我在Emulator类中使用Data类的实例作为变量?是的,正是这样。您可以始终将某个数据实例传递给任何需要它的方法或类。无需实例化另一个。-1表示字段和方法是静态的。@DiogoSantana,为什么这样做不合适?因为在过程语言中,静态就像全局变量。看看这个链接:我现在就要读了。您是否建议我在Emulator类中使用Data类的实例作为变量?是的,正是这样。您可以始终将某个数据实例传递给任何需要它的方法或类。不需要再举例说明另一个。老实说,我不知道为什么在没有解释的情况下,这一点被否决了。枚举是常数数据的完美用途。根据定义,只读的东西是指未设置或从未更改的东西。这是常数的精确定义。快速查看fontSet定义会使它看起来像是可以迭代的枚举数据,等等。我将研究它!我从来没有用过这个词,我不知道为什么在没有解释的情况下否决了它。枚举是常数数据的完美用途。根据定义,只读的东西是指未设置或从未更改的东西。这是常数的精确定义。快速查看fontSet定义会使它看起来像是可以迭代的枚举数据,等等。我将研究它!我从未使用过enum