封装性 面向对象的特征一:封装与隐藏 一.问题的引入:
当创建一个类的对象后,我们可以通过”对象.属性”的方式,对对象的属性赋值。这里,赋值操作要受到属性的数据类型和存储范围的制约。
除此之外,没有其他制约条件。但是,通常我们需要给属性赋值加入额外的限制条件。
这个条件就不能在属性声明时体现,我们只能通过方法进行限制条件的添加。(比如:setLegs())
同时,我们需要避免用户再使用”对象.属性”的方式对属性进行赋值。则需要将属性声明为私有的(private).
此时,针对于属性就体现了封装性。
二.封装性的体现:
我们将类的属性 xxx 私有化(private),同时,提供公共的(public)方法来获取(getXxx)和设置(setXxx)此属性的值
拓展:封装性的体现:
如上
不对外暴露的私有的方法
单例模式
三.封装性的体现,需要权限修饰符来配合。
1.Java 规定的 4 种权限(从小到大排列):private、缺省、protected 、public
2.4 种权限可以用来修饰类及类的内部结构:属性、方法、构造器、内部类
3.具体的,4 种权限都可以用来修饰类的内部结构:属性、方法、构造器、内部类 修饰类的话,只能使用:缺省、public
四. 四种访问权限修饰符
修饰符
类内部
同一个包
不同包的子类
同一个工程
private
Yes
(缺省)
Yes
Yes
protected
Yes
Yes
Yes
public
Yes
Yes
Yes
Yes
五.java 代码 Order 类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.atguigu.java;public class Order { private int orderPrivate; int orderDefault; public int orderPublic; private void methodPrivate () { System.out.println("methodPrivate" ); } void methodDefault () { System.out.println("methodDefault" ); } public void methodPublic () { System.out.println("methodPublic" ); } }
同一个包下面调用类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.atguigu.java;public class OrderTest { public static void main (String[] args) { Order order = new Order (); order.orderDefault = 1 ; order.orderPublic = 2 ; order.methodDefault(); order.methodPublic(); } }
不同包下面调用类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.atguigu.java1;import com.atguigu.java.Order;public class OrderTest { public static void main (String[] args) { Order order = new Order (); order.orderPublic = 2 ; order.methodPublic(); } }
六.封装性总结:Java 提供了 4 种权限修饰符来修饰类及类的内部结构,体现类及类的内部结构在被调用时的可见性的大小。 类的结构之三:构造器(或构造方法、constructor)的使用
construct:建设、建造。 construction:CCB constructor:建设者
一、构造器的作用:
创建对象
初始化对象的信息
二.Tip:
如果定义构造器的话,则系统默认提供一个空参的构造器。
定义构造器的格式:权限修饰符 类名(形参列表){}。
一个类中定义的多个构造器,彼此可以构成重载。
一旦我们定义了类的构造器之后,系统就不再提供默认的空参构造器。
一个类中,至少会有一个构造器。
三.java 代码 Person 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.atguigu.exer;public class Person { private int age; private String name; public Person () { age = 18 ; } public Person (String n, int a) { name = n; age = a; } public void setAge (int a) { if (a < 0 || a > 130 ){ throw new RuntimeException ("传入的数据非法!" ); } age = a; } public int getAge () { return age; } public void setName (String n) { name = n; } public String getName () { return name; } }
Person 类测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.atguigu.exer;public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); p1.setAge(12 ); System.out.println(p1.getName() + "\t" + p1.getAge()); Person p2 = new Person ("Tom" , 21 ); System.out.println(p2.getName() + "\t" + p2.getAge()); } }
属性赋值的先后顺序
默认初始化
显式初始化
构造器中初始化
通过”对象.方法” 或 “对象.属性”的方式,赋值
以上操作的先后顺序:① - ② - ③ - ④
java 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.atguigu.java1;public class UserTest { public static void main (String[] args) { User u1 = new User (); System.out.println(u1.age); User u2 = new User (2 ); u2.setAge(3 ); System.out.println(u2.age); } } class User { String name; int age = 1 ; public User () { } public User (int a) { age = a; } public void setAge (int a) { age = a; } }
javaBean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.atguigu.java1;public class Customer { public int id; private String name; public Customer () { } public void setId (int i) { id = i; } public int getId () { return id; } public void setName (String n) { name = n; } public String getName () { return name; } }
this 关键字的使用
this 可以用来修饰、调用:属性、方法、构造器
this 修饰属性和方法:this 为:当前对象 或 当前正在创建的对象
在类的方法中,我们可以使用”this.属性”或”this.方法”的方式,调用当前对象属性或方法。 但是,通常情况下,我们都选择省略”this.”。特殊情况下,如果方法的形参和类的属性同名时, 我们必须显式的使用”this.变量”的方式,表明此变量是属性,而非形参。
在类的构造器同理
this 调用构造器
我们在类的构造器中,可以显式的使用”this(形参列表)”方式,调用本类中指定的其他构造器
构造器中不能通过”this(形参列表)”方式调用自己
如果一个类中有 n 个构造器,则最多有 n - 1 构造器中使用了”this(形参列表)”
规定:”this(形参列表)”必须声明在当前构造器的首行
构造器内部,最多只能声明一个”this(形参列表)”,用来调用其他的构造器
java 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package com.atguigu.java2;public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); p1.setAge(1 ); System.out.println(p1.getAge()); Person p2 = new Person ("Tom" , 20 ); System.out.println(p2.getAge()); } } class Person { private String name; private int age; public Person () { System.out.println("Person初始化时,需要考虑如下的1,2,3,4" ); } public Person (String name) { this (); this .name = name; } public Person (String name, int age) { this (name); this .age = age; } public void setName (String name) { this .name = name; } public String getName () { return name; } public void setAge (int age) { this .age = age; } public int getAge () { return age; } public void eat () { System.out.println("eating" ); study(); } public void study () { System.out.println("studying" ); } }
boy,gril 测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.atguigu.exer2;public class Boy { private String name; private int age; public Boy () { } 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 (this .age >= 22 ){ System.out.println("yes!" ); }else { System.out.println("no~" ); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package com.atguigu.exer2;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 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 ; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.atguigu.exer2;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 ("2233" , 11 ); int compare = girl.compare(girl1); if (compare > 0 ){ System.out.println(girl.getName() + "大" ); }else if (compare < 0 ){ System.out.println(girl1.getName() + "大" ); }else { System.out.println("一样大" ); } } }
Account 测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package com.atguigu.exer3;public class Account { 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; } 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("余额不足,取款失败" ); return ; } balance -= amount; System.out.println("成功取出:" + amount); } public void deposit (double amount) { if (amount > 0 ){ balance += amount; System.out.println("成功存入:" + amount); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.atguigu.exer3;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 void setAccount (Account account) { this .account = account; } public String getFirstName () { return firstName; } public String getLastName () { return lastName; } public Account getAccount () { return account; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.atguigu.exer3;public class CustomerTest { public static void main (String[] args) { Customer cust = new Customer ("Jane" , "Smith" ); Account acct = new Account (1000 , 2000 , 0.0123 ); cust.setAccount(acct); cust.getAccount().deposit(100 ); cust.getAccount().withdraw(960 ); cust.getAccount().withdraw(2000 ); System.out.println("Customer[" + cust.getLastName() + "," + cust.getLastName() + "]" + "\n" + "id:" + cust.getAccount().getId() + "\n" + "annualInterestRate:" + cust.getAccount().getAnnualInterestRate()*100 + "%" + "\n" + "balance:" +cust.getAccount().getBalance()); } }
Bank 测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.atguigu.exer4;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("存钱成功" ); } } public void withdraw (double amt) { if (balance >= amt){ balance -= amt; System.out.println("取钱成功" ); }else { System.out.println("余额不足" ); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.atguigu.exer4;public class Bank { private Customer[] customers; private int numberOfCustomers; public Bank () { customers = new Customer [10 ]; } public void addCustomer (String f, String l) { Customer cust = new Customer (f, l); customers[numberOfCustomers] = cust; numberOfCustomers++; } public int getNumberOfCustomers () { return numberOfCustomers; } public Customer getCustomer (int index) { if (index >= 0 && index < numberOfCustomers){ return customers[index]; } return null ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.atguigu.exer4;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 Account getAccount () { return account; } public void setAccount (Account account) { this .account = account; } public String getFirstName () { return firstName; } public String getLastName () { return lastName; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.atguigu.exer4;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 ); double balance = bank.getCustomer(0 ).getAccount().getBalance(); System.out.println("客户:" + bank.getCustomer(0 ).getFirstName() + "的账户余额为:" + balance); System.out.println(); bank.addCustomer("万里" , "杨" ); System.out.println("银行客户的个数为:" + bank.getNumberOfCustomers()); } }
Package 的使用
为了更好的实现项目中类的管理,提供包的概念
使用 package 声明类或接口所属的包,声明在源文件的首行
包,属于标识符,遵循标识符的命名规则、规范(xxxyyyzzz)、“见名知意”
每”.”一次,就代表一层文件目录。
JDK 中主要的包介绍
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)。
Import 的使用
在源文件中显式的使用 import 结构导入指定包下的类、接口
声明在包的声明和类的声明之间
如果需要导入多个结构,则并列写出即可
可以使用”xxx.*“的方式,表示可以导入 xxx 包下的所有结构
在 java.lang 下的话(java.lang.System 常用),则可以省略 import 结构
如果使用的类或接口是本包下定义的,则可以省略 import 结构
如果在源文件中,使用了不同包下的同名的类,则必须至少有一个类需要以全类名的方式显示。
使用”xxx.*“方式表明可以调用 xxx 包下的所有结构。但是如果使用的是 xxx 子包下的结构,则仍需要显式导入
import static:导入指定类或接口中的静态结构:属性或方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package com.atguigu.java2;import com.atguigu.exer4.Account;import com.atguigu.exer4.Bank;import com.atguigu.java2.java3.Dog;import java.util.*; import static java.lang.System.*; import static java.lang.Math.*;public class PackageImportTest { public static void main (String[] args) { Bank bank = new Bank (); Arrays.toString(new int []{1 , 2 , 3 }); HashMap map = new HashMap (); Scanner scanner = null ; Account acct = new Account (1000 ); com.atguigu.exer3.Account acct1 = new com .atguigu.exer3.Account(1000 ,2000 ,0.0123 ); Date date = new Date (); java.sql.Date date1 = new java .sql.Date(123465l ); Dog dog = new Dog (); out.println("hello" ); long num = round(81 ); } }