java学习笔记2021.1.10
有關于多接口中的重名變量的解決方案:下面列出一些錯誤的寫法:
?
?interface one{static int x=11;}interface ?two{static int x=22;}class three {public int x=33;}public class TestOne implements one,two {public void test(){System.out.println(x);}?public static void main(String[] args) {new TestOne().test();}} ?interface one {static int x = 11;}?interface two {static int x = 22;}?class three {public int x = 33;}?public class TestOne extends three implements one, two {public void test() {System.out.println(x);}?public static void main(String[] args) {new TestOne().test();}}第一種正確的寫法應該是
?interface one {static int x = 11;}?interface two {static int x = 22;}?class three {public int x = 33;}?public class TestOne ?implements one, two {public void test() {System.out.println(one.x);}?public static void main(String[] args) {new TestOne().test();}}?關鍵在于明確是哪個接口的數據x
第二種正確的改法
?interface one {static int x = 11;}?interface two {static int x = 22;}?class three {public int x = 33;}?public class TestOne extends three implements one, two {public int x = 44;?public void test() {System.out.println(x);}?public static void main(String[] args) {new TestOne().test();}}不管在父類中是否定義了x,編譯系統都無法正常識別,這和方法是有一定區別的.
接口的繼承:1.接口可以多繼承.其實感覺是因為實例化接口的話實際上直接得到的方式是通過類的轉型來實現的,這就有了多繼承的可能性,而如果是類有多繼承的話,就會導致子類對應多個類,而這個的問題我感覺應該是出在了如果繼承了類有多個,若這些類中都有些同名的方法的話,子類就無法分清楚應該是繼承哪個類;2.如果多繼承中父類有同名的默認方法的話,那就要在子類中重新定義下,否則會報錯
成員內部類:方法一:
?外部類.內部類 類名 = new 外部類().new 內部類()方法2:
?外部類.內部類 類名 = 實例化后得到的外部類.new 內部類()方法3
?外部類.內部類 類名 = 實例化后得到的外部類.獲取方法其實簡單來說就是內部類無法直接被實例化出來,都是需要借助外部類來進行間接的實例化操作
默認的訪問權限同包中可以進行調用,跨包無法調用
抓住一個原則,默認構造器會導致類變量被初始化為0,然后內部類可以調用外部類的全部信息,但是如果內部類中有同名的方法和屬性的話,則優先調用內部類自己的方法和屬性,這個原則可以由代碼塊推導,內部類可以利用外部類的信息參考c語言中定義在所有函數外的變量可以被所有函數訪問(在這個變量的下面)
靜態內部類:首先我對用靜態修飾的變量,方法和類有一個直觀的理解就是,它們的所在地址是被該類的所有實例所共享的,因此不屬于任何一個實例對象,而是屬于類本身,因此,如果靜態內部類調用了外部類中的非靜態方法,必須實例化該外部類在進行調用,(外部類實例化時無參注意默認的構造方法0的問題),然后究其不能直接調用的原因我個人理解是因為這形成了1對多的不明確性,因為大部分的方法應該只有在特定的類中才是有效.
總結
以上是生活随笔為你收集整理的java学习笔记2021.1.10的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java学习笔记2021.1.9
- 下一篇: csapp学习笔记2021.1.9