Java学习笔记(九)
关于面向对象的实验
public class AccountTest {
public static void main(String[] args) {
Account account = new Account();
Customer customer = new Customer();
account.id = 20131995;
account.balance = 622848;
account.withdraw(200);
System.out.println(account.balance);
customer.address = "北京";
customer.idNumber = 122341234234124211l;
customer.name = "Tom";
customer.phoneNumber = 13877114343l;
System.out.println(account.getInfo(customer));
}
}
public class Account {
//账户应有的属性
int id;
int balance;
Customer customer;
public String getInfo(Customer customer) {
String detials = "卡号为"+id+" "+"余额为"+balance+" "+"所属用户"+customer.say();
return detials;
}
//取钱的方法
public boolean withdraw(int money) {
if (money > 0) {
balance += money;
System.out.println("成功存钱"+money);
return true;
}else {
System.out.println("存钱失败");
return false;
}
}
//存钱的方法
public boolean save(int money) {
if (money > 0) {
balance -= money;
System.out.println("成功取钱"+money);
return true;
}else {
System.out.println("取钱失败");
return false;
}
}
}
public class Customer {
//客户应有的属性
String name;
long idNumber;
long phoneNumber;
String address;
//显示客户详情的方法
public String say() {
String detials = "姓名"+name+" "+ "身份证号码"+idNumber+" "+"联系电话"
+phoneNumber+" "+"地址"+address;
return detials;
}
}
可以很直观的感觉到主函数写的很简洁,有的只是对象的声明和方法的调用