1. 程式人生 > 其它 >|NO.Z.00020|——————————|BigDataEnd|——|Java&方法封裝.V02|---------------------------------------------|Java.v02|point類.v01|定義|

|NO.Z.00020|——————————|BigDataEnd|——|Java&方法封裝.V02|---------------------------------------------|Java.v02|point類.v01|定義|



[BigDataJava:Java&方法封裝.V02]                                                                             [BigDataJava.面向物件] [|章節二|方法和封裝|point類的定義|]








一、point類的定義
### --- 案例題目

~~~     ——>        程式設計實現Point類的定義並向Point類新增構造方法
~~~     ——>        Point() 預設建立原點物件
~~~     ——>        Point(inti, intj) 根據引數建立點物件
二、程式設計程式碼
### --- 程式設計程式碼

/*
    程式設計實現Point類的定義
 */
public class Point {
    
    int x; // 用於描述橫座標的成員變數
    int y; // 用於描述縱座標的成員變數
    
    // 自定義無參構造方法
    Point() {}
    // 自定義有參構造方法
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }   
    
    // 自定義成員方法實現特徵的列印
    void show() {
        System.out.println("橫座標是:" + x + ",縱座標是:" + y);
    }
    
    public static void main(String[] args) {
        
        // 1.使用無參方式構造物件並列印特徵
        Point p1 = new Point();
        p1.show(); // 0 0
        
        // 2.使用有參方式構造物件並列印特徵
        Point p2 = new Point(3, 5);
        p2.show(); // 3 5
    }
}
三、編譯列印
### --- 編譯

C:\Users\Administrator\Desktop>javac Point.java
### --- 列印輸出

C:\Users\Administrator\Desktop>java Point
橫座標是:0,縱座標是:0
橫座標是:3,縱座標是:5








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)