如果它似乎无关紧要,即值是一个参数而且函数不直接在调用对象实例上运行,那么"引擎盖下是什么?调用Class.function()和Object.function()?
之间的区别这可以通过一个错综复杂的例子来说明:
public String getImsiSIM1() {
return imsiSIM1;
}
public String getImsiSIM2() {
return imsiSIM2;
}
public boolean isSIM1Ready() {
return isSIM1Ready;
}
public boolean isSIM2Ready() {
return isSIM2Ready;
}
/*public static void setSIM2Ready(boolean isSIM2Ready) {
TelephonyInfo.isSIM2Ready = isSIM2Ready;
}*/
public boolean isDualSIM() {
return imsiSIM2 != null;
}
public String getNetworkOperator(){
return networkOperatorOne ;
}
public String getNetworkOperatorDual(){
return networkOperatorDual ;
}
private TelephonyInfo() {
}
public static TelephonyInfo getInstance(Context context){
if(telephonyInfo == null) {
telephonyInfo = new TelephonyInfo();
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
telephonyInfo.imsiSIM1 = telephonyManager.getDeviceId();;
telephonyInfo.imsiSIM2 = null;
telephonyInfo.networkOperatorOne =telephonyManager.getNetworkOperator();
telephonyInfo.networkOperatorDual = null ;
try {
telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0);
telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1);
telephonyInfo.networkOperatorOne = getCarrierName(context,"getCarrierName",0);
telephonyInfo.networkOperatorDual = getCarrierName(context,"getCarrierName",1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0);
telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1);
telephonyInfo.networkOperatorOne = getCarrierName(context,"getCarrierName",0);
telephonyInfo.networkOperatorDual = getCarrierName(context,"getCarrierName",1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.printStackTrace();
}
}
telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
telephonyInfo.isSIM2Ready = false;
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.printStackTrace();
}
}
}
return telephonyInfo;
}
private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
String imsi = null;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimID.invoke(telephony, obParameter);
if(ob_phone != null){
imsi = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return imsi;
}
private static boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
boolean isReady = false;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);
if(ob_phone != null){
int simState = Integer.parseInt(ob_phone.toString());
if(simState == TelephonyManager.SIM_STATE_READY){
isReady = true;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
private static String getCarrierName(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
String carrier = null;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method sim = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = sim.invoke(telephony, obParameter);
if(ob_phone != null){
carrier = ob_phone.toString();
if(carrier .equals(TelephonyManager.SIM_STATE_READY)) {
Log.d("Services","servicessss"+carrier);
;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return carrier;
}
private static class GeminiMethodNotFoundException extends Exception {
private static final long serialVersionUID = -996812356902545308L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
}
VS
Character c = new Character();
boolean b = c.isDigit(c);
如果只重复使用Character而不是一堆不同的实例名称,我可以看到硬编码变量更易于更改(查找/替换)的情况。最重要的是:最受欢迎的最佳做法是什么?
答案 0 :(得分:0)
对于静态方法,内部c.isDigit(c)
被视为Character.isDigit(c);
在使用引用变量调用静态方法的情况下,编译器只需用类名替换引用变量。
答案 1 :(得分:0)
完全没有区别。这是Java语法的一个怪癖,您可以通过实例引用调用static
方法。无论您是调用Character.isDigit
还是c.isDigit
,编译器都会发出相同的字节码,甚至不关心运行时c
是否为null
(因为c
在运行时不参与调用 - 因为字节码正在执行Character.isDigit
)。