封装
1.封装
1.1 封装的概念和实现
封装:
属性私有:将属性使用private修饰,表示此属性只能在本类中访问,不让外部直接访问
方法公开:针对每一个属性编写一对公开的方法 get set 分别用于属性的取值/赋值
package com.atguigu.demo.demo1; public class Test1 { public static void main(String[] args) { Rabbbit rabbbit = new Rabbbit(); rabbbit.setName("小白兔"); rabbbit.setHealth(-19); rabbbit.setAppetite(1111); rabbbit.setColor("白色"); } } class Rabbbit{ private String name;//名字 public void setName(String name){ this.name = name; } public String getName(){ return name; } private String color;//颜色 public void setColor(String color) { if((!color.equals("黑"))||(!color.equals("白"))||(!color.equals("灰"))){ System.out.println("您输入的颜色太花哨,"+color+"我们将默认设置为:白色"); this.color="白"; }else{ this.color = color; } } public String getColor() { return color; } private int health;//健康值 public void setHealth(int health) { if(((health<0)||(health>100))){ System.out.println("您输入的健康值不合法,"+health+"我们将默认设置为:60"); this.health=60; }else{ this.health = health; } } public int getHealth() { return health; } private double appetite;//食量 public void setAppetite(double appetite) { if(((appetite<0)||(appetite>100))){ System.out.println("您输入的健康值不合法,"+appetite+"我们将默认设置为:60"); this.appetite=10; }else{ this.appetite = appetite; } } public double getAppetite() { return appetite; } }
1.2封装的好处
便于使用者正确使用系统,防止错误修改属性
降低了构建大型系统的风险
提高程序的可重用性
降低程序之间的耦合度
2.访问权限修饰符
2.1 类的访问修饰符
类的访问权限修饰符有两个:
public :表示在本项目中任何位置都可以访问
默认不写:表示在同包中才能访问
2.2 类成员的访问修饰符
- 类成员访问修饰符:类的成员包括属性、方法、构造方法、代码块
- private:本类
- 默认不写:本包/同包
- protected:本类、本包、子类
- public:任何位置
3. 类类型的属性
3.1 对象类型的属性
自定义类型的属性,引用数据类型。
3.2 对象类型数组属性
自定义类型的数组 ,依然属于引用数据类型,使用方式与之前一致
package com.atguigu.demo.demo3; public class Person { private String name; private String sex; private int age; private Address address; private Hobby[] habby; public Person() { } public Person(String name, String sex, int age, Address address, Hobby[] habby) { this.name = name; this.sex = sex; this.age = age; this.address = address; this.habby = habby; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Hobby[] getHabby() { return habby; } public void setHabby(Hobby[] habby) { this.habby = habby; } }
package com.atguigu.demo.demo3; public class Address { private String province; private String city; private String street; private String buildNo; public Address() { } public Address(String province, String city, String street, String buildNo) { this.province = province; this.city = city; this.street = street; this.buildNo = buildNo; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getBuildNo() { return buildNo; } public void setBuildNo(String buildNo) { this.buildNo = buildNo; } }
package com.atguigu.demo.demo3; public class Hobby { private String type; private String name; public Hobby() { } public Hobby(String type, String name) { this.type = type; this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.atguigu.demo.demo3; public class TestPerson { public static void main(String[] args) { Person p1 = new Person(); p1.setName("张麻子"); p1.setSex("男"); p1.setAge(32); Address a1 = new Address("重庆市","江津区","龙华镇","鹅城"); p1.setAddress(a1); Hobby[] hobbis=new Hobby[3]; hobbis[0] = new Hobby("体育","跑步"); hobbis[1] = new Hobby("电竞","天涯明月刀"); hobbis[2] = new Hobby("音乐" ,"周杰伦"); p1.setHabby(hobbis); System.out.println("姓名:"+p1.getName()); System.out.println("性别:"+p1.getSex()); System.out.println("年龄:"+p1.getAge()); System.out.println("家庭住址:"+p1.getAddress().getProvince()+ p1.getAddress().getCity()+p1.getAddress().getStreet()+p1.getAddress().getBuildNo()); int way = 1; for (int i = 0; i < hobbis.length; i++) { System.out.println("第"+(way++)+"个爱好是:"+hobbis[i].getType()+"类:"+hobbis[i].getName()); } } }
4. 继承
子类继承父类,子类与父类是is-a的关系
Java中只支持单根继承,即一个类只能有一个直接父类,间接父类不设限
4.1 可以继承那些内容
继承public和protected修饰的属性和方法,不管子类和父类是否在同一个包里
继承默认权限修饰符修饰的属性和方法,但子类和父类必须在同一个包里
private修饰的属性和方法不能被继承
4.2 不能继承那些内容
private成员
子类与父类不在同包,使用默认访问权限的成员
构造方法(
构造方法用于给子类访问,不属于继承
)
5. super关键字
super关键字:表示父类的对象
可以访问父类的属性、方法、构造方法
5.1 super访问父类属性
5.2 super访问父类方法
回顾之前我们调用父类print方法,存在的两个问题:
1.因为父类中只有共有的属性,所以打印宠物信息不完善
2.父类中对宠物身份描述不具体
我们通过super关键字访问父类的方法,可以解决第一个小问题,相当于功能的组合
5.3 super访问父类构造
无参构造方法默认存在,JVM提供的,如果书写了有参构造,无参构造将被覆盖,如需使用,必须显式书写。
1.子类构造方法默认访问父类的无参构造方法,除非子类显式的访问父类的有参构造方法
2.子类构造方法必须访问父类无参或者有参构造其中一个
3.访问父类的构造方法,并不会创建父类对象,访问父类构造方法智慧初始化父类中的属
package com.atguigu.demo.demo1; public class Animal { protected String name; protected int age; protected String sex; public Animal() { } public Animal(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void printAnimal(){ System.out.println("This animal name's :"+ name ); System.out.println("This animal age is :"+ age ); System.out.println("This animal sex is :"+ sex ); } }
package com.atguigu.demo.demo1; /** * super关键字 */ public class Dog extends Animal{ private String dogType; public Dog() { } public Dog(String name,int age,String sex ,String dogType) { super(name,age,sex); //调用父类构造 this.dogType = dogType; } public String getDogType() { return dogType; } public void setDogType(String dogType) { this.dogType = dogType; } public void printDog() { super.printAnimal(); //调用父类方法 // System.out.println(super.name);//调用父类属性 System.out.println("This dog's TYPE is"+dogType); } }