1. 程式人生 > >Java用類實現結構體的功能

Java用類實現結構體的功能

我們都知道C/C++裡面的結構體在儲存資料的時候很方便,但是在Java中沒有Struct,但是我們可以用類來實現Struct的功能

與Struct宣告功能一樣的類的定義:

public  class platform
{
    private int x;//平臺左端的位置
    private int y;//平臺右邊的位置
    private int h;//平臺離地面的高度
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getH() {
        return h;
    }
    public void setH(int h) {
        this.h = h;
    }
    
}

使用上面類的類:

public class Test10_4 {

    private static final int INFINITE = 1000000000;// 對於該題相當於無窮大
    private static int n;// 平臺的個數
    private static platform[] pf ;// 儲存所有平臺
    private static int max;// 能降落的最大高度
    private static int[] aLeftMinTime;// 儲存從每個平臺左端到地面的最短時間
    private static int[] aRightMinTime;// 儲存從每個平臺右端到地面的最短時間

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        pf=new platform[n+1];
        
        /* 開始的位置也看做是一個平臺*/
        int m = sc.nextInt();
        platform p=new platform();//在給pf[]陣列賦值的時候,需要先宣告一個物件,然後再將物件賦值給陣列,否則訪問陣列時會出現空指標錯誤。
        p.setX(m);
        p.setY(m);
        p.setH(sc.nextInt());
        pf[0]=p;
        max = sc.nextInt();
        
        // 給各個平臺的資料賦值
        for (int i = 1; i <= n; i++) {
            p=new platform();
            p.setX(sc.nextInt());
            p.setY(sc.nextInt());
            p.setH(sc.nextInt());
            pf[i]=p;
        }
    }

}

上面的類分別對三個私有變數(private)寫了set和get方法,在訪問私有變數的時候可以呼叫函式來訪問,同時如果將變數宣告為公有變數(public)不用為公有變數寫set和get方法,直接用物件.變數名來訪問即可。

該類可以寫在用到該類的類的外邊,也可以寫成內部類。

import java.util.*;

public class Test10_4 {

    public  class platform
    {
        public int x;//平臺左端的位置
        private int y;//平臺右邊的位置
        private int h;//平臺離地面的高度
        
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
        public int getH() {
            return h;
        }
        public void setH(int h) {
            this.h = h;
        }
    }
    
    private static final int INFINITE = 1000000000;// 對於該題相當於無窮大
    private static int n;// 平臺的個數
    private static platform[] pf ;// 儲存所有平臺
    private static int max;// 能降落的最大高度
    private static int[] aLeftMinTime;// 儲存從每個平臺左端到地面的最短時間
    private static int[] aRightMinTime;// 儲存從每個平臺右端到地面的最短時間


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        pf=new platform[n+1];
        
        /* 開始的位置也看做是一個平臺*/
        int m = sc.nextInt();

        //內部類物件的生成方法
        Test10_4 t=new Test10_4();
        Test10_4.platform p=t.new platform();
        p.setX(m);
        p.setY(m);
        p.setH(sc.nextInt());
        pf[0]=p;
        max = sc.nextInt();
        
        // 給各個平臺的資料賦值
        for (int i = 1; i <= n; i++) {
            p=t.new platform();
            p.setX(sc.nextInt());
            p.setY(sc.nextInt());
            p.setH(sc.nextInt());
            pf[i]=p;
        }
    }

}

注意:內部類物件的生成方法