[英]What's the semantical difference between the two statements?
我有兩個(相當無用的) interfaces
與test1
擴展test
interface test
{
int a = 8;
void show();
}
interface test1 extends test
{
int a = 4;
void show();
}
我有一個實現test1
接口的類MyClass
(再次有點無用)
public class MyClass implements test1
{
public void show()
{
System.out.println(((test)this).a); // 1
System.out.println(test.a); // 2
}
public static void main(String ar[])
{
new MyClass().show();
}
}
在我提供的代碼中,我有興趣知道兩個語句之間的語義差異 (如果有的話)分別為1.和2 . 因為結果明智,如果我錯了,請糾正我,他們幾乎是一樣的。
因此,接口變量默認為public static final
。 ((test)this).a
和test.a
之間沒有區別 ((test)this)
編譯器期望參考類型的test
。 對於編譯器來說這無關緊要。 例如
System.out.println(((test)null).a);
上面的行也可以工作,並為您提供適當的輸出。
沒有區別。 但是通過實例引用引用靜態成員並不是一個好習慣。
將this
轉換為父級以訪問其靜態字段實際上是誤導性的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.