1. 程式人生 > 實用技巧 >面向物件(上)--2

面向物件(上)--2

一、記憶體解析

  1、物件陣列的記憶體解析

  2、形參記憶體解析

    1.形參是基本資料型別

    2.形參是引用資料型別

二、練習題

  1、方法的過載

package com.xuanxiao.object;

/*
 * 1.編寫程式,宣告一個method方法,在方法列印一個10*8 的 * 形矩陣,在main方法中呼叫該方法
 * 2.修改上一個程式,在method方法,除方法列印一個10*8 的 * 形矩陣,在計算該矩陣的面積
 * 並將其最為方法返回值,在main方法中呼叫該方法,接受返回的面積並列印輸出
 * 
 * 3.修改上一個程式,在method方法中提供 m n 兩個引數,方法中列印一個m*n的*型面積
 * 並計算該矩形的面積,將其作為方法的返回值,在main方法中呼叫該方法,接收返回的面積值並列印。
 
*/ public class ExerTest { public static void main(String[] args) { ExerTest test = new ExerTest(); test.method(12,11); System.out.println(test.method(12,11)); } // 1. public void method() { for(int i =0;i<10;i++) { for(int j = 0;j<8;j++) { System.out.print(
"*"); } System.out.println(); } double area = 10 * 8; System.out.println("面積為:" + area); } // 2. public int method() { for(int i =0;i<10;i++) { for(int j = 0;j<8;j++) { System.out.print("*"); } System.out.println(); }
return 10 * 8; } // 3. public int method(int m,int n) { for(int i =0;i<m;i++) { for(int j = 0;j<n;j++) { System.out.print("*"); } System.out.println(); } return m * n; } }

  2、求圓的面積

package com.xuanxiao.object;

//求圓的面積
public class CircleTest {
    public static void main(String[] args) {
        Circle c = new Circle();
        c.radius = 2;
        double area = c.findArea();
        System.out.println("圓的面積是:" + area);
    }
}

class Circle {
    double radius;
    
    public double findArea() {
        double area = Math.PI * radius * radius;
        return area;
    }
}

  3、同一個類中不同物件的關係

package com.xuanxiao.object;

/*
 * 要求:
 * (1)建立Person類的物件,設定該物件的name,age和sex屬性,呼叫study方法
 * 輸出字串“studying”,吊桶showAge方法顯示age值。
 * 條用addAge方法給物件的age屬性值加2歲。
 * (2)建立第二個物件,體會同一個類中不同物件之間的關係。
 */
public class PersonTest1 {
    public static void main(String[] args) {
        Person1 p1 = new Person1();
        p1.name = "Tom";
        p1.age = 18;
        p1.sex = 1;
        p1.study();
        p1.showAge();
        int newAge = p1.addAge(2);
        System.out.println("新年齡是:" + newAge);
        
        Person1 p2 = new Person1();
        p2.showAge();
    }
}
package com.xuanxiao.object;

public class Person1 {
    String name;
    int age;
    int sex;
    
    public void study() {
        System.out.println("Studying");
    }
    
    public void showAge() {
        System.out.println("age:" + age);
    }
    
    public int addAge(int i) {
        age = age + i;
        return age;
    }
}

  4、屬性方法的使用

package com.xuanxiao.object;

public class PersonTest {
    public static void main(String[] args) {
        //2.建立類的物件
        Person p1 = new Person();
        //3.呼叫物件的結構
        p1.name = "麗";
        System.out.println(p1.name);
        p1.inMale = false;
        p1.eat();
        p1.sleep();
        p1.talk("chinese");
        System.out.println("************");
        Person p2 = new Person();
        p2.name = "小明";
        System.out.println(p2.name);
        System.out.println("************");
        //p3 和 p1 指向同一個地址
        Person p3 = p1;
        System.out.println(p3.name);
    }
}

//1.建立類,設計類的成員
class Person {
    String name;
    int age;
    boolean inMale;
    
    public void eat() {
        System.out.println("人可以吃飯");
    }
    
    public void sleep() {
        System.out.println("人可以睡覺");
    }
    
    public void talk(String language) {
        System.out.println("人可以說話,使用的是:" + language);
    }
}

  5、物件陣列

package com.xuanxiao.object;

/*
 * 定義一個Student類,包含三個屬性,學號number(int),年級state(int),成績score(int)
 * 建立20個學生物件,學號為1到20,年級和成績都由隨機數確定。
 * (1)打印出三年級(state = 3)的學生資訊。
 * (2)使用氣泡排序按照學生成績排序,並遍歷所有學生資訊。
 */
public class StudentTest {
    public static void main(String[] args) {
        
        Student[] stus = new Student[20];
        for(int i = 0;i < stus.length;i++ ) {
            stus[i] = new Student();
            stus[i].number = i+1;
            stus[i].state = (int)(Math.random() * (6-1+1) + 1);
            stus[i].score = (int)(Math.random() * ((100-0) + 1));
        }
        for(int i = 0;i<stus.length;i++) {
            System.out.println("學號:" + stus[i].number +"\t年級:" + stus[i].state + "\t成績:" + stus[i].score);
        }
        System.out.println("**********************************");
        
        
        for(int i = 0;i<stus.length;i++) {
            if(stus[i].state == 3) {
                System.out.println("學號:" + stus[i].number +"\t年級:" + stus[i].state + "\t成績:" + stus[i].score);
            }
        }
        System.out.println("**********************************");
        
        
        for(int i = 0;i<stus.length-1;i++) {
            for(int j = 0;j<stus.length-1-i;j++) {
                if (stus[j].score > stus[j+1].score) {
                    Student temp = stus[j];
                    stus[j] = stus[j + 1];
                    stus[j+1] = temp;
                }
            }
        }
        for(int i = 0; i < stus.length; i++) {
            //System.out.println("學號:" + stus[i].number + "\t年級:" + stus[i].state + "\t成績:" + stus[i].score);
            System.out.println(stus[i].printer());
        }
    }
}

class Student {
    int number;
    int state;
    int score;
    
    public String printer() {
        return "學號:" + number + "\t年級:" + state + "\t成績:" + score;
    }
}

  6、遞迴

package com.xuanxiaio.zhaoyi1;


/*
 * 1.用Java語言寫出遞迴求階乘的演算法。
 * 2.已知有一個數列,f(0)=1,f(1)=4,f(n+2)=2*f(n+1)+f(n),其中n是大於0的整數,求f(10)的值。
 * 3.已知一個數列,f(20)=1,f(21)=4,f(n+2)=2*f(n+1)+f(n),其中n是大於0的整數,求f(10)的值。
 * 4.輸入一個數據n,計算斐波那契數列的第n個值
 * 1 1 2 3 5 8 13 21 34 55
 * 要求:計算斐波那契數列的第n個值,並將整個數列打印出來。
 */

public class Recursion {
    
    public static void main(String[] args) {
        Recursion test = new Recursion();
        
        System.out.println("測試結果1為:" + test.factorial(10));
        
        int word = test.getnext1(10);
        System.out.println("測試結果2為:" + word);
        
        System.out.println("測試結果3為:" + test.getnext2(10));
        
        System.out.println("測試結果4為:" + test.Fibonacci(10));
    }
    
    //1.用Java語言寫出遞迴求階乘的演算法。
    public int factorial(int n) {
        if(n == 1) {
            return 1;
        }else {
            return n * factorial(n - 1);
        }
    }
    
    
    //2.已知有一個數列,f(0)=1,f(1)=4,f(n+2)=2*f(n+1)+f(n),其中n是大於0的整數,求f(10)的值。
    public int getnext1(int n) {
        if(n == 0) {
            return 1;
        }else if(n == 1) {
            return 4;
        }else {
            return 2*getnext1(n-1) + getnext1(n-2);
        }
    }
    
    
    //3.已知一個數列,f(20)=1,f(21)=4,f(n+2)=2*f(n+1)+f(n),其中n是大於0的整數,求f(10)的值。
    public int getnext2(int n) {
        if(n == 20) {
            return 1;
        }else if(n == 21) {
            return 4;
        }else {
            return getnext1(n+2) - 2*getnext1(n+1);
        }
    }
    
    /*
     * 4.輸入一個數據n,計算斐波那契數列的第n個值
     * 1 1 2 3 5 8 13 21 34 55
     * 要求:計算斐波那契數列的第n個值,並將整個數列打印出來。
     */
    public int Fibonacci(int n) {
        if(n == 1) {
            return 1;
        }else if(n == 2) {
            return 1;
        }else {
            return Fibonacci(n-1) + Fibonacci(n-2);
        }
    }
    
}