当前位置:网站首页>2021-05-07封装 继承 super this

2021-05-07封装 继承 super this

2022-06-23 09:55:00 鹿其其鹿

封装

高内聚 低耦合
属性私有 get/set

封装的意义:
提高程序的安全性,保护数据
隐藏代码的实现细节
统一接口
系统可维护性增加

继承

继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
extends的意思是扩展,子类是父类的扩展

java类中只有单继承,没有多继承

子类继承父类,使用关键字extends来表示

//Person 人 父类
public class Person {
    
    public void say(){
    
        System.out.println("说了一句话");
    }
}
package oop.demo5;

//学生 is 人 子类
public class Student extends Person{
    
}

public class Application {
    
    public static void main(String[]args){
    
        Student student = new Student();
        student.say();
    }
}

私有的东西无法被继承(除非用get,set提取)

super 注意点

  1. super调用父类的构造方法,必须在构造方法的第一个
  2. super必须只能出现在子类的方法或者构造方法中
  3. super和this不能同时调用构造方法

super VS this

代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用

前提:
this:没有继承也可以使用
super:只能在继承条件下才可以使用

构造方法:
this():调用本类的构造
super():调用父类的构造
.

原网站

版权声明
本文为[鹿其其鹿]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45478564/article/details/116496696