#### 封装对象初始流程

1. 会员卡都有哪些数据信息。
• 卡号码
• 余额
• 积分(每存100，增加一个积分，只有大于1000的时候才会给积分)

``````package base.encapsulation.CashCard.v1;

public class CashCard {
String number;
int balance;
int point;
}

``````

``````CashCard card1 = new CashCard()；
card1.number = "A001";
card1.balance = 500;
card1.point = 0;
CashCard card2 = new CashCard()；
card2.number = "A002";
card2.balance = 500;
card2.point = 0;
...............
``````

``````package base.encapsulation.CashCard.v2;

public class CashCard {
String number;
int balance;
int point;

public CashCard(String number, int balance, int point) {
this.number = number;
this.balance = balance;
this.point = point;
}
}
``````

``````CashCard card1 = new CashCard("A001", 500, 0)；
CashCard card2 = new CashCard("A002", 500, 0)；
....
``````
##### 思考时间
• 想一下，V1和V2相比，我们添加了什么？

• 我们为什么这么做？

#### 封装对象操作流程

• 关于存钱动作

``````Scanner scanner = new Scanner(System.in);
CashCard card1 = new CashCard("A001", 500, 0)；
int money = scanner.nextInt();
if(money > 0) {
card1.balance += money;
if(money >= 1000) {
card1.point += money/100;
}
} else {
System.out.println("你想干什么？");
}
CashCard card2 = new CashCard("A002", 500, 0)；
money = scanner.nextInt();
if(money > 0) {
card2.balance += money;
if(money >= 1000) {
card2.point += money/100;
}
} else {
System.out.println("你想干什么？");
}
...
``````

``````package base.encapsulation.CashCard.v3;

public class CashCard {
String number;
int balance;
int point;

public CashCard(String number, int balance, int point) {
this.number = number;
this.balance = balance;
this.point = point;
}

void store(int money) {
if (money > 0) {
this.balance += money;
if (money >= 1000) {
this.point += money / 100;
}
} else {
System.out.println("你想干什么？");
}
}
}

``````

``````Scanner scanner = new Scanner(System.in);
CashCard card1 = new CashCard("A001", 500, 0)；
card1.store(scanner.nextInt());
CashCard card2 = new CashCard("A002", 500, 0)；
card2.store(scanner.nextInt());
``````

#### 封装对象内部数据

``````CashCard card1 = new CashCard("A001", 500, 0)；
card1.balance += scanner.nextInt();
card1.point += 10000;
``````
##### 思考时间
• 问题出在哪里？

CashCard类中的所以字段在CashCard类以外也可以被回去和修改。

``````package base.encapsulation.CashCard.v4;

public class CashCard {
private String number;
private int balance;
private int point;

public CashCard(String number, int balance, int point) {
this.number = number;
this.balance = balance;
this.point = point;
}

void store(int money) {
if (money > 0) {
this.balance += money;
if (money >= 1000) {
this.point += money / 100;
}
} else {
System.out.println("你想干什么？");
}
}

public String getNumber() {
return number;
}

public int getBalance() {
return balance;
}

public int getPoint() {
return point;
}

}

``````

###### 小总结

private也可以用在方法或构造函数声明上，私有方法或构造函数通常是类内部某个共享的演算流程，外界不用知道私有方法的存在。

#### 类的语法细节

###### public权限修饰

Java中，可以使用访问控制符来保护对类、变量、方法和构造方法的访问。Java 支持 4 种不同的访问权限。

• default (即缺省，什么也不写）: 在同一包内可见，不使用任何修饰符。使用对象：类、接口、变量、方法。
• private : 在同一类内可见。使用对象：变量、方法。 注意：不能修饰类（外部类）
• public : 对所有类可见。使用对象：类、接口、变量、方法
• protected : 对同一包内的类和所有子类可见。使用对象：变量、方法。 注意：不能修饰类（外部类）
###### 关于构造函数

``````public class CashCard {
String number;
int balance;
int point;

public CashCard(String number, int balance, int point) {
this.number = number;
this.balance = balance;
this.point = point;
}
}
``````

• String
• valueOf

###### 不定长度自变量

System.out.printf()

###### 传值调用

Java只有传值调用

``````package cc.openhome;
class Customer {
String name;
Customer(String name) {
this.name = name;
}
}

public class CallByValue {
public static void some(Customer c) {
c.name = "John";
}

public static void other(Customer c) {
c = new Customer("Bill");
}

public static void main(String[] args) {
Customer c1 = new Customer("Justin");
some(c1);
System.out.println(c1.name);

Customer c2 = new Customer("Justin");
other(c2);
System.out.println(c2.name);
}
}

``````