1.1.類的定義 創建類 創建對象 1.2.private關鍵字 1.3.構造方法 1.4.Scanner類 1.5.Random類 1.6.ArrayList集合 1.7.String類 ...
1.1.類的定義
創建類
package derek.day06.demo; public class Student { //成員變數 String name; int age; //成員方法 public void eat(){ System.out.println("吃飯"); } public void study(){ System.out.println("學習"); } }
創建對象
package derek.day06.demo; public class demoStudent { public static void main(String[] args) { //創建對象 Student stu = new Student(); stu.name = "derek"; stu.age = 18; System.out.println(stu.name); stu.eat(); } }
1.2.private關鍵字
package derek.day06.demo; public class Student { //成員變數 String name; private int age; public void show(){ System.out.println(name+":"+age); } public void setAge(int num){ if (num < 100 && num > 0){ age = num; }else { System.out.println("錯誤的數據"); } } public int getAge(){ return age; } }
package derek.day06.demo; public class demoStudent { public static void main(String[] args) { Student stu = new Student(); stu.name = "derek"; stu.setAge(18); stu.show(); } }
1.3.構造方法
package derek.day06.demo; import org.w3c.dom.ls.LSOutput; public class Student { //成員變數 private String name; private int age; //無參數的構造方法 public Student(){ System.out.println("無參數的構造方法執行"); } //全參數的構造方法 public Student(String name, int age){ System.out.println("全參構造方法執行"); this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } }
package derek.day06.demo; public class demoStudent { public static void main(String[] args) { Student s1 = new Student(); //無參數的構造方法執行 Student s2 = new Student("derek", 18); //全參構造方法執行 } }
1.4.Scanner類
package derek.learn; //1.導包 import java.util.Scanner; public class DemoScanner { public static void main(String[] args) { //2.創建,System.in代表從鍵盤進行輸入 Scanner sc = new Scanner(System.in); //3.使用,獲取鍵盤輸入的int數字 int num = sc.nextInt(); System.out.println(num); //字元串 String name = sc.next(); System.out.println(name); } }
1.5.Random類
package derek.learn; import java.util.Random; public class DemoRandom { public static void main(String[] args) { Random r = new Random(); int num = r.nextInt(10); System.out.println(num); } }
1.6.ArrayList集合
package derek.learn; import java.util.ArrayList; public class DemoArrayList { public static void main(String[] args) { //創建一個ArrayList集合,裡面是String類型的數據 ArrayList<String> list = new ArrayList<>(); System.out.println(list); //[] //1.向集合中添加數據 list.add("迪麗熱巴"); list.add("古力娜扎"); list.add("馬爾扎哈"); System.out.println(list); //[迪麗熱巴, 古力娜扎, 馬爾扎哈] //2.獲取 System.out.println(list.get(1)); //古力娜扎 //3.刪除 list.remove(1); System.out.println(list); //[迪麗熱巴, 馬爾扎哈] //4.獲取集合長度 System.out.println(list.size()); //2 //5.遍歷 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
1.7.String類
package derek.learn; public class DemoString { public static void main(String[] args) { String str1 = "Zhang"; String str2 = "Derek"; //1.拼接字元串 String str3 = str1.concat(str2); System.out.println(str3); //ZhangDerek //2.獲取指定索引位置的字元 System.out.println(str1.charAt(1)); //h //3.獲取子字元串的索引 System.out.println(str3.indexOf('e')); //6 //4.截取 System.out.println(str3.substring(1,4)); //han //5.轉換成為字元數組 char[] chars = "Hello".toCharArray(); System.out.println(chars[1]); //e //6.替換 String str4 = "aaabbbccc"; String str5 = str4.replace('a','d'); System.out.println(str5); //dddbbbccc //7.分割 String str6 = "aa,bb,cc,d"; String[] arr = str6.split(","); System.out.println(arr[1]); //bb } }