所以我想知道为什么这会返回错误:“无法从类型Compn”中对非静态方法comparen( args )进行静态引用。我显然无法使方法comparen()静态,所以不确定是什么问题。
//8 bit comparison. bit 7 and bit 15 are highest.
public static boolean[] comparator(boolean bit0, boolean bit1, boolean bit2,
boolean bit3, boolean bit4, boolean bit5,
boolean bit6, boolean bit7,
boolean bit8, boolean bit9, boolean bit10,
boolean bit11, boolean bit12, boolean bit13,
boolean bit14, boolean bit15) {
boolean[] comparatorOUT = new boolean[3]; //0 == A>B, 1 == B>A, 2 == A==B
boolean[][] currVal = new boolean[16][3]; // [0] = highest bit.
class Compn {
boolean[] comparen(boolean curr1, boolean curr2, boolean curr3, boolean b1, boolean b2) {
boolean[] storage = new boolean[3];
storage[0] = curr3 && (((!xor(curr1, curr2) && b1) && !(!xor(curr1, curr2) && b2)) || curr1 && !curr2);
storage[1] = curr3 && (((!xor(curr1, curr2) && b2) && !(!xor(curr1, curr2) && b1)) || !curr1 && curr2);
storage[2] = curr3 && (!storage[0] && !storage[1]);
return storage;
}
}
currVal[0] = Compn.comparen(false, false, true, bit7, bit15);
currVal[1] = Compn.comparen(currVal[0][0], currVal[0][1], currVal[0][2], bit6, bit14);
currVal[2] = Compn.comparen(currVal[1][0], currVal[1][1], currVal[1][2], bit5, bit13);
currVal[3] = Compn.comparen(currVal[2][0], currVal[2][1], currVal[2][2], bit4, bit12);
currVal[4] = Compn.comparen(currVal[3][0], currVal[3][1], currVal[3][2], bit3, bit11);
currVal[5] = Compn.comparen(currVal[4][0], currVal[4][1], currVal[4][2], bit2, bit10);
currVal[6] = Compn.comparen(currVal[5][0], currVal[5][1], currVal[5][2], bit1, bit9);
currVal[7] = Compn.comparen(currVal[6][0], currVal[6][1], currVal[6][2], bit0, bit8);
comparatorOUT[0] = currVal[0][0] || currVal[1][0] || currVal[2][0] || currVal[3][0] || currVal[4][0] || currVal[5][0] || currVal[6][0] || currVal[7][0];
comparatorOUT[1] = currVal[0][1] || currVal[1][1] || currVal[2][1] || currVal[3][1] || currVal[4][1] || currVal[5][1] || currVal[6][1] || currVal[7][1];
comparatorOUT[2] = !comparatorOUT[0] && !comparatorOUT[1];
return comparatorOUT;
}
答案 0 :(得分:2)
只有当方法是静态的时,你才能这样调用。
Compn.comparen
你这样做是
Compn c = new Compn();
currVal[0] = c.comparen(false, false, true, bit7, bit15);
或将comparen更改为static。
为什么不把Compn类放在方法之外?
答案 1 :(得分:0)
你是对的,你不能在comparen
内使Comp
静止。但这也意味着你不能称它为静态。
从您的示例中,您不清楚为什么需要课程Compn
。您可以将comparen
设为comparator
的静态同级方法。
答案 2 :(得分:0)
您已声明本地类,它是一种内部类,它不能声明静态成员。 Java有这个限制是有原因的:根本没有必要声明你的课程。
将comparen
方法移至比较器方法的兄弟,然后将其设为static
。它没有捕获任何本地状态,所以没有任何东西强迫你首先介绍这个类。
如果要将comparen
与comparator
封装在一起,则需要为这两个分别设置一个类。这是使用Java语言功能实现目标的唯一方法。
答案 3 :(得分:0)
这是因为方法comparen
是类Compn
的实例方法。如果不创建该类的实例,则无法调用实例方法。