簡體   English   中英

這兩個陳述之間的語義差異是什么?

[英]What's the semantical difference between the two statements?

我有兩個(相當無用的) interfacestest1擴展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).atest.a之間沒有區別 ((test)this)編譯器期望參考類型的test 對於編譯器來說無關緊要。 例如

System.out.println(((test)null).a);

上面的行也可以工作,並為您提供適當的輸出。

沒有區別。 但是通過實例引用引用靜態成員並不是一個好習慣。

this轉換為父級以訪問其靜態字段實際上是誤導性的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM