Java的面向对象编程有三大特征:封装,继承和多态
封装的介绍:
封装(encapsulation)就是把 抽象出的数据(属性)和 对数据的操作(方法)封装在一起,数据被保护在内部,程序的其他部分只有通过被授权的操作(方法),才能对数据进行操作
封装的理解和好处:
1)隐藏实现细节 方法(链接数据库)<--调用(传入参数)
2)可以对数据进行验证,保证安全合理
封装的实现步骤:
1)将属性进行私有化(private)(不能直接修改属性)
2)提供一个公共的(public)set方法,用于对属性判断并赋值
java
public void setXxx(类型 参数名){
//加入数据验证的业务逻辑
属性 = 参数名;
}
3)提供一个公共的get方法,用于获取属性的值
java
public 数据类型 getXxx(){ //权限判断,Xxx 某个属性
return xx;
}
快速入门案例:
java
package com.encapsulation.encap;
public class Encapsulation01 {
/*快速入门案例:
请看一个小程序(com.encapsulation.encap:Encapsulation01.java)
不能随便查看别人的年龄,工资等隐私,并对设置的年龄进行合理的验证,年龄合理就设置,否则给默认年龄,
必须在1-120,年龄和工资不能随便查看,name的长度在2-6字符之间
*/
public static void main(String[] args) {
Person person = new Person();
person.setName("jack");//名字是public,可以直接设置
//通过调用setName方法将jack传入
person.setAge(30);
person.setSalary(30000);
System.out.println(person.info());
//如果要快捷键ctrl
}
}
class Person{
public String name;//名字公开
private int age;//年龄私有化
private double salary;
//年龄和薪水是私有的,不能直接设置,需要通过set和get方法
//自己写get和set方法太慢,可以通过alt+ins - Getter和Setter自动写出
//然后根据要求完善代码
public String getName() {
return name;
}
public void setName(String name) {
//加入对数据的校验
if(name.length() >= 2 && name.length() <= 6) {
this.name = name;
}else{
System.out.println("名字长度不对,需要2-6字符");
this.name = "无名";
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age >= 1 && age <= 120)
{
this.age = age;
}else{
System.out.println("年龄需要在1-120");
this.age = 18;//给一个默认年龄
}
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//写一个方法,返回属性信息
public String info(){
return "信息为 name=" + name + " age=" + age + " sal=" + salary;
}
}
将构造器与setXxx结合:
java
package com.encapsulation.encap;
public class Encapsulation01 {
/*快速入门案例:
请看一个小程序(com.encapsulation.encap:Encapsulation01.java)
不能随便查看别人的年龄,工资等隐私,并对设置的年龄进行合理的验证,年龄合理就设置,否则给默认年龄,
必须在1-120,年龄和工资不能随便查看,name的长度在2-6字符之间
*/
public static void main(String[] args) {
Person person = new Person();
person.setName("jack");//名字是public,可以直接设置
//通过调用setName方法将jack传入
person.setAge(30);
person.setSalary(30000);
System.out.println(person.info());
//如果要快捷键ctrl
Person smith = new Person("smith", 2000, 50000);
}
}
class Person{
public String name;//名字公开
private int age;//年龄私有化
private double salary;
//构造器 快速生成alt+ins
public Person() {
}
//有三个属性的构造器
//我们可以将set方法写在构造器中,这样仍然可以验证
public Person(String name, int age, double salary) {
// this.name = name;
// this.age = age;
// this.salary = salary;
setName(name);//等价this
setAge(age);
setSalary(salary);
}
快速入门案例二:
class Account:
java
package com.encapsulation.encap;
public class Account {
/*
创建程序,在其中定义两个类:Account和AccountTest类体会Java的封装性
1.Account类要求具有属性:姓名(长度为2位3位或4位),余额(必须>20),密码(必须是六位),如果不满足,则给出提示信息,并给默认值
2.通过setXxx的方法给Account的属性赋值
3.在AccountTest中测试
*/
private String name;
private double money;
private String password;
public Account() {
}
public Account(String name, double money, String password) {
setName(name);
setMoney(money);
setPassword(password);
}
public String getName() {
return name;
}
public void setName(String name) {
if(name.length() >= 2 && name.length() <= 4) {
this.name = name;
}else{
System.out.println("姓名必须为2-4位");
this.name = "默认姓名";
}
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
if(money > 20) {
this.money = money;
}else{
System.out.println("余额必须大于20");
this.money = 21;
}
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
if(password.length() == 6) {
this.password = password;
}else{
System.out.println("密码必须等于6位,已设置为默认密码123456");
this.password = "123456";
}
}
}
class AccountTest:
java
package com.encapsulation.encap;
public class AccountTest {
//测试
public static void main(String[] args) {
Account account = new Account();
account.setName("小明");
account.setMoney(22);
account.setPassword("666666");
System.out.println(account.getName());
System.out.println(account.getMoney());
System.out.println(account.getPassword());
}
}