最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java面向?qū)ο蠡A(chǔ)實例詳解

 更新時間:2026年06月02日 09:06:40   作者:在繁華處  
本篇詳細(xì)介紹了類與對象的概念、構(gòu)造函數(shù)的使用、this關(guān)鍵字的作用以及封裝和static關(guān)鍵字的用法,幫助初學(xué)者快速掌握面向?qū)ο缶幊痰暮诵乃枷?感興趣的朋友一起看看吧

面向?qū)ο缶幊淌荍ava的靈魂,理解類與對象就是理解Java的世界觀。

1. 面向?qū)ο缶幊趟枷?/h2>

1.1 什么是面向?qū)ο螅?/h3>

面向?qū)ο笫且环N編程思想,它將現(xiàn)實世界的事物抽象為程序中的對象。每個對象都有:

  • 屬性(Attribute):描述對象的狀態(tài)
  • 方法(Method):描述對象的行為

類比理解:

  • 類(Class):相當(dāng)于"圖紙"或"模板",定義了對象應(yīng)該有什么屬性和方法
  • 對象(Object):相當(dāng)于"實物",根據(jù)圖紙創(chuàng)建出來的具體實例

舉例:

  • "人"是一個類,"張三"是一個對象
  • "汽車"是一個類,"我的車"是一個對象
  • "銀行賬戶"是一個類,"我的儲蓄賬戶"是一個對象

2. 定義類

2.1 類的基本結(jié)構(gòu)

public class Person {
    // 屬性(成員變量)
    String name;
    int age;
    String gender;
    // 方法(成員方法)
    void introduce() {
        System.out.println("大家好,我叫" + name + ",今年" + age + "歲。");
    }
    void eat() {
        System.out.println(name + "正在吃飯。");
    }
    void sleep() {
        System.out.println(name + "正在睡覺。");
    }
}

2.2 成員變量 vs 局部變量

public class VariableScope {
    // 成員變量:定義在類中,整個類都能訪問
    String memberVar = "我是成員變量";
    void testMethod() {
        // 局部變量:定義在方法中,只在方法內(nèi)有效
        String localVar = "我是局部變量";
        System.out.println(memberVar);  // 可以訪問
        System.out.println(localVar);   // 可以訪問
    }
    void anotherMethod() {
        System.out.println(memberVar);  // 可以訪問
        // System.out.println(localVar);  // 編譯錯誤!無法訪問
    }
}

3. 創(chuàng)建和使用對象

3.1 實例化對象

使用 new 關(guān)鍵字創(chuàng)建對象:

public class ObjectDemo {
    public static void main(String[] args) {
        // 創(chuàng)建Person對象
        Person person1 = new Person();
        person1.name = "張三";
        person1.age = 20;
        person1.gender = "男";
        // 調(diào)用對象的方法
        person1.introduce();  // 大家好,我叫張三,今年20歲。
        person1.eat();        // 張三正在吃飯。
        // 創(chuàng)建第二個對象
        Person person2 = new Person();
        person2.name = "李四";
        person2.age = 22;
        person2.gender = "女";
        person2.introduce();  // 大家好,我叫李四,今年22歲。
    }
}

4. 構(gòu)造函數(shù)

構(gòu)造函數(shù)是對象創(chuàng)建時自動調(diào)用的特殊方法,用于初始化對象。

4.1 默認(rèn)構(gòu)造函數(shù)

如果不寫構(gòu)造函數(shù),Java會自動提供一個無參構(gòu)造函數(shù):

public class Student {
    String name;
    int age;
    // 如果不寫任何構(gòu)造函數(shù),Java自動提供:
    // public Student() {}
}

4.2 自定義構(gòu)造函數(shù)

public class Student {
    String name;
    int age;
    String school;
    // 無參構(gòu)造函數(shù)
    public Student() {
        System.out.println("創(chuàng)建了一個學(xué)生對象");
    }
    // 帶參數(shù)的構(gòu)造函數(shù)
    public Student(String name, int age, String school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }
    // 部分參數(shù)的構(gòu)造函數(shù)
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        this.school = "未知學(xué)校";
    }
    void introduce() {
        System.out.println("我叫" + name + ",今年" + age + "歲,在" + school + "上學(xué)。");
    }
}

4.3 構(gòu)造函數(shù)重載

同一個類可以有多個構(gòu)造函數(shù),參數(shù)不同:

public class ConstructorOverloadDemo {
    public static void main(String[] args) {
        // 使用無參構(gòu)造函數(shù)
        Student s1 = new Student();
        s1.introduce();  // 輸出默認(rèn)值
        // 使用全參構(gòu)造函數(shù)
        Student s2 = new Student("張三", 18, "北京大學(xué)");
        s2.introduce();  // 張三的介紹
        // 使用部分參數(shù)構(gòu)造函數(shù)
        Student s3 = new Student("李四", 20);
        s3.introduce();  // 李四的介紹,學(xué)校為"未知學(xué)校"
    }
}

5. this關(guān)鍵字

this 關(guān)鍵字指向當(dāng)前對象:

5.1 區(qū)分成員變量和局部變量

public class Person {
    String name;
    int age;
    public Person(String name, int age) {
        // this.name 是成員變量
        // name 是局部變量(參數(shù))
        this.name = name;
        this.age = age;
    }
    public void setName(String name) {
        this.name = name;  // 區(qū)分成員變量和參數(shù)
    }
    public String getName() {
        return this.name;
    }
}

6. 封裝

封裝是面向?qū)ο蟮暮诵奶匦灾?,隱藏內(nèi)部實現(xiàn),只暴露必要的接口。

6.1 訪問修飾符

修飾符同類同包子類其他
public????
protected????
默認(rèn)(包訪問)????
private????

6.2 使用private封裝屬性

public class BankAccount {
    private String accountNumber;
    private double balance;
    private String owner;
    public BankAccount(String accountNumber, String owner, double initialBalance) {
        this.accountNumber = accountNumber;
        this.owner = owner;
        this.balance = initialBalance;
    }
    // getter方法
    public String getAccountNumber() {
        return accountNumber;
    }
    public double getBalance() {
        return balance;
    }
    // setter方法(帶驗證)
    public boolean deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("存款成功,余額:" + balance);
            return true;
        } else {
            System.out.println("存款金額必須大于0");
            return false;
        }
    }
    public boolean withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            System.out.println("取款成功,余額:" + balance);
            return true;
        } else {
            System.out.println("取款金額無效或余額不足");
            return false;
        }
    }
}

7. static關(guān)鍵字

static 表示"屬于類"而非"屬于對象"。

7.1 靜態(tài)變量

所有對象共享同一個靜態(tài)變量:

public class Student {
    String name;
    static String school;  // 靜態(tài)變量,所有學(xué)生共享
    public Student(String name) {
        this.name = name;
    }
}
public class StaticDemo {
    public static void main(String[] args) {
        Student.school = "北京大學(xué)";  // 通過類名訪問
        Student s1 = new Student("張三");
        Student s2 = new Student("李四");
        System.out.println(s1.name + "的學(xué)校:" + s1.school);  // 北京大學(xué)
        System.out.println(s2.name + "的學(xué)校:" + s2.school);  // 北京大學(xué)
        // 修改靜態(tài)變量,所有對象都受影響
        Student.school = "清華大學(xué)";
        System.out.println(s1.name + "的學(xué)校:" + s1.school);  // 清華大學(xué)
    }
}

7.2 靜態(tài)方法

不能訪問非靜態(tài)成員,只能訪問靜態(tài)成員:

public class MathUtils {
    // 靜態(tài)變量
    static final double PI = 3.14159265;
    // 靜態(tài)方法
    public static int add(int a, int b) {
        return a + b;
    }
    public static double circleArea(double radius) {
        return PI * radius * radius;
    }
}
public class StaticMethodDemo {
    public static void main(String[] args) {
        // 通過類名調(diào)用靜態(tài)方法
        int sum = MathUtils.add(10, 20);
        System.out.println("10 + 20 = " + sum);
        double area = MathUtils.circleArea(5);
        System.out.println("圓的面積:" + area);
    }
}

8. 實戰(zhàn)案例

8.1 設(shè)計一個學(xué)生管理系統(tǒng)

public class StudentManager {
    private String[] names;
    private int[] scores;
    private int count;
    private int capacity;
    public StudentManager(int capacity) {
        this.capacity = capacity;
        names = new String[capacity];
        scores = new int[capacity];
        count = 0;
    }
    public boolean addStudent(String name, int score) {
        if (count >= capacity) {
            System.out.println("學(xué)生數(shù)量已滿");
            return false;
        }
        if (score < 0 || score > 100) {
            System.out.println("成績無效");
            return false;
        }
        names[count] = name;
        scores[count] = score;
        count++;
        return true;
    }
    public void printAll() {
        System.out.println("=== 學(xué)生列表 ===");
        for (int i = 0; i < count; i++) {
            System.out.println(names[i] + ":" + scores[i] + "分");
        }
    }
    public double getAverage() {
        if (count == 0) return 0;
        int sum = 0;
        for (int i = 0; i < count; i++) {
            sum += scores[i];
        }
        return (double) sum / count;
    }
}

9. 總結(jié)

本篇我們學(xué)習(xí)了:

? 面向?qū)ο笏枷?/strong>:類與對象的概念
? 類的定義:屬性、方法、構(gòu)造函數(shù)
? 對象的創(chuàng)建:new關(guān)鍵字、內(nèi)存存儲
? 構(gòu)造函數(shù):初始化對象、重載
? this關(guān)鍵字:指向當(dāng)前對象
? 封裝:訪問修飾符、getter/setter
? static關(guān)鍵字:靜態(tài)變量、方法、代碼塊

核心要點:

  1. 類是模板,對象是實例
  2. 構(gòu)造函數(shù)用于初始化對象
  3. 封裝保護數(shù)據(jù)安全
  4. static屬于類而非對象

下一篇預(yù)告: 《Java從零到熟練(五):面向?qū)ο筮M(jìn)階》

  • 學(xué)習(xí)繼承和多態(tài)
  • 理解接口和抽象類
  • 掌握面向?qū)ο蟮母呒壧匦?/li>

參考資源

  1. Java語言規(guī)范 - 類
  2. Java教程 - 面向?qū)ο缶幊谈拍?/a>
  3. 廖雪峰Java教程 - 面向?qū)ο?/a>

到此這篇關(guān)于Java面向?qū)ο蠡A(chǔ)實例詳解的文章就介紹到這了,更多相關(guān)Java面向?qū)ο蠡A(chǔ)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IDEA新建Springboot項目(圖文教程)

    IDEA新建Springboot項目(圖文教程)

    下面小編就為大家?guī)硪黄狪DEA新建Springboot項目(圖文教程)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot框架DataSource多數(shù)據(jù)源配置方式

    SpringBoot框架DataSource多數(shù)據(jù)源配置方式

    這篇文章主要介紹了SpringBoot框架DataSource多數(shù)據(jù)源配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 非常詳細(xì)的Java異常處理機制知識整理大全

    非常詳細(xì)的Java異常處理機制知識整理大全

    Java異常指在程序運行時可能出現(xiàn)的一些錯誤,比如試圖打開一個根本不存在的文件等,異常處理將會改變程序的控制流程,讓程序有機會對錯誤做出處理,下面這篇文章主要給大家介紹了關(guān)于Java異常處理機制知識整理的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 使用FormData上傳二進(jìn)制文件、對象、對象數(shù)組方式

    使用FormData上傳二進(jìn)制文件、對象、對象數(shù)組方式

    這篇文章主要介紹了使用FormData上傳二進(jìn)制文件、對象、對象數(shù)組方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java查詢緩存之優(yōu)化重復(fù)查詢的執(zhí)行效率問題

    Java查詢緩存之優(yōu)化重復(fù)查詢的執(zhí)行效率問題

    這篇文章主要介紹了Java查詢緩存之優(yōu)化重復(fù)查詢的執(zhí)行效率問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Java爬蟲抓取視頻網(wǎng)站下載鏈接

    Java爬蟲抓取視頻網(wǎng)站下載鏈接

    本文是通過JAVA獲取優(yōu)酷、土豆、酷6、6間房等視頻,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • Springboot 1.5.7整合Kafka-client代碼示例

    Springboot 1.5.7整合Kafka-client代碼示例

    這篇文章主要介紹了Springboot 1.5.7整合Kafka-client代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • java多線程消息隊列的實現(xiàn)代碼

    java多線程消息隊列的實現(xiàn)代碼

    本篇文章主要介紹了java多線程消息隊列的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java定時任務(wù)ScheduledThreadPoolExecutor示例詳解

    Java定時任務(wù)ScheduledThreadPoolExecutor示例詳解

    這篇文章主要介紹了Java定時任務(wù)ScheduledThreadPoolExecutor示例詳解,這里使用scheduleAtFixedRate方法安排一個任務(wù),該任務(wù)是一個 Runnable 匿名類,其run方法中調(diào)用了new LoginViewTimeTask().loginStatisticsHandle()方法,需要的朋友可以參考下
    2023-11-11
  • Java8新特性之StampedLock_動力節(jié)點Java學(xué)院整理

    Java8新特性之StampedLock_動力節(jié)點Java學(xué)院整理

    本文從synchronized、Lock到Java8新增的StampedLock進(jìn)行對比分析,對Java8新特性之StampedLock相關(guān)知識感興趣的朋友一起看看吧
    2017-06-06

最新評論

团风县| 双辽市| 湘西| 高平市| 新干县| 湖南省| 两当县| 西贡区| 梅河口市| 永修县| 湖州市| 密山市| 古交市| 民丰县| 新津县| 镇赉县| 哈巴河县| 阿图什市| 阜新| 宁德市| 敦化市| 左贡县| 漯河市| 中阳县| 确山县| 抚顺县| 宜君县| 阳山县| 山西省| 烟台市| 葫芦岛市| 达孜县| 金华市| 镇宁| 泰兴市| 嘉祥县| 柞水县| 蚌埠市| 东兰县| 旅游| 科技|