三、构造器和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());
}
}
内存图: