Java学习笔记(九)

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;
	}
}

可以很直观的感觉到主函数写的很简洁,有的只是对象的声明和方法的调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值