目录
封装性
Java中通过将数据声明为私有的(private),再提供公共的(public)方法:getXxx()和setXxx()实现对该属性的操作,
以实现下述目的:
- 隐藏一个类中不需要对外提供的实现细节;
- 使用者只能通过事先定制好的方法来访问数据,可以方便地加入
- 控制逻辑,限制对属性的不合理操作;
- 便于修改,增强代码的可维护性;
权限修饰符(❤)
- 类的权限修饰只可以用public和default(缺省)
- public类可以在任意地方被访问
- default类(缺省)只可以被同一个包内部的类访问
封装性实例
设置成员变量不符合取值范围,则抛出异常,不打印age值(符合逻辑,否则打印默认值,不符合逻辑)
构造器
特征:
1)它具有与类相同的名称
2)它不声明返回值类型。(与声明为void不同)
3)不能被static、final、synchronized、abstract、native修饰,不能有return语句,返回值(方法签名不声明返回值类型)
根据参数不同,分类如下:
1)隐式无参构造器(系统默认提供)
2)显式定义一个或多个构造器(无参、有参)
注 意:
1)Java语言中,每个类都至少有一个构造器
2)默认构造器的修饰符与所属类的修饰符一致
3)一旦显式定义了构造器,则系统不再提供默认构造器
4)一个类可以创建多个重载的构造器
5)父类的构造器不可被子类继承
默认初始化指的是default值(不同数据类型对应不同default值),显式初始化指的是给类成员变量赋值(private int age = 20;)
构造器练习
1)
2)
package TestBanking;
/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
import banking1.*;
public class TestBanking2 {
public static void main(String[] args) {
Customer customer;
Account account;
account = new Account(500.00);
customer = new Customer("jane","smith");// Create an account that can has a 500.00 balance.
System.out.println("Creating the customer Jane Smith.");
customer.setAccount(account);//code
System.out.println("Creating her account with a 500.00 balance.");
customer.getAccount().withdraw(150.00);//code
System.out.println("Withdraw 150.00");
customer.getAccount().deposit(22.50);
//code
System.out.println("Deposit 22.50");
customer.getAccount().withdraw(47.62);//code
System.out.println("Withdraw 47.62");
//code
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
}
}
上图 account.getBalance() 最好改成 customer.getAccount.getBalance(),原因见下面的内存结构图
acct指setAcount方法中的局部变量
ALT+/:表示代码提示
3)
修改 withdraw 方法以返回一个布尔值,指示交易是否成功
package banking4;
public class Account {
private double balance;
public Account(double init_balance){
balance = init_balance;
}
public double getBalance(){
return balance;
}
public boolean deposit(double amt){
balance += amt;
return true;
}
public boolean withdraw(double amt){
if(balance >= amt){
balance -= amt;
return true;
}else{
System.out.println("余额不足");
return false;
}
}
}
package banking4;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f,String l){
firstName = f;
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 acct) {
this.account = acct;
}
}
4)
将用数组实现银行与客户间的多重关系
package TestBanking;
/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
import banking4.*;
public class TestBanking4 {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addCustomer("J","Simms");
// Add Customer Jane, Simms
bank.addCustomer("O","Simms");//code
//Add Customer Owen, Bryant
bank.addCustomer("T","Simms");//code
// Add Customer Tim, Soley
bank.addCustomer("M","Simms");//code
// Add Customer Maria, Soley
//code
for ( int i = 0; i < bank.getNumberOfCustomers(); i++ ) {
Customer customer = bank.getCustomer(i);
System.out.println("Customer [" + (i+1) + "] is "
+ customer.getLastName()
+ ", " + customer.getFirstName());
}
}
}
package banking4;
public class Bank {
private Customer[] customers;
private int numberOfCustomer;
public Bank(){
customers = new Customer[5];
}
public void addCustomer(String f,String l){
Customer cust = new Customer(f,l);
customers[numberOfCustomer] = cust;
numberOfCustomer++;
}
public int getNumberOfCustomers() {
return numberOfCustomer;
}
public Customer getCustomer(int index){
return customers[index];
}
}
创建Bank类的对象的内存结构图如下
创建bank对象--》创建Customer数组
--》添加customer(创建customer对象)
--》数组元素赋值(值传递,地址值)--》依次进行
注意:cust是方法内的局部变量,方法执行完自动出栈,引用变量赋的是地址值(值传递机制)
this关键字
1.类方法内调用的name属性,其实是this.name,同理,方法其实是this.方法,表示当前调用属性或方法的类的对象,若在构
造器中使用name属性,其实是this.name,表示正在创建的对象的属性
补充:若是以下程序为age=age,则均认为为局部变量,age并未赋值
注意注释部分
注意this(形参)的使用
总结
复习(实验二)
1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()
程序截图:省略部分类属性的封装内容
可选择set或者get
练习7:
关注this在多个类之间的使用
JavaBean
强调:
1)package一层点代表一层目录
2)同名类导入的方法
3)关注下图第八点