fengye0518

导航

Java学习 (23) 对象篇(03)封装&继承&多态

封装

  • 我们程序设计要追求“高内聚,低耦合”

    • 高内聚:就是类的内部数据操作细节自己完成,不允许外部干涉
    • 低耦合:仅暴露少量的方法给外部使用
  • 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏

  • 属性私有,get/set

    • get作用是获得相应数据
    • set是给get获得数据赋予值
  • 封装很基础,易懂,需牢牢掌握

语法实例

public class Student {

    //属性私有
    private String name;
    private int age;
    private String sex;

    //提供可操作方法

    //获得相应数据
    public String getName(){
        return this.name;
    }
    //赋予相应数据的值
    public void setName(String name){
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >= 100 || age <= 0){
            System.out.println("不存在!");
        }else {
            this.age = age;
        }
    }


    public String getSex(){
        return sex;
    }
    public void setsex(String sex){
        this.sex = sex;
    }
}
import com.luo.oop.demo03.Student;

public class Application {
    public static void main(String[] args){
        Student s1 = new Student();

        s1.setName("枫叶");
        System.out.println(s1.getName());

        s1.setAge(101);
        System.out.println(s1.getAge());

        s1.setsex("男");
        System.out.println(s1.getSex());
    }
}
/*run:
	枫叶
	不存在!
	0
	男
*/

具体讲解视频(狂神说Java)

面向对象08:封装详解_哔哩哔哩_bilibili


继承

  • 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
  • extends 的意思是“扩展”。子类是父类的扩展
  • JAVA中类只有单继承,没有多继承
  • 继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等
  • 继承关系的俩个类,一个为子类(派生类).一个为父类(基类)。子类继承父类,使用关键字extends来表示。子类和父类之间,从意义上讲应该具有"is a"的关系

Object 类

  • object 类是最高级别的类,高于任何类

语法实例

public class Person {

    //public
    //protected
    //default
    //private

    public String say = "你属猪";

    private int money = 10_0000_0000;
    public int getMoney(){
        return money;
    }
    public void setMoney(int money){
        this.money = money;
    }

}
public class Student extends Person{}
import com.luo.oop.demo04.Student;


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

        System.out.println(student.getMoney());

        System.out.println(student.say);


}
/*run:
	1000000000
	你属猪
*/

具体讲解视频(狂神说Java)

面向对象09:什么是继承_哔哩哔哩_bilibili


Super 关键字

super注意点:

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

语法实例

public class Person {

    public Person(){
        System.out.println("Person无参调用");
    }

    public void print(){
        System.out.println("Person");
    }


    protected String name = "枫叶";


}
public class Student extends Person{


//    private String name = "fengye";

    public Student(){
        /*隐藏代码
        super();
         */
        System.out.println("Student无参调用");
    }

    public void print(){
        System.out.println("Student");
    }

    public void text1(){
        print();
        this.print();
        super.print();
    }

    public void text(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.text("小枫");
        //student.text1();
    }
}

super 与 this 不同点

  • super
    • 代表父类对象的应用前提
    • 只能在继承条件才可以使用构造方法
    • 基于父类的构造
  • this
    • 本身调用者这个对象
    • 没有继承也可以使用
    • 是基于本类的构造

具体讲解视频(狂神说Java)

面向对象10:Super详解_哔哩哔哩_bilibili


重写

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

  • 重写规则

    1. 方法名必须相同
    2. 参数列表列表必须相同
    3. 修饰符:范围可以扩大但不能缩小
    4. 抛出的异常:范围,可以被缩小,但不能扩大
    5. 子类的方法和父类方法必要一致,方法体不同
  • 跟多态一样,同一件事(方法),父亲(父类)和儿子(子类)的处理方式不同,就相当于各有各的想法

  • 重写:方法名和参数都一样,实现过程随意

  • 重载:方法名相同,参数必须不同

具体讲解视频(狂神说Java)

面向对象11:方法重写_哔哩哔哩_bilibili


多态

  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式

  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

  • 多态存在条件

    • 有维承关系
    • 子类重写父类方法
    • 父类引用指向子类对象
  • 注意事项:

    • 多态是方法的多态,属性没有多态
    • 类之间必须有联系
    • 存在必须是继承关系,方法需要重写,父类引用指向子类对象,输出的是子类方法

语法实例

public class Person {
    public void run(){
        System.out.println("run");
    }
}
public class Student extends Person{
    public void run(){
        System.out.println("sun");
    }
}
public class Application {
    public static void main(String[] args){

        //父类的引用可以指向子类的类型
        //对象执行方法主要看对象左边是否有相关方法,与右边关系不大

        //student 能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //Person 父类可以指向子类,但是不能调用子类的方法
        Person s2 = new Student();
        Object s3 = new Student();


        //由于子类调用了父类的方法,所以输出父类时将会执行子类的方法(重写方法)
        s2.run();
        s1.run();
    }
}

具体讲解视频

面向对象12:什么是多态_哔哩哔哩_bilibili


instanceof 和转换类型

**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 s1 = new Student();

        System.out.println(s1 instanceof Object);    //true
        System.out.println(s1 instanceof Person);    //true
        System.out.println(s1 instanceof Student);   //true
        System.out.println(s1 instanceof Teacher);   //false
        System.out.println(s1 instanceof String);    //false

        System.out.println("=================================");

        Person s2 = new Student();

        System.out.println(s2 instanceof Object);    //true
        System.out.println(s2 instanceof Person);    //true
        System.out.println(s2 instanceof Student);   //true
        System.out.println(s2 instanceof Teacher);   //false
        //System.out.println(s2 instanceof String);    //编译报错,同级不能比较

        System.out.println("=================================");

        Student s3 = new Student();

        System.out.println(s3 instanceof Object);    //true
        System.out.println(s3 instanceof Person);    //true
        System.out.println(s3 instanceof Student);   //true
        //理解方式:我虽然和你一个班级,但我俩没直接联系
        //System.out.println(s3 instanceof Teacher);   //编译报错,同级不能比较
        //System.out.println(s3 instanceof String);    //编译报错,不存在联系

    }
}

转换类型

  • 父类引用指向子类的对象
  • 把子类转换为父类,向上转型,但会失去自己的方法
  • 把父类转换为子类,向下转型,,需要强制转换方便方法的调用,减少重复的代码

具体讲解视频

面向对象13:instanceof和类型转换_哔哩哔哩_bilibili

posted on 2022-08-20 14:02  落枫叶子  阅读(33)  评论(0编辑  收藏  举报