類和對象的子函數或方法:



馬克-to-win:馬克java社區:實例的子函數(或叫方法)操作實例自己的屬性。類里子函數外的都叫屬性。和原來一樣,fuzhizhangsan就是子函數,現在多了一條就是,在這個子函數里,如果有屬性,比如name,那么這個屬性就是zhang這個對象的,因為是這樣調用的:zhang.fuzhizhangsan("zhang", 96);
例1:

class Student {
    String name;
    int fenshu;
/*賦值就是給值的意思*/
    void fuzhizhangsan(String name1, int fenshu1) {
        name = name1;
        fenshu = fenshu1 ;
    }
}

public class test4 {
    public static void main(String[] args) {
        int a = 0;
        Student zhang = new Student();
        a = 2;
        zhang.fuzhizhangsan("zhang", 96);
        System.out.println(zhang.name);
        System.out.println(zhang.fenshu);
        int b = 0;
        Student yang = new Student();
        b = 5;
        yang.fuzhizhangsan("yang", 97);
        System.out.println(yang.name);
        System.out.println(yang.fenshu);
    }
}
結果:
zhang
96
yang
97

馬克- to-win:馬克 java社區:防盜版實名手機尾號: 73203。
作業1:馬克-to-win:馬克java社區:把上面的類中加一個子函數(方法):dayin(),能把上面類的兩個屬性都打印出來。返回值是void,沒有輸入參數。

class Student2 {
    String name;
    int fenshu;

    void fuzhizhangsan(String name1, int fenshu1) {
        name = name1;
        fenshu = fenshu1;
    }

    void dayin() {
        System.out.println(name + fenshu);
    }
}

public class test9 {
    public static void main(String[] args) {
        int a = 0;
        Student2 zhang = new Student2();
        a = 2;
        zhang.fuzhizhangsan("zhang", 96);
        int b = 0;
        Student2 yang = new Student2();
        b = 5;
        yang.fuzhizhangsan("yang", 97);
        zhang.dayin();
        yang.dayin();
    }

}
結果:
zhang96
yang97


作業2:馬克-to-win:馬克java社區:做一個類名叫LiaShu,他有兩個屬性,int shu1和int shu2,先編一個方法,給他們倆賦值(模仿例1)。再編一個方法void zhaodashu(),找出他們倆之中大的那個數,打印出來。測試Test類中要都調用他們的方法來測試。


class LiaShu {
    int shu1;
    int shu2;

    void fuzhishu(int x, int y) {
        shu1 = x;
        shu2 = y;
    }

    int zhaodashu() {
        int c = 0;
        if (shu1 < shu2) {
            c = shu2;
        }
        if (shu1 > shu2) {
            c = shu1;
        }
        return c;
    }
}

public class test10 {
    public static void main(String[] args) {
        LiaShu si = new LiaShu();      
        si.fuzhishu(43, 27);      
        int k=si.zhaodashu();      
        System.out.println(k);
    }
}
結果:
43






例2:(把上一章的找大數子函數,放在這一章的類里執行)

class Shu {
    int zhaodashu(int x, int y) {
        int c = 0;
        if (x < y) {
            c = y;
        }
        if (x > y) {
            c = x;
        }
        return c;
    }
}

public class test5 {
    public static void main(String[] args) {
        int a = 3;
        int b = 2;
        Shu shuli = new Shu();
        int y = shuli.zhaodashu(a, b);
        System.out.println(y);
    }
}
結果:
3

例3:(把上一章的數組翻倍的例子,放在這一章的類里執行)

class Shu {
    int b[] = { 0, 0, 0, 0, 0 };

    void fuzhi(int[] c) {
        int i = 0;
        // b=new int[c.length];//這種動態,最好
        while (i < c.length) {
            b[i] = c[i];
            i++;
        }
    }

    void fanbei() {
        int i = 0;
        while (i < b.length) {
            b[i] = b[i] * 2;
            i++;
        }
    }

    void dayin() {
        int j = 0;
        while (j < b.length) {
            System.out.println(b[j]);
            j++;
        }
    }
}

public class Test {
    public static void main(String[] args) {
        int a[] = { 7, 4, 3, 2, 5 };
        Shu shuli = new Shu();
        shuli.fuzhi(a);
        shuli.fanbei();
        shuli.dayin();
    }
}


結果:

14
8
6
4
10

作業3:做一個類ShuZu,測試類中有一個4個數的數組,用void fuzhi(a)方法賦值給類ShuZu的屬性,之后用int qiuhe()方法求這4個數的數組的和,在主測試類中打印出和來。



class ShuZu {
    int b[] = { 0, 0, 0, 0 };
    void fuzhi(int[] c) {
        int i = 0;
        b = new int[c.length];
        while (i < c.length) {
            b[i] = c[i];
            i++;
        }
    }

    int qiuhe() {
        int i = 0;
        int d = 0;
        while (i < b.length) {
            d = d + b[i];
            i++;
        }
        return d;
    }
}

public class Test1 {
    public static void main(String[] args) {
        int a[] = { 2, 4, 3, 5, };
        ShuZu number = new ShuZu();
        number.fuzhi(a);
        int h=number.qiuhe();
        System.out.println(h);
    }
}
結果:
14





作業4:做一個類ShuZu,測試類中有一個4個數的數組,用void fuzhi(a)方法賦值給類ShuZu的屬性,之后用int zhaoweizhi()方法求這4個數的數組的最大的數的位置,在主測試類中打印出位置來。參考上章。



class ShuZu2 {
    int b[] = { 0, 0, 0, 0 };

    void fuzhi(int[] c) {
        int i = 0;
        b = new int[c.length];
        while (i < c.length) {
            b[i] = c[i];
            i++;
        }
    }

    int zhaoweizhi() {
        int i = 0;
        int d = b[0];
        int p = 0;
        while (i < b.length) {
            if (b[i] > d) {
                d = b[i];
                p = i;
            }
            i++;
        }
        return p;
    }
}

public class Test2 {
    public static void main(String[] args) {
        int a[] = { 3, 6, 8, 9, };
        ShuZu2 number = new ShuZu2();
        number.fuzhi(a);
        int h = number.zhaoweizhi();
        System.out.println(h);
    }
}
結果:
3


作業5:做一個類Shu,里面有一個屬性int a;測試類中先用void fuzhi(3)方法賦值給類Shu的屬性a,之后用int bidaxiao(4)方法比較輸入參數和屬性a比是大還是小,如果大,則返回1,如果小,則返回-1.如果等,則返回0.測試類中根據返回值,打印比較結果。


class Shu {
    int a;

    void fuzhi1(int x) {
        a = x;
    }

    int bidaxiao(int e) {
        int big = 0;
        if (e > a) {
            big = 1;
        }
        if (e < a) {
            big = -1;
        }
        if (e == a) {
            big = 0;
        }
        return big;

    }
}

public class Test3 {
    public static void main(String[] args) {
        Shu number = new Shu();
        number.fuzhi1(3);
        int b = 4;
        int y = number.bidaxiao(b);

        if (y == 1) {
            System.out.println("大");
        }

        if (y == -1) {
            System.out.println("小");
        }
        if (y == 0) {
            System.out.println("相等");
        }
    }
}

結果:



作業6:做一個類Shu,里面有一個屬性int a;測試類中先用void fuzhi(3)方法賦值給類Shu的屬性a,之后用String bidaxiao(4)方法比較輸入參數和屬性a比是大還是小,如果大,則返回"大",如果小,則返回"小".如果等,則返回"相等".測試類中直接打印比較結果。


class Shu1 {
    int a;

    void fuzhi1(int t) {
        a = t;
    }

    String bidaxiao(int f) {
        String big = "";
        if (f > a) {
            big="大";
        }
        if (f < a) {
            big="小";
        }
        if (f == a) {
            big="相等";
        }
        return big;

    }
}

public class Test4 {
    public static void main(String[] args) {
        Shu1 number = new Shu1();
        number.fuzhi1(3);
        int b = 4;
        String y = number.bidaxiao(b);
        System.out.println(y);

    }
}


結果:



作業7:做一個類DengLu,里面有兩個屬性String yonghuming;String mima;測試類中先用void zhuce("zhangsan","123")方法賦值給類DengLu的兩個屬性,之后用String denglu("zhangsan","123")和String denglu("zhangsan","1234")方法比較輸入參數和屬性是否相等,如果相等,則返回登錄成功,如果不等,則返回登錄失敗.測試類中打印結果。提示:字符串相等與否用yonghuming.equals(a),如果返回值是真,則yonghuming和a這兩個字符串是相等的。


class DengLu {
    String yonghuming;
    String mima;

    void zhuce(String a, String y) {
        yonghuming = a;
        mima = y;
    }

    String denglu(String o, String p) {
        String big = "";
        if (yonghuming.equals(o) && mima.equals(p))

        {
            big = "登錄成功";
        }
      
        else {
            big = "登錄失敗";
        }
        return big;

    }
}

public class Test5 {
    public static void main(String[] args) {
        DengLu zifu = new DengLu();
        zifu.zhuce("zhangsan", "123");
        String b = "zhangsan";
        String v = "1234";      
        String y = zifu.denglu(b, v);      
        System.out.println(y);
        String g = "zhangsan";
        String t = "123";      
        String m=zifu.denglu(g, t);      
        System.out.println(m);
      
    }

}
結果:
登錄失敗
登錄成功