21. java面向对象 - JavaBean
一、定义
JavaBean是一种Java语言写成的可重用组件,所谓JavaBean是指符合如下要求的java类:①类是公共的 ②有一个无参的公共构造器 ③有属性,且有对应的get、set方法。用户可以使用JavaBean将功能、处理、值、数据库访问和其他任何可以用Java代码创造的对象进行打包,并且其他的开发者可以通过内部的JSP页面、Servlet、其他JavaBean、applet程序或应用来使用这些对象。用户可以认为JavaBean提供了一种随时随地的复制粘贴的功能,而不要关心任何改变。
二、this关键字
- this可以修饰属性、方法、构造器
- this理解为当前对象
- 在类方法中,用"this.属性"或"this.方法"是调用对象的属性或方法,一般我们都省this
public class Person {
private String name;
private int age;
//构造器
public Person(){
this.eat();
}
public Person(String name){
this.name = name;
}
public Person(String name, int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
//如果不加this,则设置是就是空字符串,因为name就是形参就近原则
//加上后,就可区分this.name是属性;后面name是形参
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void eat(){
System.out.println("people eat fruit");
this.study();
}
public void study(){
System.out.println("pepole study english");
}
}
class PersonTest{
public static void main(String[] args) {
Person p1 = new Person();
p1.eat();
}
}
- this调构造器
- 我们在构造器中,可以显示使用"this(形参列表)"方式调用指定本类中指定其他构造器
- 构造器中,不能使用"this(形参列表)"方式调用自己
- 如果一个类中有n个构造器,最多有n-1个调用,不能成环
- 构造器中只能声明一个"this(形参列表)"
public class Person {
private String name;
private int age;
//构造器;若没创建对象都要去实现一段代码为了减少冗余,使用this调构造器
public Person(){
System.out.println("person 需要创建很多...,现行创建");
}
public Person(String name){
// 调用空参构造器
this();
this.name = name;
}
public Person(String name, int age){
// 调用带参构造器,此构造起中有name参数,就不需要自己创建了
this(name);
//this.name = name;
this.age = age;
}
// 封装性体现
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void eat(){
System.out.println("people eat fruit");
this.study();
}
public void study(){
System.out.println("pepole study english");
}
}
class PersonTest{
public static void main(String[] args) {
Person p1 = new Person("tom", 18);
System.out.println(p1.getAge());
}
}
三、实例
1. account
public class Account {
/*
* id:账号
* balance:余额
* annualInterestRate:年利率
* */
private int id;
private double balance;
private double annualInterestRate;
//构造器
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
//get和set方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//取钱
public void withdraw(double amount) {
//余额不足提示
if (balance < amount) {
System.out.println("sorry, Insufficient balance");
return;
}
balance -= amount;
System.out.println("Successfully withdraw" + " " + amount + " yuan!");
}
//存钱
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Successfully deposited" + " " + amount + " yuan!");
}
}
}
2. customer
public class Customer {
private String firstName;
private String lastName;
//对象属性
private Account account;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
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;
}
}
3. customerTest
public class CustomerTest {
public static void main(String[] args) {
/*
* 一个用户Jane Smith,账号1001,余额2000,利率1.23%
* */
Customer customerObj = new Customer("Jone","Smith");
Account accountObj = new Account(1001, 2000, 0.0123);
//在银行开个户,设置id=1001,金额2000
customerObj.setAccount(accountObj);
//先通过用户获取账户,存100元,取960元
customerObj.getAccount().deposit(100);
customerObj.getAccount().withdraw(960);
customerObj.getAccount().withdraw(2000);
}
}
4. bank
import sun.print.CUPSPrinter;
public class Bank {
//银行有多个用户,创建一个数组
private Customer[] customers;
//客户个数
private int numberOfCustomer;
public Bank() {
//需要先初始化数组
customers = new Customer[10];
}
//添加客户,添加一个后,许将numberOfCustomer自增1
public void addCustomer(String f, String l) {
Customer customerObjTwo = new Customer(f, l);
customers[numberOfCustomer] = customerObjTwo;
numberOfCustomer++;
//或
customers[numberOfCustomer++] = customerObjTwo;
}
//获取客户数量
public int getNumeberOfCustomer() {
return numberOfCustomer;
}
//获取指定位置上的客户
public Customer getCustomer(int index) {
if (index >= 0 && index < numberOfCustomer) {
return customers[index];
}
return null;
}
}
5. bankTest
public class BankTest {
public static void main(String[] args) {
/*
* 创建一个银行,增加一个客户tom hu
* 获取该客户,为他创建一个账户102,余额10000
* 先通过客户,获取他账户,在取2000元
* */
Bank bankobj = new Bank();
bankobj.addCustomer("tom","hu");
bankobj.getCustomer(0).setAccount(new Account(102,10000,0.023));
bankobj.getCustomer(0).getAccount().withdraw(2000);
System.out.println(bankobj.getCustomer(0).getAccount().getBalance());
}
}