Java面向对象-构造器-this的综合使用-2023-5-23

三、构造器和this的综合使用例题一

public class Boy {
    private String name;
    private int age;

    //方法
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
    public Boy(){
    }
    public Boy(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void marry(Girl girl){
        System.out.println("我想娶"+girl.getName());
    }

    public void shout(){
        if(this.age >= 22){
            System.out.println("我可以结婚了!");
        }else{
            System.out.println("我可以谈恋爱了!");
        }
    }

}
public class Girl {
    private String name;
    private int age;

    //方法
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
    public Girl(){
    }
    public Girl(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void shout() {
        if (this.age >= 22) {
            System.out.println("我终于可以结婚了");
        } else {
            System.out.println("我只能谈恋爱");
        }
    }
    public void marry(Boy boy){
        System.out.println("我想嫁给"+boy.getName());
        boy.marry(this);
    }
    public int compare(Girl girl){
        if(this.age > girl.age){
            return 1;
        }else if (this.age < girl.age){
            return -1;
        }else{
            return 0;
        }
    }

}

public class BoyGirlTest {
    public static void main(String[] args){

        Boy boy1 = new Boy("杰克",24);
        Girl girl1 = new Girl("朱丽叶",20);

        girl1.marry(boy1);

       boy1.shout();

       Girl girl2= new Girl("肉丝",18);
       int compare = girl1.compare(girl2);
       if(compare>0){
           System.out.println(girl1.getName()+"大");
       }else if (compare < 0){
           System.out.println(girl2.getName()+"大");
       }else{
           System.out.println("一样大");
        }

    }
}


声明方法时注意声明部位。

案例:
1、按照UML类图,创建Account类,提供必要的结构。
在提款方法withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
depolit()方法表示存款

按照UML类图,创建Customer类,提供必要的结构。

3、按照UML类图,创建Bank类,提供必要的结构。

addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer对象,然后把它放到 customer 数组中
还必须把 numberofCustomer 属性的值加 1。
getNum0fCustomers 方法返回 numberofCustomers 属性值。
getcustomer方法返回与给出的index参数相关的客户。

4.创建BankTest类,进行测试

 

 

 

public class Bank {
    //属性
    private static Customer[] customers;//用于保存多个客户
    private static int numberofCustomer;//用于记录存储的客户的个数
    //方法

    /**
     * 将指定姓名的客户保存在银行客户列表中
     * @param f
     * @param l
     */
    public void addCustomer(String f, String l){
        Customer cust = new Customer(f,l);
        //customers[numberofCustomer] = cust;
        // numberofCustomer++;
        customers[numberofCustomer++] = cust;
    }

    /**
     * 获取客户列表中存储的客户个数
     * @return
     */
    private int getNumberofCustomer(){
        return numberofCustomer;
    }

    /**
     * 获取指定索引位置上的客户
     * @param
     * @return
     */
    static Customer getCustomer(int index){
        if(index < 0 || index >= numberofCustomer){
            return null;
        }
        return customers[index];

    }
    //构造器
    public Bank(){
        customers = new Customer[10];
    }
}

/**
 * 账户类
 */
public class Account {
    private double balance;//余额

    public Account(double init_balance){
        this.balance = init_balance;
    }

    public double getBalance() {
        return balance;
    }

    //存钱
    public void deppsit(double amt){
        if(amt > 0){
            balance += amt;
            System.out.println("成功存入:"+amt);
        }
    }

    //取钱
    public void withdraw(double amt){
        if(balance  >= amt){
            balance -= amt;
            System.out.println("成功取出:"+amt);
        }else{
            System.out.println("取款有误或余额不足");
        }
    }
}

/**
 * 客户
 */
public class Customer {
    private String firstName;//名
    private String lastName;//姓
    private Account account;//账户

    public Customer(String f,String l){
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
    public Account getAccount() {
        return account;
    }

}


public class BankTest {
    public static void main(String[] args){
        Bank bank = new Bank();

        bank.addCustomer("操", "曹");
        bank.addCustomer("备","刘");

        Bank.getCustomer(0).setAccount(new Account(1000));
        Bank.getCustomer(0).getAccount().withdraw(50);

        System.out.println("账户余额为:"+Bank.getCustomer(0).getAccount().getBalance());
    }
}

内存图: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值