问题:
- 在行
Object o = myC.getConstructor(short.class).newInstance(myC.cast(pPrim));
有没有办法避免硬编码“
short.class
”,而是从 获得文字pPrim
?我从使用反射创建新对象
short.class
的答案中得到了使用“”的想法? - 我不应该使用 "
T o = ...
(例如,用于字节或短)而不是Object o = ...
?我认为我的方法与Class Literals 结尾处的方法几乎相同,即Runtime-Type Tokens 。
- 是我想做的一个案例的反思吗?
背景:
我正在学习 Finegan 和 Liguori 编写的 OCA Java SE 7:程序员 1 学习指南一书,为 1Z0-803 做准备。所以我经常练习代码。在练习时,我写了一个类,希望看到从 char 转换时原语内部发生了什么。我在下面列出了代码......如果你看一下,请关注方法 byteToBinaryString、shortToBinaryString 和 originalToBinaryString ......这就是我的问题出现的地方。
让我想到问题的步骤:
- 写了 byteToBinaryString
- 将 byteToBinaryString 克隆为 shortToBinaryString
- 想,“我应该能够避免这种方法重复,也许使用泛型”
- 将 shortToBinaryString 克隆为 originalToBinaryString 并尝试转换为泛型
- 开始认为这也是一种反思
- 被类文字硬编码困住了
这是我的代码
import java.util.TreeMap;
import java.util.Set;
public class StackoverflowQuestion {
// I wrote this 1st
public static String byteToBinaryString(byte pByte) {
int primLength = 8;
int count = 0;
String s = "";
while ( count++ < primLength ) {
byte sm = (byte) (pByte & 0x01);
pByte >>= 1;
s = sm + s;
if ( count % 4 == 0 && count != primLength ) {
s = " " + s;
}
}
return s;
}
// Then I cloned byteToBinaryString to this and had the thought,
// I shouldn' have to repeat this
public static String shortToBinaryString(short pShort) {
int primLength = 16;
int count = 0;
String s = "";
while ( count++ < primLength ) {
short sm = (short) (pShort & 0x0001);
pShort >>= 1;
s = sm + s;
if ( count % 4 == 0 && count != primLength ) {
s = " " + s;
}
}
return s;
}
// So I cloned shortToBinaryString, modifidied to this and ...
public static <T extends Number> String primitiveToBinaryString(T pPrim) {
int primLength = 16;
int count = 0;
String className = pPrim.getClass().getName();
try {
Class<?> myC = Class.forName(className);
// ... got stuck here
Object o = myC.getConstructor(short.class).newInstance(myC.cast(pPrim));
System.out.println(pPrim + "<--pPrim.equals(o)-->" + pPrim.equals(o) + "<--" + o);
} catch ( Exception e ) {
System.out.println("Caught exception: " + e);
}
String s = "";
while ( count++ < primLength ) {
//T sm = new Class<T>(pPrim.intValue() & 0x0001);
//pPrim >>= 1;
//s = sm + s;
if ( count % 4 != 0 && count != primLength ) {
s = "-" + s;
}
}
return s;
}
public static void main ( String[] args ) {
// exercise byteToBinaryString
for ( int i = 0; i < 256; i++ ) {
char cByte = (char) i;
byte b1 = (byte) cByte;
System.out.printf( "char(%c): charValue(%05d): bin(%s): dec(%+6d)\n", cByte, (int) cByte, byteToBinaryString(b1), b1 );
}
// exercise shortToBinaryString
// please ignore my use of TreeMap, just figuring out how it works
TreeMap<Integer, String> charsTM = new TreeMap<Integer, String>();
charsTM.put(00000, "00000");
charsTM.put(00001, "00001");
charsTM.put(32766, "32766");
charsTM.put(32767, "32767");
charsTM.put(32768, "32768");
charsTM.put(32769, "32769");
charsTM.put(65535, "65535");
short s1 = 32767;
char ch1 = 32768;
Set<Integer> charKeys = charsTM.keySet();
// loop through the boundary values I selected to show what's going on in memory
for ( Integer i : charKeys ) {
ch1 = (char) i.intValue();
s1 = (short) ch1;
System.out.printf( "char(%c): charValue(%05d): bin(%s): dec(%+6d)\n", ch1, (int) ch1, shortToBinaryString(s1), s1 );
}
// exercise primitiveToBinaryString
primitiveToBinaryString( (byte) 127 );
primitiveToBinaryString( (short) 32767 );
primitiveToBinaryString( (int) 2147483647);
primitiveToBinaryString( 2147483648L);
primitiveToBinaryString( 2147483648F);
primitiveToBinaryString( 2147483648D);
}
}