02- 常用類1

枚舉

需求引入

商家賣餡餅

買家買餡餅

public class Pie {
    String name;
    double price;
    public Pie() {
    }
    public Pie(String name, double price) {
        this.name = name;
        this.price = price;
    }
}
// 客戶類
Pie pie = new Pie("龍肉",10);  // 去執行的 Pie 的有參構造方法
// 不能被自定義的創建對象

將構造方法私有化, 不允許外界創建對象

public class Pie {
    /* 枚舉的本質 */
    public static final Pie pie1 = new Pie("牛肉",5);
    public static final Pie pie2 = new Pie("裏脊肉",4);
    public static final Pie pie3 = new Pie("雞肉",3);
    public static final Pie pie4 = new Pie("韭菜雞蛋",2);
    
    String name;
    double price;
    private Pie() {
    }
    private Pie(String name, double price) {
        this.name = name;
        this.price = price;
    }
}

把構造私有化 , 不允許外界創建對象, 在類的內部自己創建對象

外界可以調用類中已經創建好的對象, 僅僅是希望外界訪問,但是不允許修改

最好把該類對象 創建爲 public static final

星期, 人的性別

public class Gender {
    public static final Gender MALE = new Gender("男");
    public static final Gender FEMALE = new Gender("女");

    String name; // 性別名稱
    private Gender() {
    }

    private Gender(String name) {
        this.name = name;
    }
}
public class Person {
    String name;
    int age;
    // 給人設爲固定性別
    Gender gen;
}

星期

public class Week {
    public static final Week MON = new Week("星期一");
    public static final Week TUE = new Week("星期二");
    public static final Week WED = new Week("星期三");
    // ...

    String name;
    private Week(String name) {
        this.name = name;
    }
}

簡化爲

public Enum Week{
    MON,TUE,WED // ,...
}

概念

由一組固定的常量組成的類型

比如 中國 34個省

一年有4 個季節, 12 個月份

一週有7 天

公司 中有一些領導者 一般也不會變動

案例

使用枚舉輸出每週 日程安排

public enum Week {
    MON,TUE,WED,THU,FRI,SAT,SUN
    //相當於
    // public static final Week MON = new Week();
}
public class WeekDemo {
    public static void main(String[] args) {
        weekTest(Week.MON);
    }
    public static void weekTest(Week day){
        switch(day){
            //  MON,TUE,WED,THU,FRI,SAT,SUN
            case MON :
            case TUE :
            case WED :
            case THU :
            case FRI :
                System.out.println("努力工作, 天天向上!");
                break;
            case SAT :
            case SUN :
                System.out.println("週末好好休息!");
                break;
                default:
                    System.out.println("填寫錯誤!");
                    break;

        }
    }
}

優勢

  • 可以使代碼更易維護, 有助於確保爲變量指定的值, 是合法的, 期望的
  • 枚舉類型 是對一組常量類型的簡化
  • 代碼更清晰, 允許使用描述性的名稱來標識數據

拓展

枚舉也可以擁有參數

public enum Pie {
    P1("牛肉"),P2("豬肉");

    String name;
    private Pie() { }
    private Pie(String name){
        this.name = name;
    }
}

API

全稱 Java Application Programming Interface

​ java 應用 編程 接口

java 預先定義的運行庫集合, 程序員可以直接使用這些打包好的類和接口

API 編程文檔 幫助文檔

包裝類

java 是一門純面向對象的語言 , 基本數據類型也是對象

在實際開發中, 對基本數據類型的操作非常多, 按照對象的方式操作, 很多的不方便的地方, 把基本數據類型從對象中抽取出來, 簡化了書寫方式, 以方便開發

基本數據類型所對應的對象 就是 該基本數據類型的包裝類

基本數據類型 包裝數據類型
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
int i = 10;  // 常量池中保存
Integer in = new Integer(10); // 堆中保存

包裝類用途

用在泛型上

使用集合的時候, 集合只能存儲引用數據類型 對象

List<int> list = new  ArrayList<int>(); // 錯誤, 集合只能存儲引用數據類型
list.add(10)

List<Integer> list = new  ArrayList<Integer>(); // 正確, 集合只能存儲引用數據類型

包裝類 是一個對象, 對象有屬性和方法

基本數據類型, 無法進行方法操作, 只有轉換成爲包裝類型, 纔可以調用方法

包裝類和基本數據類型的轉換

基本數據類型 ==> 包裝類

int i = 10;
// 使用構造方法
Integer in = new Integer(i)

// 使用靜態方法valueOf(int 類型)
Integer in = Integer.valueOf(i);

包裝類==> 基本數據類型

Integer in = Integer.valueOf(100);
int i = in.intValue();

自動裝箱, 自動拆箱

Integer in = Integer.valueOf(100);
int i = in;

int a = 100;
Integer b = a;

Math

工具類, 包含基本的數學運算和幾何運算

方法名 說明
abs() 絕對值
ceil(3.14) 返回大於或等於參數的最小
(最接近負無窮大) double整數值
floor(3.14) 返回小於或等於參數的最大
(最接近正無窮大) double值,等於一個數學整數。
max/min(double a, double b) 返回兩個 double值中的較大/較小值
pow(a,b) a^b a的b次冪
random 返回一個 0 - 1 之間的隨機數
round 四捨五入

Scanner

判斷的方法

異常的問題

判斷用戶輸入的內容是否是某種數據類型

在鍵盤錄入數據的時候, 使用nextInt() 只能獲取整數類型, 一旦輸入非整數, 拋出異常

java.util.InputMismatchException

public class Demo1_Scanner {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int [] arr = new int [3];
        // 使用增強型for 遍歷數組
        for(int i = 0 ; i < arr.length ; i++){
            System.out.println("請輸入:");
           arr[i] = input.nextInt();
        }

        System.out.println(Arrays.toString(arr));
    }
}

如何避免拋出該異常, 使用if 判斷用戶輸入的是否是整數

boolean hasNestInt()

public class Demo1_Scanner {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int [] arr = new int [3];
        // 使用增強型for 遍歷數組
        for(int i = 0 ; i < arr.length ; i++){
            System.out.println("請輸入:");
            if(input.hasNextInt()){ // 用戶輸入的數據是否爲整數
                arr[i] = input.nextInt();
            }else{
                System.out.println("輸入錯誤");
            }

        }

        System.out.println(Arrays.toString(arr));
    }
}

發現問題

如果用戶在該數組的遍歷過程中, 輸入 10,20,a 結果是10,20 正常, a 無法添加到數組

但是先輸入的是 “a” , 程序的運行結果爲

請輸入:
a
輸入錯誤
請輸入:
輸入錯誤
請輸入:
輸入錯誤
[0, 0, 0]

hasNextInt() 只管判斷, 沒有其他操作 導致的結果 a 一直存在於輸入區

需要操作的事情 是一旦發現一個數不是整數, 需要把該數移除

if(input.hasNextInt()){ // 用戶輸入的數據是否爲整數
    arr[i] = input.nextInt();
}else{
    System.out.println("輸入錯誤");
    input.next(); // 無論用戶輸入的是什麼, 都可以清理掉
}

next() 和 nextline() 的區別

public class Demo1_Scanner1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String username;
        String password;

        System.out.println("請輸入用戶名:");  
        username =  input.next();  // 尼古拉斯 志強
        System.out.println("請輸入密碼:");
        password =  input.next();

        System.out.println(username + "\t"+password); // 尼古拉斯	志強
    }

next() 會把空格前後 當成多次輸入 來顯示

nextLine(); 可以把空格當成字符串的一部分來顯示

Random

構造() 構造(參數)區別

// 參數 種子,隨機數要根據種子生成
Random ran1 = new Random();
Random ran2 = new Random();
for(int i = 0 ; i < 5 ; i++){
    System.out.println("ran1: "+ran1.nextInt());
    System.out.println("ran2: "+ran2.nextInt());
}

使用種子, 類似於使用方案, 兩個隨機數類, 如果種子相同, 方案相同, 依次的隨機數也是相同的

nextInt() 和 nextInt(參數)

nextInt() int 的取值範圍內 隨機產生

nextInt(參數) 隨機生成數字, 滿足條件 0 <= 隨機數 < 參數

System

屬性

Sout  ==> 
System.out.println()
System.err.println()

方法

arrayCopy

int [] arr1 = {1,2,3,4,5};
int [] arr2 = new int[10];
System.arraycopy(arr1,3,arr2,0,2);
System.out.println(Arrays.toString(arr2));
src - 源數組。 
srcPos - 源數組中的起始位置。 
dest - 目標數組。 
destPos - 目的地數據中的起始位置。 
length - 要複製的數組元素的數量。 

currentTimeMillis()

返回當前時間(以毫秒爲單位)。 
在1970年1月1日UTC之間的當前時間和午夜之間的差異,以毫秒爲單位。 
long time = System.currentTimeMillis();
System.out.println(time/1000/60/60/24/365);

exit(int status)

終止當前運行的Java虛擬機。 該參數作爲狀態代碼; 按照慣例,非零狀態碼錶示異常終止。

gc()

運行垃圾回收器

程序而言 , 沒有用的

堆內存中, 沒有被變量指向的對象 就是垃圾對象

自動垃圾回收機制

getProperties()

System.out.println(System.getProperties());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章