第四周Java ppt作业

发布时间 2023-09-27 21:57:55作者: 牟兆迪
package Java作业;
class father{
    private int money;
    private int getMoney() {
        return money;
    }
    private void setMoney(int money) {
        this.money = money;
    }
}
class son extends father{
    private int candy;
}
public class test01 {
    public static void main(String[] args) {
        son s = new son();
        s.setMoney(1000);
        System.out.println(s.getMoney());
    }
}//子类中不能用父类中private
////////////////////////////////////////////////////////

package 数据结构;


class InitializeBlockClass{
    {
        field = 200;
    }
    public int field = 100;
    public InitializeBlockClass(int value){
        this.field = value;
    }
    public InitializeBlockClass(){}
}
public class test01{

    public static void main(String[] args) {
        InitializeBlockClass obj = new InitializeBlockClass();
        System.out.println(obj.field);//100

        obj = new InitializeBlockClass(300);
        System.out.println(obj.field);//300
    }
}
//若有重载的构造函数,对于类中属性的赋值主要看构造方法,调用哪个构造方法就会给属性赋构造方法中相应的值


///////////////////////////////////////////////////////////////////////
//通过将实例传递给静态方法:
//在静态方法中,可以将类的实例作为参数传递给静态方法,然后使用该实例来访问实例成员。这种方法需要在调用静态方法时传递一个实例作为参数。

package Java作业;

class father{
    int money;

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
public class test01{
    static void test(father f){
        int n = 1000000;
        f.setMoney(n+10);
    }

    public static void main(String[] args) {
        father f = new father();
        test(f);
        System.out.println(f.getMoney());
    }

} 


//Integer是一个类,最大能表示的整数为128,所以会报错
public class Test01 {
    public static void main(String[] args) {
        Integer i1 = 100;
        Integer j1 = 100;
        System.out.println(i1.equals(j1)); // true

        Integer i2 = 129;
        Integer j2 = 129;
        System.out.println(i2.equals(j2)); // false
    }
}