OOP三大特征

OOP三大特征

封装

意义:

  1. 提高了程序的安全性,保护数据;

  2. 隐藏代码的实现细节;

  3. 统一接口 get、set;

  4. 提高了系统的可维护性。

继承

  1. extends指令表示继承;

 

  1. 类分为父类和子类,子类可以继承父类的public方法,父类的private方法可以用get、set来调用,只有单继承,没有多继承;

 

  1. 在java里面,如果不写继承哪个类,则默认直接或间接继承Object类,Object类是java自带的;

  1. super注意点:

  • super是用来调用父类的构造方法,必须在构造方法的第一个

  • super只能出现在子类的方法或构造方法中

  • super和this不能同时调用构造方法,因为都只能放在第一个,会矛盾报错

 

  1. super和this对比:

  • 代表的对象不同:super是代表父类的对象,而this是代表本身调用者这个对象

  • 前提不一样:this没有继承也可以使用,super必须有继承关系才能使用

  • this是调用的本类的构造方法,super是调用的父类的构造方法

  1. 重写:子类重写父类的方法,需要有继承关系

 

  1. 通过Alt+Insert可以快捷输入重写,重写的标志是Override

  1. 关于重写的注意点

  • 方法名必须相同

  • 参数列表必须相同

  • 修饰符:范围可以扩大而不能缩小 public>protected>default>private

  • 抛出异常:范围可以缩小而不能扩大 ClassNotFound --> Expection(大)

 

  1. 为什么需要重写:父类的功能,子类不一定需要或不一定满足

 

多态

  1. 多态注意事项:

  • 多态是方法的多态,属性没有多态

  • 如果父类和子类没对应,则会出现异常ClassCastException

  • 存在条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son( );

  • static、final、private等方法,无法重写,更无法进行多态

 

  1. instanceof 和类型转换

  • instanceof

 System.out.println(X instanceof Y);能不能编译通过
 
 用于判断X和Y是否具有从属关系
 
         Object > Person > Studen
         Object > Person > Teacher
         Object > String
 
         System.out.println(X instanceof Y);能不能编译通过
 
         Object object = new Student();
         System.out.println(object instanceof Object);//true
         System.out.println(object instanceof Student);//true
         System.out.println(object instanceof Person);//true
         System.out.println(object instanceof Teacher);//false
         System.out.println(object instanceof String);//false
 
         System.out.println("================");
 
         Person person = new Student();
         System.out.println(person instanceof Object);//true
         System.out.println(person instanceof Student);//true
         System.out.println(person instanceof Person);//true
         System.out.println(person instanceof Teacher);//false
         //System.out.println(person instanceof String);//编译就会报错,因为Person和String是同一级的类,没有从属关系
 
         System.out.println("================");
 
         Student student = new Student();
         System.out.println(student instanceof Object);//true
         System.out.println(student instanceof Student);//true
         System.out.println(student instanceof Person);//true
         //System.out.println(student instanceof Teacher);//编译就会报错,因为Person和String是同一级的类,没有从属关系
         //System.out.println(student instanceof String);//编译就会报错,因为Person和String是同一级的类,没有从属关系

 

  • 类型转换

意义:

父类引用指向子类的对象;

把子类转换为父类,向上转型;

把父类转换为子类,向下转型,需要强制转换;

转换类型是为了方便方法的调用,减少重复的代码,简洁

 //类型之间的转换:父  子
 
 //高                 低
 Person mikasa = new Student();
 //student.go(); 现在是调用不了Student类的go方法的
 
 Student student = (Student) mikasa;//将Person类型强制转换为Student类型,有高到低
 student.go();//现在就可以使用go方法了
 //一行代码强制转换
 ((Student)mikasa).go();//这是一行代码就可以实现强制转换和方法调用
 
 
 //子类转换为父类时可能会丢失自己本来的一些方法
 Student hly = new Student();
 hly.go();
 Person person = hly;//转换为Person类
 // person.go();go方法用不了了
 
 //Studen类
 package opp.demo5.duotai;
 
 public class Student extends Person{
 
 public void go(){
     System.out.println("go go go");
 }
 
 }

 

posted @ 2021-07-17 10:33  Hly459  阅读(168)  评论(0)    收藏  举报