java学习笔记2022.1.15
其他
注意下,這種寫法下會導致a1在{}作用域外無法使用
所以針對異常處理的話,我個人感覺他不是用來在主函數(shù)里進行對象創(chuàng)建的時候用的,而是檢驗算法中正確性用的
論正確的如何在創(chuàng)建對象的過程中完成對錯誤的鑒定
?Pokemon pokemon = null;try{if(a.equals("Surf")){pokemon= new Pachirisu();}else if(a.equals("Cut")){pokemon= new Lapras();}else if (a.equals("Cut Surf")){pokemon= new Furret();}else if(a.equals("Cut,Surf")){pokemon = new Furret();}else {throw new Exception("Please input again , because your operation is out of order");}return pokemon;}catch (Exception e){e.printStackTrace();return null;}}如何在格式字符串輸出的時候里面包含雙引號,可以按下面的操作
? throw new Exception("Please input again and make sure that your operation is in the range of " +"\"Cut\" and \"Surf\" and \"Cut Surf\"");請說說 Java 中抽象類和接口的不同之處
我自己通過查資料,早期的接口其實沒有default這種方法,也就是說,它全部都是抽象方法,每一個都需要實現(xiàn)對象去實現(xiàn),而抽象類不是,抽象類可以自己先定義一些方法,然后選擇讓子類去實現(xiàn)那些抽象方法,并重寫父類中定義的方法,只要符合規(guī)范就好,當然,自從接口有了default的方法以后,這個差別就沒有了感覺
然后就是抽象類和子類是單繼承的關系,也就是說子類只能有一個父類,但是接口不一樣,一個類可以實現(xiàn)多個接口.
抽象類中可以定義私有方法,但是接口不行
接口的變量默認被什么修飾符修飾? 方法呢?
接口的變量默認被修飾為public static final類型,方法默認被public修飾符修飾
?public class two implements three {enum test{a,b,c,d,e,max}//three.a=89;?@Overridepublic void test() {System.out.println("hello ");}?public static void main(String[] args){//three.a=99;System.out.println(three.a);}}接口中只能有 public 公有方法 這句話正確咩?
我其實有點不太理解這句話,因為接口里還可以有屬性(狗頭)
如果說的是能不能有private修飾的方法的話,我的答案是不能,因為接口設計出來就是讓類實現(xiàn)方法用的,如果執(zhí)著定義私有方法,不如直接用抽象類來的舒服
父類如果實現(xiàn)了一個接口的話,子類就會默認繼承父類實現(xiàn)的接口,即便接口里被父類實現(xiàn)的方法存在默認方法
?public interface three {int a=9999;void test();public default void testTwo(){System.out.println("this is a test");}} ?public class two implements three {enum ?test{a,b,c,d,e,max}//three.a=89;?@Overridepublic void test() {System.out.println("hello ");}??public static void main(String[] args){//three.a=99;System.out.println(three.a);three Three = new two();}} ?public class four extends two{public static void main(String[] args){four test = new four();test.test();}}總結
以上是生活随笔為你收集整理的java学习笔记2022.1.15的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java学习笔记2022.1.17
- 下一篇: 周五测试题