我们程序设计追求“高内聚,低耦合”
高内聚 :类的内部数据操作细节自己完成,不允许外部干涉;
低耦合 :仅对外暴露少量的方法用于使用。
隐藏对象内部的复杂性,只对外公开简单的接口。便于外界调用,从而提高系统的可扩展性、可维护性。通俗的说,把该隐藏的隐藏起来,该暴露 的暴露出来。这就是封装性的设计思想。
Java中通过将数据声明为私有的(private),再提供公共的(public) 方法:getXxx()和setXxx()实现对该属性的操作,以实现下述目的:
隐藏一个类中不需要对外提供的实现细节;
使用者只能通过事先定制好的方法来访问数据,可以方便地加入控制逻辑, 限制对属性的 不合理操作;
便于修改,增强代码的可维护性;
- package com.xxx.java;
-
- public class AnimalTest {
-
- public static void main(String[] args) {
- Animal a = new Animal();
-
- a.name = "花花";
- a.age = 1;
- //a.legs = 1; //The field Animal.legs is not visible
- a.setLegs(6);
-
- a.show();
- a.eat();
- }
-
- }
-
- class Animal {
- String name;
- int age;
- private int legs; //防止用户跳出限制
-
- //对属性的设置
- public void setLegs(int l) { //为了禁止用户输入负数和奇数
- if (l >= 0 && l % 2 == 0) {
- legs = l;
- } else {
- legs = 0;
- //抛出异常(后续完善)
- }
- }
-
- //对属性的获取
- public int getLegs() {
- return legs;
- }
-
- public void eat() {
- System.out.println("动物进食");
- }
- public void show() {
- System.out.println("name = " + name + ",age = " + age + ",legs = " + legs);
- }
- }
Java权限修饰符public、protected、(缺省)、private置于类的成员定义前用来限定对象对该类成员的访问权限。

对于class的权限修饰只可以用public和default(缺省)。
public类可以在任意地方被访问。
default类只可以被同一个包内部的类访问。
4种权限都可以用来修饰类的内部结构:属性、方法、构造器、内部类
修饰类的话:只能使用:缺省、public
- package com.xxx.java;
-
- public class Order {
- private int orderPrivate;
- int orderDefault;
- public int orderPublic;
-
- private void methodPrivate() {
- orderPrivate = 1;
- orderDefault = 2;
- orderPublic = 3;
- }
-
- void methodDefault() {
- orderPrivate = 1;
- orderDefault = 2;
- orderPublic = 3;
- }
- public void methodPublic() {
- orderPrivate = 1;
- orderDefault = 2;
- orderPublic = 3;
- }
- }
- package com.xxx.java;
-
- public class OrderTest {
- public static void main(String[] args) {
- Order order = new Order();
-
- order.orderDefault = 1;
- order.orderPublic = 2;
-
- //私有结构无法调用
- //order.orderPrivate = 3;
-
- order.methodDefault();
- order.methodPublic();
-
- //私有结构无法调用
- //order.methodPrivate();
- }
- }
- package com.xxx.java1;
-
- import com.xxx.java.Order;
-
- public class OrderTest {
- public static void main(String[] args) {
- Order order = new Order();
-
- //缺省结构出了包后无法调用
- //order.orderDefault = 1;
- order.orderPublic = 2;
-
- //私有结构出了类后无法调用
- //order.orderPrivate = 3;
-
- //缺省结构出了包后无法调用
- //order.methodDefault();
- order.methodPublic();
-
- //私有结构出了类后无法调用
- //order.methodPrivate();
- }
-
-
- }
创建程序,在其中定义两个类:Person和PersonTest类。
定义如下: 用setAge()设置人的合法年龄(0~130),用getAge()返回人的年龄。
在 PersonTest 类 中实例化 Person 类的对象 b , 调 用 setAge() 和 getAge()方法,体会Java的封装性。
- package com.xxx.exer;
-
- public class Person {
- private int age;
-
- public void setAge(int i) {
- if(i >= 0 && i <= 130) {
- age = i;
- }else {
- System.out.println("age的范围为0-130,请重新设置");
- }
- }
-
- public int getAge() {
- return age;
- }
- }
- package com.xxx.exer;
-
- public class PersonTest {
- public static void main(String[] args) {
- Person p1 = new Person();
-
- p1.setAge(12);
-
- System.out.println(p1.getAge());
- }
- }
构造器的特征
它具有与类相同的名称
它不声明返回值类型。(与声明为void不同)
不能被static、final、synchronized、abstract、native修饰,不能有 return语句返回值
说明:
如果没有显式的定义类的构造器,则默认提供一个空参的构造器
一个类中的多个构造器,彼此构成重载
一旦显示的定义了类的构造器后,系统就不在提供默认的空参构造器
构造器的作用:
创建对象;
给对象进行初始化
语法格式:
- 修饰符 类名 (参数列表) {
- 初始化语句;
- }
- package com.xxx.java1;
-
- public class PersonTest {
- public static void main(String[] args) {
- //创建类的对象:new + 构造器
- Person p = new Person();
- Person p1 = new Person("Tom");
-
- p.eat();
-
- }
- }
-
- class Person {
- //属性
- String name;
- int age;
-
- //构造器
- public Person() {
- System.out.println("Person!");
- }
-
- public Person(String n) {
- name = n;
- }
-
- public Person(String n, int a) {
- name = n;
- age = a;
- }
-
- //方法
- public void eat() {
- System.out.println("人吃饭");
- }
-
- public void study() {
- System.out.println("人学习");
- }
- }
1. 在练习1定义的Person类中添加构造器,利用构造器设置所有人的age属 性初始值都为18。
2. 修改上题中类和构造器,增加name属性,使得每次创建Person对象的同 时初始化对象的age属性值和name属性值
- package com.xxx.exer;
-
- public class Person {
- private int age;
- private String name;
-
-
- public Person() {
- age = 18;
- }
-
- public Person(int a,String s) {
- age = a;
- name = s;
- }
-
- public void setAge(int i) {
- if(i >= 0 && i <= 130) {
- age = i;
- }else {
- System.out.println("age的范围为0-130,请重新设置");
- }
- }
-
- public int getAge() {
- return age;
- }
-
- public void setName(String s) {
- name = s;
- }
-
- public String getName() {
- return name;
- }
- }
- package com.xxx.exer;
-
- public class PersonTest {
- public static void main(String[] args) {
- Person p1 = new Person();
-
- // p1.setAge(12);
-
- System.out.println(p1.getAge());
-
- Person p2 = new Person(5,"Tom");
- System.out.println(p2.getAge());
- System.out.println(p2.getName());
- }
- }
3.编写两个类,TriAngle和TriAngleTest,其中TriAngle类中声明私有的底边长base和高height,同时声明公共方法访问私有变量。此外,提供类 必要的构造器。另一个类中使用这些公共方法,计算三角形的面积
- package com.xxx.exer;
-
- public class TriAngle {
- private double base;
- private double height;
-
- public TriAngle() {
-
- }
-
- public TriAngle(double b,double h) {
- base = b;
- height = h;
- }
-
- public void setBase(double b) {
- base = b;
- }
-
- public double getBase() {
- return base;
- }
-
- public void setHeight(double h) {
- height = h;
- }
-
- public double getHeight() {
- return height;
- }
-
- public double getArea() {
- return (base * height) / 2;
- }
- }
测试
- package com.xxx.exer;
-
- public class TriAngleTest {
- public static void main(String[] args) {
- TriAngle t1 = new TriAngle(10,20);
-
- System.out.println(t1.getBase());
- System.out.println(t1.getHeight());
- System.out.println(t1.getArea());
- }
- }
截止到目前,我们讲到了很多位置都可以对类的属性赋值。现总结这几个位 置,并指明赋值的先后顺序。
赋值的位置:
① 默认初始化
② 显式初始化
③ 构造器中初始化
④ 通过“对象.属性“或“对象.方法”的方式赋值
赋值的先后顺序:
① - ② - ③ - ④
- package com.xxx.java1;
-
- public class UserTest {
- public static void main(String[] args) {
- //证明显式比默认初始化后赋值
- User u = new User();
-
- System.out.println(u.age);
-
- //证明构造器比显式后赋值
- User u1 = new User(5);
- System.out.println(u1.age);
-
- //证明通过“对象.属性“或“对象.方法”的方式比构造器后赋值
- u1.age = 8;
- System.out.println(u1.age);
- }
- }
-
- class User{
- String name;
- int age = 1;
-
- public User() {
-
- }
- public User(int a) {
- age = a;
- }
-
-
- }
JavaBean是一种Java语言写成的可重用组件。
所谓javaBean,是指符合如下标准的Java类:
类是公共的
有一个无参的公共的构造器
有属性,且有对应的get、set方法
- package com.xxx.java1;
-
- public class JavaBean {
- private int age;
-
- public JavaBean() {
-
- }
-
- public void setAge(int a) {
- age = a;
- }
- public int getAge() {
- return age;
- }
- }
用户可以使用JavaBean将功能、处理、值、数据库访问和其他任何可以用Java代码创造的对象进行打包并且其他的开发者可以通过内部的JSP 页面、Servlet、其他JavaBean、applet程序或者应用来使用这些对象。用户可以认为JavaBean提供了一种随时随地的复制和粘贴的功能,而不用关心任何改变。

在Java中,this关键字比较难理解,它的作用和其词义很接近。
它在方法内部使用,即这个方法所属对象的引用;
它在构造器内部使用,表示该构造器正在初始化的对象。
this可以调用类的属性、方法和构造器
什么时候使用this关键字呢?
当在方法内需要用到调用该方法的对象时,就用this。
注意:
可以在类的构造器中使用"this(形参列表)"的方式,调用本类中重载的其他的构造器!
明确:构造器中不能通过"this(形参列表)"的方式调用自身构造器
如果一个类中声明了n个构造器,则最多有 n - 1个构造器中使用了 "this(形参列表)"
"this(形参列表)"必须声明在类的构造器的首行!
在类的一个构造器中,最多只能声明一个"this(形参列表)"
可以在工具栏的source或快捷键alt+shift+s快速生成代码
如:getter和setter,构造器等
练习
添加必要的构造器,综合应用构造器的重载,this关键字

- package com.xxx.exer;
-
- public class Boy {
- private String name;
- private int age;
-
-
- public Boy() {
- super();
- }
-
- public Boy(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
-
- public void marry(Girl girl) {
- System.out.println("我想娶" + girl.getName());
- }
-
- public void shout() {
- if(age >= 22) {
- System.out.println("你可以去结婚了");
- }else {
- System.out.println("先谈恋爱吧");
- }
- }
- }
- package com.xxx.exer;
-
- public class Girl {
- private String name;
- private int age;
-
- public Girl(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
-
- public void marry(Boy boy) {
- System.out.println("我想嫁给" + boy.getName());
- boy.marry(this);
- }
-
- public int compare(Girl girl) {
- if(this.age > girl.age) {
- return 1;
- }else if(this.age > girl.age) {
- return -1;
- }else {
- return 0;
- }
- }
-
- }
测试
- package com.xxx.exer;
-
- public class BoyGirlTest {
- public static void main(String[] args) {
- Boy boy = new Boy("罗密欧",21);
- boy.shout();
-
- Girl girl = new Girl("朱丽叶",18);
- girl.marry(boy);
-
- Girl girl1 = new Girl("祝英台",19);
- System.out.println(girl1.compare(girl));
- }
-
- }
1.写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。该类包括的属性: 账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和 setter 方法),取款方法 withdraw(),存款方法 deposit()。

提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不 能,应给出提示。
2. 创建 Customer 类。

a. 声明三个私有对象属性:firstName、lastName 和 account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)
c. 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName 返回相应 的属性。
d. 声明 setAccount 方法来对 account 属性赋值。
e. 声明 getAccount 方法以获取 account 属性。
3.写一个测试程序。
(1) 创建一个 Customer ,名字叫 Jane Smith, 他有一个账号为 1000,余额为 2000 元, 年利率为 1.23% 的账户。
(2) 对 Jane Smith 操作。
存入 100 元,再取出 960 元。再取出 2000 元。
打印出 Jane Smith 的基本信息
- package com.xxx.exer;
-
- public class Account {
- private int id;
- private double balance;
- private double annualInterestRate;
-
- public Account(int id, double balance, double annualInterestRate) {
- super();
- this.id = id;
- this.balance = balance;
- this.annualInterestRate = annualInterestRate;
- }
-
- 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) {
- balance -= amount;
- System.out.println("成功取出:" + amount + ",当前账户剩余" + balance);
- }else {
- System.out.println("余额不足,取款失败");
- }
- }
- public void deposit (double amount) {
- if(amount > 0) {
- balance += amount;
- System.out.println("成功存入 :" + amount + ",当前账户剩余" + balance);
- }
- }
- }
- package com.xxx.exer;
-
- public class Customer {
- private String firstName;
- private String lastName;
- private Account account;
-
- public Customer(String f, String l) {
- this.firstName = f;
- this.lastName = l;
- }
-
- public String getFirstName() {
- return firstName;
- }
-
- public String getLastName() {
- return lastName;
- }
-
- public Account getAccount() {
- return account;
- }
-
- public void setAccount(Account account) {
- this.account = account;
- }
-
-
-
-
- }
测试
- package com.xxx.exer;
-
- public class Test {
- public static void main(String[] args) {
- Customer c1 = new Customer("Jane", "Smith");
- Account a = new Account(1000,2000,1.23);
-
- c1.setAccount(a);
- c1.getAccount().deposit(100);
- c1.getAccount().withdraw(960);
- c1.getAccount().withdraw(2000);
-
- }
- }
1.按照如下的 UML 类图,创建相应的类,提供必要的结构

在提款方法 withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能, 应给出提示。deposit()方法表示存款。
2. 按照如下的 UML 类图,创建相应的类,提供必要的结构
3. 按照如下的 UML 类图,创建相应的类,提供必要的结构
addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer 对象,然后把它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1
getNumOfCustomers 方法返回 numberofCustomers 属性值。
getCustomer 方法返回与给出的 index 参数相关的客户。
创建 BankTest 类,进行测试。
- package com.xxx.erer2;
-
- public class Account {
- private double balance;
-
- public Account(double init_balance) {
- this.balance = init_balance;
- }
-
- public double getBalance() {
- return balance;
- }
-
- //存款
- public void deposit (double amt) {
- if(amt > 0) {
- balance += amt;
- System.out.println("成功存入 :" + amt);
- }
- }
-
- //取款
- public void withdraw(double amt) {
- if(balance >= amt) {
- balance -= amt;
- System.out.println("成功取出:" + amt);
- }else {
- System.out.println("余额不足,取款失败");
- }
- }
- }
- package com.xxx.erer2;
-
-
- public class Customer {
- private String firstName;
- private String lastName;
- private Account account;
-
- public Customer(String f, String l) {
- this.firstName = f;
- this.lastName = l;
- }
-
- public String getFirstName() {
- return firstName;
- }
-
- public String getLastName() {
- return lastName;
- }
-
- public Account getAccount() {
- return account;
- }
-
- public void setAccount(Account acct) {
- this.account = acct;
- }
- }
- package com.xxx.erer2;
-
- public class Bank {
- private int numberOfCustomer;
- private Customer[] customers;
-
- public Bank() {
- customers = new Customer[10];
- }
-
-
-
- public int getNumberOfCustomer() {
- return numberOfCustomer;
- }
-
- public void addCustomer(String f,String l) {
- Customer c = new Customer(f, l);
- customers[numberOfCustomer] = c;
- numberOfCustomer++;
- }
-
- public Customer getCustomer(int index) {
- if (index > numberOfCustomer || index < 0) {
- return null;
- } else {
- return customers[index];
- }
- }
- }
测试
- package com.xxx.erer2;
-
- public class BankTest {
- public static void main(String[] args) {
- Bank bank = new Bank();
-
- bank.addCustomer("Jane", "Smith");
-
- bank.getCustomer(0).setAccount(new Account(2000));
-
- bank.getCustomer(0).getAccount().withdraw(500);
-
- System.out.println(
- "客户:" + bank.getCustomer(0).getFirstName() + " 余额为:" + bank.getCustomer(0).getAccount().getBalance());
-
- bank.addCustomer("Mary", "Brown");
-
- System.out.println("银行客户的个数为:" + bank.getNumberOfCustomer());
- }
- }
package语句作为Java源文件的第一条语句,指明该文件中定义的类所在的包。(若缺省该语句,则指定为无名包)。它的格式为:
举例:pack1\pack2\PackageTest.java
- package pack1.pack2; //指定类PackageTest属于包pack1.pack2
- public class PackageTest{
- public void display(){
- System.out.println("in method display()");
- }
- }
包对应于文件系统的目录,package语句中,用 “.” 来指明包(目录)的层次;
包通常用小写单词标识。通常使用所在公司域名的倒置:com.atguigu.xxx
包的作用:
包帮助管理大型软件系统:将功能相近的类划分到同一个包中。比如:MVC的设计模式
包可以包含类和子包,划分项目层次,便于管理
解决类命名冲突的问题
控制访问权限
MVC是常用的设计模式之一,将整个程序分为三个层次:视图模型层,控制器层,与 数据模型层。这种将程序输入输出、数据处理,以及数据的展示分离开来的设计模式 使程序结构变的灵活而且清晰,同时也描述了程序各个对象间的通信方式,降低了程 序的耦合性。
模型层 model 主要处理数据
数据对象封装 model.bean/domain
数据库操作类 model.dao
数据库 model.db
视图层 view 显示数据
相关工具类 view.utils
自定义view view.ui
控制层 controller 处理业务逻辑
应用界面相关 controller.activity
存放fragment controller.fragment
显示列表的适配器 controller.adapter
服务相关的 controller.service
抽取的基类 controller.base
java.lang----包含一些Java语言的核心类,如String、Math、Integer、 System和 Thread,提供常 用功能
java.net----包含执行与网络相关的操作的类和接口。
java.io ----包含能提供多种输入/输出功能的类。
java.util----包含一些实用工具类,如定义系统特性、接口的集合框架类、使用与日 期日历相关的 函数。
java.text----包含了一些java格式化相关的类
java.sql----包含了java进行JDBC数据库编程的相关类/接口
java.awt----包含了构成抽象窗口工具集(abstract window toolkits)的多个类,这 些类被用来构建 和管理应用程序的图形用户界面(GUI)。 B/S C/S
为使用定义在不同包中的Java类,需用import语句来引入指定包层次下所需要的类 或全部类(.*)。
import语句告诉编译器到哪里去寻找类。
语法格式:
import 包名. 类名;
注意:
1. 在源文件中使用import显式的导入指定包下的类或接口
2. 声明在包的声明和类的声明之间。
3. 如果需要导入多个类或接口,那么就并列显式多个import语句即可
4. 举例:可以使用java.util.*的方式,一次性导入util包下所有的类或接口。
5. 如果导入的类或接口是java.lang包下的,或者是当前包下的,则可以省略此import语句。
6. 如果在代码中使用不同包下的同名的类。那么就需要使用类的全类名的方式指明调用的是 哪个类。
- //全类名
- com.xxx.erer3.Account
7. 如果已经导入java.a包下的类。那么如果需要使用a包的子包下的类的话,仍然需要导入。
8. import static组合的使用:调用指定类或接口下的静态的属性或方法