Java 程式設計 -- 物件導向中的封裝特性

Java 程式

簡介

運算式

分枝

迴圈

陣列

函數

遞迴

錯誤處理

物件導向

封裝

繼承

多型

技巧

函式庫

字串

數學

正規表達式

容器

檔案

網路

資料庫

視窗

Thread

Listener

錯誤陷阱

相關檔案

相關資源

教學錄影

Eclipse

考題解答

訊息

相關網站

參考文獻

最新修改

簡體版

English

範例一:人的體重

class Object1 {
    public static void main(String args[]) {
        Person1 p1, p2;
        p1 = new Person1("大雄", 50);
        p2 = new Person1("胖虎", 80);
        p1.checkWeight();
        p2.checkWeight();
        p2.weight = 68;
        p1.checkWeight();
        p2.checkWeight();
    }
}
 
class Person1 {
    String name;
    int weight;
 
    Person1(String pName, int pWeight) {
        name   = pName;
        weight = pWeight;
    }
 
    void checkWeight() {
        System.out.print(name+"體重 "+weight+" 公斤,");
        if (weight < 70) 
            System.out.println("很苗條!");
        else
            System.out.println("很穩重!");
    }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License