学习笔记-面向对象的三大特性:封装、继承、多态

学习来源:B站【狂神说Java】Java零基础学习视频通俗易懂

封装

  • 该露的露,该藏的藏
    • 程序设计追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用
  • 封装(数据的隐藏)
    • 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏
  • 记住这句话就够了:属性私有,get/set
  • 大部分时候针对属性,针对方法用的比较少
package com.oop.demo04;

import com.oop.demo04.Student;

/*
1. 提高程序安全性,保护数据
2. 隐藏代码的实现细节
3. 统一接口
4. 系统可维护性增加了
 */

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("zhangsan");
        System.out.println(student.getName());

        student.setAge(1000);
        System.out.println(student.getAge());


    }
}

-------------------------------------------
package com.oop.demo04;

public class Student {
    //属性私有
    private String name; //名字
    private int id; //学号
    private char sex; //性别
    private int age; //年龄
    //提供一些可以操作这个属性的方法!
    //提供一些public的get、set方法

    //get时获得这个数据
    public String getName() {
        return name;
    }
    //set给这个数据设置值
    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age > 150 || age < 0){
            this.age = 3;
        }else{
            this.age = age;
        }

    }
}

继承

  • 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模

  • extends的意思是“扩展”。子类是父类的扩展

  • Java中类只有单继承,没有多继承(一个儿子只能有一个爸爸,但是一个爸爸可以有多个儿子)

  • 继承是类与类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等

  • 继承关系的两个类,一个为子类(派生类),一个是父类(基类)。子类继承父类,使用关键字extends来表示

  • 子类和父类之间,从某意义上讲应该具有“is a”的关系

  • object类

    所有的类都直接或者间接的继承Object类

  • super

  package com.oop.demo05;

  //Student is Person:子类,派生类
  //子类继承父类,就会拥有父类的全部方法
  public class Student extends Person {

      public Student(){

          //隐藏了代码super() 调用了父类的无参构造
          super();//调用父类的构造器,必须要在子类构造器的第一行
          System.out.println("Student无参执行了");

      }

      private String name = "zhangsan";

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

      public void test1(){
          print();//Student
          this.print();//Student
          super.print();//Person
      }

      public void test(){
          System.out.println(this.name);//zhangsan
          System.out.println(name);//zhangsan
          System.out.println(super.name);//王多鱼
      }
  }
  ---------------------------------------------
  public class Application {
      public static void main(String[] args) {
          Student student = new Student();
          //student.say();
          //System.out.println(student.money);
          //student.test();
          //student.test1();

      }
  }

super注意点:

  1. super调用父类的构造方法,必须在构造方法的第一个
  2. super必须只能出现在子类的方法或者构造方法中
  3. super和this不能同时调用构造方法
  • 对比this:*
  1. 代表的对象不同:this调用当前本身这个对象;super代表父类对象的引用
  2. 前提:this没有继承也可以使用,super只能在继承条件才可以使用
  3. 构造方法:this()本类的构造;super()父类的构造
  • 方法重写

    重写:需要有继承关系,子类重写父类的方法!
    1.方法名必须相同
    2.参数列表列表必须相同
    3.修饰符:范围可以扩大但不能缩小:public>Protected>Default>private
    4.抛出的异常:范围,可以被缩小,但不能扩大; classNotFoundException --> Exception(大)重写,子类的方法和父类必要一致:方法体不同!
    为什么需要重写:
    1.父类的功能,子类不一定需要,或者不一定满足!

    Alt +Insert :override;

  package com.oop;

  import com.oop.demo05.A;
  import com.oop.demo05.B;

  public class Application {
      public static void main(String[] args) {
          A a = new A();
          B b = new A();
          a.test();//A=>test()
          b.test();//B=>test()
          a.test1();//A=>test1()
          b.test1();//A=>test1()
  /*
  即b是A new出来的对象,因此调用了A的方法
  因为静态方法是类的方法,而非静态是对象的方法
  有static时,b调用了B类的方法,因为b是用B类定义的
  没有static时,b调用的是对象的方法,而b是用A类new的
   */
      }
  }
  ---------------------------------------
  package com.oop.demo05;

  public class A extends B{
      public static void test(){
          System.out.println("A=>test()");
      }
      //重写
      @Override//注解:有功能的注释
      public void test1(){
          System.out.println("A=>test1()");
      }

  }
  -----------------------------------
  package com.oop.demo05;

  public class B {
      public static void test(){
          System.out.println("B=>test()");
      }
      public void test1(){
          System.out.println("B=>test1()");
      }
  }

多态

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

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

  • 多态存在的条件

    • 有继承关系
    • 子类重写父类方法
    • 父类引用指向子类对象
  • 注意:多态是方法的多态,属性没有多态性

  package com.oop;

  import com.oop.demo06.Person;
  import com.oop.demo06.Student;

  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();无法调用
          ((Student) s2).eat();//强制类型转换后可以调用
          
      }
  }
  --------------------------------------------------
  package com.oop.demo06;

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

  }
  --------------------------------------------------
  package com.oop.demo06;

  public class Student extends Person{
      @Override
      public void run() {
          System.out.println("student:run");
      }
      public void eat(){
          System.out.println("student:eat");
      }
  }

  /*
  1. 多态是方法的多态,属性没有多态
  2. 父类和子类有联系  类型转换异常CLassCastException
  3. 存在条件:有继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son();

  无法重写:
      1.static方法,属于类,不属于实例
      2.final常量
      3.private方法
*/
  • instanceof (类型转换) 引用类型
  package com.oop;

  import com.oop.demo06.Person;
  import com.oop.demo06.Student;
  import com.oop.demo06.Teacher;

  public class Application {
      public static void main(String[] args) {

          //System.out.println(X instanceof Y);
          //编译通过:X与Y存在父子关系
          //true:X指向的类型是Y的子类型(对象) Student->Person->Object

          //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);//true
          System.out.println(object instanceof Object);//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 Student);//true
          System.out.println(person instanceof Person);//true
          System.out.println(person instanceof Object);//true
          System.out.println(person instanceof Teacher);//false
          //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);//true
          System.out.println(student instanceof Object);//true
          //System.out.println(student instanceof Teacher);//编译报错
          //System.out.println(person instanceof String);//编译报错
      }
  }

  ------------------------------------------------
  package com.oop.demo06;

  public class Teacher extends Person{

  }

  -------------------------------------------------
  package com.oop.demo06;

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

  }
  --------------------------------------------------
  package com.oop.demo06;

  public class Student extends Person{
      @Override
      public void run() {
          System.out.println("student:run");
      }
      public void eat(){
          System.out.println("student:eat");
      }
  }
  • 类型转换
  package com.oop;

  import com.oop.demo06.Person;
  import com.oop.demo06.Student;

  public class Application {
      public static void main(String[] args) {
          //类型之间的转换 父  子
          //高               低
          Person student = new Student();
          //student.go();无法调用go方法
          //student 将这个对象转化成Student类型,我们就可以使用Student类型的方法了。
          ((Student) student).go();
          ((Student) student).run();

          Student student1 = new Student();
          student1.go();
          Person person = student1;
          //person.go();无法调用go方法
          //子类转换为父类,可能丢失自己本来的一些方法
      }
  }

  /*
  1. 父类引用指向子类的对象
  2. 把父类转换为子类,向下转型,需强制转换
  3. 把子类转换为父类,向上转型,可能丢失自己本来的一些方法(与基本数据类型大范围转小范围丢失精度相反)
  4. 方便方法的调用,减少重复的代码,简洁
   */
   
   -----------------------------------------------------------
  package com.oop.demo06;

  public class Student extends Person{
      public void go(){

      }

  }
  ------------------------------------------------------------
  package com.oop.demo06;

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

  }

posted @ 2022-06-23 22:01  夏夜晚风-  阅读(30)  评论(0编辑  收藏  举报