java 笔记一些
阅读原文时间:2023年07月08日阅读:2

面象对的umv 视图示例:

用户  银行  帐户  怎么关联

帐户代码:

文件命名 Account

package com.atuigu.exer4;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Account {
private double balance;

public Account(double init\_balance) {  
    this.balance = init\_balance;

}

public double getBalance() {  
    return balance;  
}

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

public void withdraw(double amt) {  
    if(balance>=amt) {  
        balance-=amt;  
        System.out.println("余额不足");  
    }  
}//支

}//主函数结束

银行代码:文件命名:Customer

package com.atuigu.exer4;

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 void setFirstName(String firstName) {  
    this.firstName = firstName;  
}  
public String getLastName() {  
    return lastName;  
}  
public void setLastName(String lastName) {  
    this.lastName = lastName;  
}  
public Account getAccount() {  
    return account;  
}  
public void setAccount(Account account) {  
    this.account = account;  
}

}

用户代码:文件命名:Bank

package com.atuigu.exer4;

public class Bank {
private Customer[] customers;
//定义一个可以装很多用户的数组类;
private int numberofCustomer;

public Bank() {  
    customers = new Customer\[10\];

}

public void addCustomer(String f,String l) {  
    Customer cust = new Customer(f,l);  
    customers\[numberofCustomer\] = cust;  
    numberofCustomer++;

}

public int getNumofCustomers(){  
    return numberofCustomer;

}

public Customer getCustomer(int index) {  
    if(index>=0 && index < numberofCustomer){  
    return customers\[index\];  
    }  
    return null;

}

}

主函数代码示例:文件命名:BankTest

package com.atuigu.exer4;

public class BankTest {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addCustomer("jane","Smith");
bank.addCustomer("ok", "err");

    Account a = new Account(2000);

    bank.getCustomer(0).setAccount(a);

    bank.getCustomer(0).getAccount().withdraw(500);  
    double balance = bank.getCustomer(0).getAccount().getBalance();  
    System.out.print(""+bank.getCustomer(0).getFirstName());  
    System.out.println("奶奶的银业有我少个"+ bank.getNumofCustomers());

}

}