Java面向对象-封装、继承、多态11-12

封装

  • 程序设计要求“高内聚,低耦合“。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用

  • 封装(数据的隐藏)

通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口访问,这称为信息隐藏。

  • 属性私有,get/set

package com.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---自动生成get/set方法
    //测试:
​
    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 > 120 || age < 0){// 不合法
            this.age = 3;
        }else{
            this.age = age;
        }
​
    }
}
package com.oop;
​
​
import com.oop.demo04.Student;
​
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("yy");
        System.out.println(s1.getName()); // yy
​
        s1.setAge(999); // 不合法
        System.out.println(s1.getAge()); // 3
    }
}

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

继承

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

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

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

  • Java里只有单继承。

  • Object类

  • supper与this对比

  • 方法重写

package com.oop.demo05;
​
//Person 人 :父类
public class Person {
    // public 子类可继承父类,用public
    // protected
    // default
    // private
    private int money = 10_0000_0000;
    public void say(){
        System.out.println("说了一句话");
    }
​
    public int getMoney() {
        return money;
    }
​
    public void setMoney(int money) {
        this.money = money;
    }
}
package com.oop.demo05;
//学生  is 人:子类,派生类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person {
    //快捷键:CTRL+H:hierarchy等级
​
}
​
/*
除了继承,还可以组合
public class Student{
    Person person;
}
​
 */
package com.oop.demo05;
//teacher is 人:子类
public class Teacher extends Person {
}
  • 运行

package com.oop;
​
import com.oop.demo05.Student;
​
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();// 说了一句话/继承父类Person的方法
        student.setMoney(10_0000);
        System.out.println(student.getMoney());// 100000
    }
}
  • 父类为空时:

package com.oop.demo05;
// 在Java中,所有的类,都默认直接或者间接继承Object
// Person 人 :父类
public class Person {
​
}

 


supper与this对比

package com.oop.demo05;
​
// Person 人 :父类
public class Person {
​
    public Person(){
        System.out.println("Person无参执行");
    }
    protected String name = "yl";
​
    //私有的东西无法被继承!private
    public void print(){
        System.out.println("Person");
    }
}
package com.oop.demo05;
​
//Student 子类
public class Student extends Person {
​
    public Student(){
        //隐藏代码;默认调用了父类的无参构造
        super();// 调用父类的构造器,必须要在子类构造器的第一行
        //this();super() 子类父类调用只能在第一行,且只能调用一种,要么this(),要么super()
        System.out.println("Student无参执行了");
    }
​
    private String name = "yy";
​
    public void print(){
        System.out.println("Student");
    }
​
    public void test1(){
        print();// Student
        this.print();// Student
        super.print();// Person
    }
​
    public void test(String name) {
        System.out.println(name);// 玉树
        System.out.println(this.name);// yy 这个子类
        System.out.println(super.name);// yl父类
    }
}
  • 运行

package com.oop;
​
import com.oop.demo05.Person;
import com.oop.demo05.Student;
​
public class Application {
    public static void main(String[] args) {
        Student student = new Student();//Person无参执行 Student无参执行了
        //student.test("玉树");// 玉树 yy yl
        //student.test1();// Student Student Person
    }
}
  • 方法重写

package com.oop.demo05;

//重写都是方法的重写,和属性无关
public class B {
    public static void test(){
        System.out.println("B==>test");
    }
}
package com.oop.demo05;
//A继承B
public class A extends B {
    public static void test() {
        System.out.println("A==>test");
    }
}
  • 运行:

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();
        a.test(); // A==>test

        //父类的引用指向子类
        B b = new A();
        b.test(); // B==>test
    }
}
  • 静态方法和非静态方法区别很大

  • 静态方法:方法的调用只和左边,定义的数据类型有关

  • 非静态方法:重写 public ,非静态才能重写

package com.oop.demo05;
public class B {
    public void test(){
        System.out.println("B==>test");
    }
}
package com.oop.demo05;
//A继承B
public class A extends B {
    //Override 重写
    @Override  //注解:有功能的注释!
    public void test() {
        System.out.println("A==>test");
    }
}
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();
        a.test(); // A==>test

        B b = new A(); // 子类重写了父类的方法
        b.test(); // A==>test
    }
}

总结:

super注意点:

  1. super调用父类的构造方法,必须在构造方法的第一个

  2. super必须只能出现在子类的方法或者构造方法中!

  3. super和this不能同时调用构造方法!

VS this:

代表的对象不同:

this: 本身调用者这个对象

super: 代表父类对象的应用

前提:

this:没有继承也可以使用

super:只能在继承条件才能使用

构造方法不同:

this(); 本类的构造

super(); 父类的构造!

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

  1. 方法名必须相同

  2. 参数列表必须相同

  3. 修饰符:范围可以扩大--- public(大) -- >-Protected-> Default-->private (小)

  4. 抛出的异常:范围可以被缩小,但不能扩大:ClassNotFoundException ---> Exception(大)

重写:子类的方法和父类的方法必须一致---方法体不同!

为什么需要重写?

  1. 父类的功能,子类不一定需要,或者不一定满足!

  2. 快捷键:ALT+INSERT --- > override

多态

  • 动态编译:类型--可扩展性更强

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

  • 多态存在的条件:

  1. 有继承关系

  2. 子类重写父类方法

  3. 父类引用指向子类对象

***多态是方法的多态,属性没有多态性。

  • instanceof : (类型转换) 引用类型,判断一个对象是什么类型~

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("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}
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(); // son 子类重写了父类的方法,执行子类的方法
        s1.run(); // son
        //s2.eat(); //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
    }
}
  • 多态注意事项:

    1. 多态是方法的多态,属性没有多态

    2. 父类和子类,有联系 类型转换异常! ClassCastExcption!

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

    不能重写:1. static 方法,静态方法属于类,不属于实例。

    1. final 常量;

    2. private 方法;

package com.oop.demo06;

public class Person {

    public void run(){
        System.out.println("run");
    }
}
package com.oop.demo06;

public class Student extends Person{}
package com.oop.demo06;

public class Teacher extends Person{}
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) {

        // Object > String
        // Object > Person > Student
        // Object > Person > Teacher
        Object object = new Student();

        //System.out.println(X instanceof Y); //判断能不能编译通过!

        System.out.println(object instanceof Student); // true
        System.out.println(object instanceof Object); // 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 Student); // true
        System.out.println(person instanceof Object); // true
        System.out.println(person instanceof Person); // 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 Object); // true
        System.out.println(student instanceof Person); // true
        // System.out.println(student instanceof Teacher); // 编译报错
        // System.out.println(student instanceof String); // 编译报错
    }
}
  • instanceof 和类型转换(引用类型)

package com.oop.demo06;

public class Student extends Person{
    public void go(){
        System.out.println("go");
    }
}
package com.oop;

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

public class Application {
    public static void main(String[] args) {
        // 类型之间的转换:基本类型转换---高 --> 低 (高转低需要强制转换)
        //引用类型 : 父  子
        
        //高---------------->低
        Person st = new Student();
        //st 将这个对象转换为Student类型,才可以使用student类型的方法
        Student student = (Student) st;
        student.go(); // ((Student) st).go();  括号强转
        
        // 子类转换为父类,可能丢失自己本来的一些方法
        Student student1 = new Student();
        student1.go();
        Person person = student1;
        //person.go(); //调用不了 go()方法丢失
    }
}
  1. 父类的引用指向子类的对象

  2. 把子类转换为父类,向上转型;(可直接转)

  3. 把父类转换为子类,向下转型;强制转换,

  4. 方便方法的调用,减少重复的代码!简洁!

抽象: 封装、继承、多态!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值