1、複習了一維數組,還複習了強制類型轉換的注意點。java
package com.bjpowernode.java_learning; public class D67_1_GoDeepIntoArrays { public static void main(String[] args) { //建立一個數組,這個數組既能夠存儲Dog,也能夠存儲Cat Animal67[] as = new Animal67[4]; //給數組每一個元素賦值 Dog67 d1 = new Dog67(); Dog67 d2 = new Dog67(); Cat67 c1 = new Cat67(); Cat67 c2 = new Cat67(); as[0] = d1; as[1] = d2; as[2] = c1; as[3] = c2; //需求:遍歷數組,取出每一個對象,若是是Dog67那麼就執行eat方法,若是是Cat67就執行move方法 for(int i = 0;i<as.length;i++) { Animal67 a = as[i]; if (a instanceof Cat67) { Cat67 c3 = (Cat67)a; c3.move(); }else { Dog67 d3 = (Dog67)a; d3.eat(); } } } } class Animal67{ } class Dog67 extends Animal67{ public void eat() { System.out.println("Dog eat"); } } class Cat67 extends Animal67{ public void move() { System.out.println("Cat move"); } }
package com.bjpowernode.java_learning; public class D67_1_GoDeepIntoArrays { public static void main(String[] args) { //建立一個數組,這個數組既能夠存儲Dog,也能夠存儲Cat Animal67[] as = new Animal67[4]; //給數組每一個元素賦值 Dog67 d1 = new Dog67(); Dog67 d2 = new Dog67(); Cat67 c1 = new Cat67(); Cat67 c2 = new Cat67(); as[0] = d1; as[1] = d2; as[2] = c1; as[3] = c2; //需求:遍歷數組,取出每一個對象,若是是Dog67那麼就執行eat方法,若是是Cat67就執行move方法 for(int i = 0;i<as.length;i++) { Animal67 a = as[i]; if (a instanceof Cat67) { Cat67 c3 = (Cat67)a; c3.move(); }else { Dog67 d3 = (Dog67)a; d3.eat(); } } } } class Animal67{ } class Dog67 extends Animal67{ public void eat() { System.out.println("Dog eat"); } } class Cat67 extends Animal67{ public void move() { System.out.println("Cat move"); } }
2、方法調用的時候也能夠傳遞一個數組node
package com.bjpowernode.java_learning; public class D67_2_ { public static void main(String[] args) { //第一種方式 int[] a = {12,3,4,5,6}; m1(a); //第二種方式 m1(new int[] {34,4,5,6}); //m1({34,4,5,6});這種方式是不行的,編譯報錯 } public static void m1(int[] a) { for(int i = 0;i<a.length;i++) { System.out.println(a[i]); } } }
3、關於main方法中的參數列表String[] argsgit
1.String[] args是專門用來接受命令行參數的github
2.例如:java ArrayTest07 abc def aaa數組
JVM在調用ArraysTest07類的main方法以前,先「abc def aaa"這個字符串以」空格「的方式進行分割,而後存儲在String數組中。·微信
package com.bjpowernode.java_learning; public class D67_3_ArgsAnlysis { public static void main(String[] args) { System.out.println("String類型的數組中的元素個數爲:" + args.length); //遍歷 for(int i=0; i<args.length;i++) { System.out.println(args[i]); } //需求說明:運行該軟件的時候必須提供用戶名和密碼。 //格式:java D67_3_ArgsAnlysis usrname password //若是用戶沒有提供足夠的參數,則退出系統 if (args.length != 2) { System.out.println("要想使用這個系統,就必須這樣輸入java D67_3_ArgsAnlysis usrname password"); return ; } //參數正確,若是用戶名爲admin,密碼是123則登錄成功 String username = args[0]; String passsword = args[1]; if ("admin".equals(username) && "123".equals(password)) { //這裏若是寫成username.equals("admin"),就容易形成空指針異常 System.out.println("登陸成功,歡迎admin"); }else { System.out.println("登陸失敗,請從新確認") } } }
4、源碼:學習
D67_1_GoDeepIntoArrays.java大數據
D67_2_TwoWaysOfInvolingArray.javaui
D67_3_ArgsAnlysis.javaspa
https://github.com/ruigege66/Java/blob/master/D67_1_GoDeepIntoArrays.java
https://github.com/ruigege66/Java/blob/master/D67_2_TwoWaysOfInvolingArray.java
https://github.com/ruigege66/Java/blob/master/D67_3_ArgsAnlysis.java
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關注微信公衆號:傅里葉變換,我的公衆號,僅用於學習交流,後臺回覆」禮包「,獲取大數據學習資料