1. 程式人生 > >Java中封裝

Java中封裝

Java中的封裝

什麼是封裝:將類的某些資訊隱藏在內部同時提供訪問介面

例子:ATM機對鈔票的隱藏,但是提供了介面

實現封裝的步驟:

1.修改屬性的可見性:將變數設為private

2.建立getter/setter方法:設為public用於屬性的讀取

3.在getter/setter方法中對屬性值進行判斷

例子:建立一個學生類,類中將學生的姓名、性別和年齡設為隱藏屬性,同時提供對這些隱藏屬性的介面

public class Student{
    private String name;
    private String gender;
    private int age;

    public
Student(){ } public Student(String name, String gender, int age){ this.setName(name); this.setGender(gender); this.setAge(age); } public void setName(String name){ this.name = name; } public String getName(){ return this.name; }
public void setGender(String gender){ // 在setter方法中判斷傳遞過來的性別是否正確,如果不正確就將性別預設設定為男性 if(gender.equals("男") || gender.equals("女")) this.gender = gender; else this.gender = "男"; } public String getGender(){ return this.gender; } public
void setAge(int age){ // 對傳遞進來的年齡進行判斷 if(age<=0 || age >=120) System.out.println("年齡必須在1-120範圍內!"); else this.age = age; } public int getAge(){ return this.age; } }

Static的用法

static修飾的成員通常被稱為靜態成員或者類成員,在類首次載入時產生,類徹底消亡時釋放。

static + 屬性:靜態屬性(類屬性)

static + 方法:靜態方法(類方法)

static修飾的屬性或者方法的訪問方式有兩種:

1.通過`類名.屬性`或者`類名.方法`來訪問

2.通過`例項物件.屬性`或者`例項物件.方法`訪問

下面的程式碼需要格外注意! Student類:

public class Student{
    private static int age;

    public void setAge(int age){
        // 沒做age的檢查
        this.age = age;
    }

    public int getAge(){
        return this.age;
    }

    public String studentInfo(){
        String str = "學生的年齡為:" + this.getAge();
        return str;
    }
}

StudentTest類:

public class StudentTest{
    public static void main(String[] args){
        // 測試Student 類
        Student stu1 = new Student();
        stu1.setAge(20);
        System.out.println(stu1.studentInfo());

        Student stu2 = new Student();
        stu2.setAge(30);
        System.out.println(stu2.studentInfo());

        System.out.println("========================================");

        System.out.println(stu1.studentInfo());

    }
}

執行後輸出的結果如下: 在這裡插入圖片描述

為什麼明明給stu1的年齡賦值為20,stu2的年齡賦值為30,到最後stu1的年齡也變了呢?這是因為用static修飾的成員共用一塊記憶體空間,所以stu1的age和stu2的age實際上用的是同一塊空間,當stu2設定age時會將stu1設定的age給覆蓋掉!

注意:

1.static不能新增在class前,比如`public static class Student`這樣的寫法是錯誤的

2.static不能修飾方法內的變數

程式碼塊:在{}中間的程式碼塊

程式碼塊的分類:

  • 普通程式碼塊:在方法中定義,順序執行,先出現先執行 Student類:
public class Student{
    public void info(){
        //程式碼塊1
        {
            // 程式碼塊2
            int temp = 1;
            System.out.println("程式碼塊2中的temp=" + temp);
        }
        {
            // 程式碼塊3
            int temp = 2
            System.out.println("程式碼塊3中的temp=" + temp);        
        }

    }
}

StudentTest類:

public class StudentTest{
    public static void main(String[] args){
        Student stu1 = new Student();
        stu1.info();
    }
}

需要注意的是:普通程式碼塊定義的變數會在這一程式碼塊執行完成後銷燬,而包含普通程式碼塊的方法中定義的變數的作用域是從定義開始到方法結束。

所以下面的程式碼會報錯:

public class Student{
    public void info(){
        //程式碼塊1
        int temp = 3;
        {
            // 程式碼塊2
            int temp = 1;
            System.out.println("程式碼塊2中的temp=" + temp);
        }
        {
            // 程式碼塊3
            int temp = 2
            System.out.println("程式碼塊3中的temp=" + temp);        
        }

    }
}
  • 構造程式碼塊:在類中定義,比構造法方法先執行,並且沒例項化一個物件都要執行一次 Student類:
public class Student{
    public Student(){
        System.out.println("我是構造方法");
    }

    {
        System.out.println("我是構造程式碼塊");
    }
}

StudentTest類:

public class StudentTest{
    public static void main(String[] args){
        Student stu1 = new Student();

        Student stu2 = new Student();
    }
}

可以看到上面程式碼的執行結果為: 在這裡插入圖片描述

  • 靜態程式碼塊:在構造程式碼塊前面加上static關鍵字修飾的程式碼塊,在類載入時呼叫,優先於構造程式碼塊,且無論例項化多少個物件只執行一次 Student類:
public class Student{
    public Student(){
        System.out.println("我是構造方法");
    }

    {
        System.out.println("我是構造程式碼塊");
    }

    static{
        System.out.println("我是靜態程式碼塊");
    }
}

StudentTest類

public class StudentTest{
    public static void main(String[] args){
        Student stu1 = new Student();

        Student stu2 = new Student();
    }
}

執行結果如下: 在這裡插入圖片描述

Java中的包管理

Java中包的作用:

1.管理java檔案

2.解決同名檔案的衝突

注意事項:

1.Java中一個包內不能存在同名的類

2.package語句必須位於原始檔的第一行,且只能由一個package語句

3.每個包記憶體放單一功能的類

4.使用不同包中的類時需要使用import語句匯入

5.載入類的順序和import 語句的位置無關

6.import 包名.*只能訪問該包下直接的類,無法訪問該包的子包中的類

Java中常用的包:

1.java.lang包:包含java語言基礎的類,預設匯入。如System、String、Math

2.java.util包:常用工具包,如Scanner、Random

3.java.io包:包含輸入、輸出相關功能的類,如File、InputStream