4.1 封装
- 该露的露,该藏的藏
- 我们程序设计要追求“高内聚,低耦合”。
- 高内聚:类的内部数据操作细节自己完成,不允许外部干涉;
- 低耦合:仅暴露少量的方法给外部使用。
- 封装(数据的隐藏)
- 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。
- 记住这句话就够了:属性私有,get/set
package com.lanseyutou.oop.Demo04;
//类 private:私有
public class Student {
//属性私有
private String name;//名字
private int id;//学号
private char sex;//性别
private int age;//年龄
//提供一些可以操作这些属性的方法
//提供一些public的 get,set方法
//get 获得这个数据
public String getName(){
return this.name;
}
//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
//alt+insert 选择 setter/getter/setter and getter
public void setId(int id) {
this.id = id;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age>0 && age<120){
this.age = age;
}else{
this.age = 3;
}
}
}
package com.lanseyutou.oop.Demo04;
public class Application {
/*
1.提高程序安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护性增加了
*/
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("蓝色芋头");
System.out.println(s1.getName());
s1.setAge(123);//年龄不合法
System.out.println(s1.getAge());
s1.setAge(70);
System.out.println(s1.getAge());
}
}
4.2 继承
- 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
- extends的意思是“扩展”。子类是父类的扩展。
//person 人 父类
public class Person {
}
//Student is Person 子类
//子类继承了父类,就会拥有父类的全部方法(public)
public class Student extends Person {
}
//Teacher is Person 子类
public class Teacher extends Person {
}
- JAVA中类只有单继承,没有多继承!
- 继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
- 继承关系的俩个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
- 子类和父类之间,从意义上讲应该具有"is a"的关系.
4.2.1 object类
- 在java中所有的类,都默认直接或间接的继承object类(Ctrl+H)(私有的无法被继承:private)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pNwzumQf-1640263798708)(F:\研究生阶段\JAVA\8.Java面向对象\picture\image-20211209202150338.png)]
4.2.2 super
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-caOBrlo1-1640263798709)(F:\研究生阶段\JAVA\8.Java面向对象\picture\image-20211209204043976.png)]
public class Person {
protected String name ="lzk";
}
public class Student extends Person {
protected String name = "lanseyutou";
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("蓝色芋头");
}
}
/*
蓝色芋头
lanseyutou
lzk
*/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SGyJzO4S-1640263798711)(F:\研究生阶段\JAVA\8.Java面向对象\picture\image-20211209204819343.png)]
public class Person {
public void print(){
System.out.println("Person");
}
}
public class Student extends Person {
public void print(){
System.out.println("Student");
}
public void test1(){
print();
this.print();
super.print();
}
}
public class Application {
public static void main(String[] args) {
student.test1();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-24cPRW3x-1640263798712)(F:\研究生阶段\JAVA\8.Java面向对象\picture\image-20211209205839098.png)]
public class Person {
public Person(){
System.out.println("Person无参执行了");
}
}
public class Student extends Person {
public Student(){
//⬇⬇⬇⬇⬇隐藏代码:调用了父类的无参构造
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
}
public class Application {
public static void main(String[] args) {
}
}
/*
Person无参执行了
Student无参执行了
*/
- super注意点:
- super调用父类的构造方法,必须在构造方法的第一个
- super 必须只能出现在子类的方法或者构造方法中!
- super和 this 不能同时调用构造方法!
- Vs this:
- 代表的对象不同:
- this:本身调用者这个对象
- super:代表父类对象的应用
- 前提
- this:没哟继承也可以使用
- super:只能在继承条件才可以使用
- 构造方法
- this() :本类的构造
- super():父类的构造!
- 代表的对象不同:
4.2.3 方法重写
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nHU1M89T-1640263798714)(F:\研究生阶段\JAVA\8.Java面向对象\picture\image-20211209213152990.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ddoamI3C-1640263798716)(F:\研究生阶段\JAVA\8.Java面向对象\picture\image-20211209213423121.png)]
package com.lanseyutou.oop.Demo06;
//重写都是方法的重写,跟属性无关
public class A extends B {
//ctrl+o
//Override :重写
@Override //注解:有功能的注释
public void test() {
System.out.println("A=>test()");
}
}
package com.lanseyutou.oop.Demo06;
//重写都是方法的重写,跟属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
package com.lanseyutou.oop.Demo06;
public class Application {
public static void main(String[] args) {
//静态方法和非静态方法区别巨大
//静态方法:方法的调用只和等号左边(定义的数据类型)有关
//非静态方法:重写
A a = new A();
a.test();//A
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();//B
}
}
/*
A=>test()
A=>test()
*/
- 重写:需要有继承关系,子类重写父类的方法!
- 方法名必须相同
- 参数列表列表必须相同
- 修饰符:范围可以扩大但不能缩小: public>Protected>Default>private
- 抛出的异常:范围,可以被缩小,但不能扩大; ClassNotFoundException --> Exception(大)
- 重写,子类的方法和父类必要一致;方法体不同!
- 为什么需要重写:
- 父类的功能,子类不一定需要, 或者不一定满足!
- 不能重写的情况:
- static 方法,属于类,不属于实例
- final 常量
- private 私有方法
4.3 多态
4.3.1 多态基础
- 即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
- 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
- 注意:
- 多态是方法的多态,属性没有多态性。
- 子类和父类 有联系 类型转换异常(ClassCastExpectation)
- 存在条件:继承关系;方法需要重写;父类引用指向子类对象
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person():
//可以指向的引用类型就不确定了;父类的引用指向子类
//Student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person 父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象执行哪些方法,主要看对象左边的类型,和右边关系不大
s2.run();//子类重写了父类的方法,执行子类的方法
s1.run();
s1.eat();
//s2.eat(); Person类不能调用
}
}
/*
son
son
*/
4.3.2 instanceof
instanceof(类型关系) 引用类型,判断一个对象是什么类型
public class Person {
}
public class Student extends Person{
}
public class Teacher extends Person{
}
public class Application {
public static void main(String[] args) {
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//ture
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof Object);//true
System.out.println(object instanceof String);//false
System.out.println("============");
Person person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//ture
System.out.println(person instanceof Teacher);//false
System.out.println(person instanceof Object);//true
//System.out.println(person instanceof String);//编译报错!
System.out.println("============");
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//ture
//System.out.println(student instanceof Teacher);//编译报错!
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof String);//编译报错!
}
}
4.3.3 类型转换
- 父类<----->子类
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person {
public void go(){
System.out.println("go");
}
}
public class Application {
public static void main(String[] args) {
//高 低
Person obj = new Student();
//student将这个类型转换为Student类型,我们就可以使用Student类型的方法了
Student student = (Student) obj;
student.go();
//((Student)obj).go();
System.out.println("==========");
//子类转换为父类可能会丢失一些自己本来的方法
Student s1 = new Student();
s1.go();
Person person = s1;
}
}
- 父类引用指向子类的对象
- 把子类转化为父类,向上转型
- 把父类转化为子类,向下转型,强制转换
- 方便方法调用,减少重复的代码。简洁
4.4 补充汇总:static
//static
public class Student {
private static int age;//静态变量:属于类
private double score;//非静态变量:属于对象
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.score);
System.out.println(s1.age);
}
}
public final class Person { //用了final无法再被继承
//第二个执行 : 可以用来赋初始值
{
//(匿名)代码块
System.out.println("匿名代码块");
}
//第一个执行,跟类一起执行,且只执行一次
static {
//静态代码块
System.out.println("静态代码块");
}
//第三个执行
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("===========");
Person person2 = new Person();
}
}
/*
静态代码块
匿名代码块
构造方法
===========
匿名代码块
构造方法
*/
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(Math.random());
System.out.println(random());
System.out.println(PI);
}
}