类和对象的子函数或方法:



马克-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);
      
    }

}
结果:
登录失败
登录成功