Java學習筆記(32)—— 訪問控制符
阿新 • • 發佈:2021-02-16
這裡將自己學習java及其應用的一些筆記、積累分享一下,如果涉及到了文章、文字侵權,請聯絡我刪除或調整。
一、訪問控制符
1.1 概述
- 訪問控制符用於控制一個類,或類中的成員的訪問範圍
public:該類和非該類的均能訪問
protected:該類和該類的子類,同一個包內的成員也能訪問
[defalut]:同一個包內的類可以訪問
private:只有該類可以訪問
1.2 選擇原則
- 儘量使用小範圍
- public 是與其他開發者的一個契約,約定公開的東西會盡量保持穩定不變
- 小範圍的好處,便於維護修改
1.3 練習:學生抽獎
在一個類中,一般情況下,我們習慣將成員變數設定為privare型別,同時通過set/get方法提供對成員變數的修改與查詢途徑。
package 學生抽獎; public class Student { private int id; private String name; private String gender; private int age; private int times;// 中獎次數 public Student(int id, String name) { this(id,name,null,0,0); } public Student(int id, String name, String gender, int age, int times) { this.id = id; this.name = name; this.gender = gender; this.age = age; this.times = times; } @Override public String toString() { return id+","+name+","+gender+","+age+","+times; } public int getId() { return this.id; } public int getAge() { return this.age; } public int getTimes() { return this.times; } public String getName() { return this.name; } public String getGender() { return this.gender; } public void setTimes(int i) { this.times+=i; } public void setId(int id) { this.id = id; } }
package 學生抽獎; /* * 讀取、寫入與修改學生的資料與中獎次數的工具類 */ import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; public class StuIO { // ArrayList集合,用來存放所有的學生物件 // 靜態,原因是學生資料載入後,沒必要載入多分一樣的資料,僅有一份資料即可 static ArrayList<Student> stu = new ArrayList<Student>(); // 靜態初始化塊,類第一次被載入時執行,且只執行一次 static { try { /* * 讀取資料 * 之前用過的FileReader的read()方法一次讀取一個位元組; * BufferedReader readLine()讀取一行字串,讀取結束後,再讀返回null值; * 結合FileReader和BufferedReader來協同讀取資料,FileReader負責從檔案 * 讀取字元,FileReader可以以行為單位讀取文字資料,二者協同實現字串 * (文字)的行讀取 */ BufferedReader in = new BufferedReader(new FileReader("d:/Students.txt")); // 死迴圈讀取文字 while(true) { // 讀入指定檔案的一行資料 String line = in.readLine(); // 判斷檔案讀取是否結束,null為標誌 if(line == null) { System.out.println("讀取結束!"); break; } // 利用正則表示式匹配字串中任意位置的空白,並刪除空格 line = line.replaceAll("\\s+", ""); // 判斷刪除空格後,該行是否還有資料,或是空行 if(line.length() == 0) { continue;// 如若是空行,則無需進行下一步的操作,直接跳至讀取下一行 } // 將處理後的行資料處理成Student物件 Student s = strToStu(line); // 將該Student物件新增進泛型陣列列表 stu.add(s); } } catch (Exception e) { // TODO: handle exception } } private static Student strToStu(String line) { // 1,張三,男,23,0 // .split(",")方法將讀取的行以逗號為標誌分割為5個部分,分別存入a字串陣列 String[] a = line.split(","); // 引數為一個正則表示式 Student s = new Student( Integer.parseInt(a[0]),// a[i]是String陣列,對於int資料需要先解析為int型別 a[1], a[2], Integer.parseInt(a[3]), Integer.parseInt(a[4])); return s; } // 類測試方法 // public static void main(String[] args) { // for(int i =0;i<stu.size();i++) { // System.out.println(stu.get(i));; // } // } public static void save() { try { /* * FileWriter的write()方法 */ FileWriter out = new FileWriter("d:/Students.txt"); // 迴圈變數每一個泛型陣列中的Student物件並toString相應資訊,寫入指定檔案 for(int i =0;i<stu.size();i++) { Student s = stu.get(i); out.write(s.toString()+"\n");// txt文字的換行是\r\n,這是windows要求的 } out.close(); } catch (Exception e) { // TODO: handle exception } } }
package 學生抽獎;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
// while死迴圈抽獎,回車繼續
while(true) {
System.out.println("回車繼續,exit退出");
String s = new Scanner(System.in).nextLine();
if(s.equals("exit")) {
System.out.println("結束抽獎!");
break;
}
// 獲得Student物件的泛型陣列列表
ArrayList<Student> stu = StuIO.stu;
// 隨機取得下標[0,stu.size())
Student obj = stu.get(new Random().nextInt(stu.size()));
obj.setTimes(1);
System.out.printf("恭喜%s,您中了大獎!",obj.getName());
// 將改變的times寫入檔案,無需傳陣列列表引數,因為是static的
// 直接將StuIo類中的泛型陣列列表儲存至檔案
StuIO.save();
}
}
}