随笔 - 53  文章 - 0  评论 - 0  阅读 - 4181

多态

多态

  1. 同一方法可以根据发送的对象的不同而采用多种不同的行为方式
  2. 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)
  3. 多态存在的条件

有继承关系 , 子类重写父类方法 , 父类引用子类对象

4.多态注意谁事项

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

  • 父类和子类 有联系 类型转换异常!ClassCastException!

  • 存在的条件: 继承关系 关系需要重写,父类引用指向子类对象!father son = new son();、

5.那些方法不能重写

  • static 方法 属于类 他不属于实例

  • final 常量

  • private方法(私有);

  1. 注意:多态是方法的多态 属性没有多态性

例题:

首先创建定义父类

package opp.demo06;

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

}
/*
多态注意谁事项
 1. 多态是方法的多态,属性没有多态
 2. 父类和子类  有联系  类型转换异常!ClassCastException!
 3. 存在的条件: 继承关系  关系需要重写,父类引用指向子类对象!father son = new son();

 那些方法不能重写
 1.static 方法 属于类 他不属于实例
 2.final  常量
 3.private方法(私有);
 */

然后创建定义子类

package opp.demo06;

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

最后在新类中输出

package opp;


import opp.demo06.Person;
import opp.demo06.Student;

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

        //一个对象的实际类型是确定的
        //new Student
        //new Person

        //可以指向的引用类型就不确定: 父类的引用子类

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

        //对象能执行那些方法 ,主要看对象左边的类型,和右边关系不大
        ((Student) s2).eat();//子类重写父类的方法,执行子类的方法
        s1.eat();
    }
}

instanceof

  1. (类型转换) 引用类型,判断一个对象是什么类型

    Object > String
    Object > Person > Teacher
    Object > Person > Student

  2. 公式

 // System.out.println(X instanceof Y); //能不能编译成功   X与Y是否存在父子关系 (存在成功,不存在编译失败)

例题

1.创建Person父类 Student子类 Teacher子类

2.在新类中进行编译

package opp;

import opp.demo06.*;

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

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

        System.out.println(object instanceof Student);//ture
        System.out.println(object instanceof Person);//ture
        System.out.println(object instanceof Object);//ture
        System.out.println(object instanceof Teacher01);//False
        System.out.println(object instanceof String);//False

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

        Person person =new Student();
        System.out.println(person instanceof Student);//ture
        System.out.println(person instanceof Person);//ture
        System.out.println(person instanceof Object);//ture
        System.out.println(person instanceof Teacher01);//False
        //System.out.println(persont instanceof String);//编译报错

        System.out.println("==============================");
        Student student = new Student();
        System.out.println(student instanceof Student);//ture
        System.out.println(student instanceof Person);//ture
        System.out.println(student instanceof Object);//ture
        //System.out.println(student instanceof Teacher01);//编译报错
        //System.out.println(student instanceof String);//编译报错
    }
}

类型转换

  1. 父类的引用指向子类的对象
  2. 把子类转换为父类 向上转型
  3. 把父类转换为子类 向下转型 强制转换 (容易丢失数据)
  4. 方便方法的调用 减少重复代码 简介
  5. 抽象 封装 继承 多态!
package opp;

import opp.demo06.Person;
import opp.demo06.Student;
import opp.demo06.Student01;
import opp.demo06.Teacher01;

import java.util.Scanner;

public class Application01 {
    public static void main(String[] args) {
     //类型之间的转换 父   子

       //高                    //低
        Person obj = new Student01();
        //student将这个对象转换为Student类型  我们就可以使用Student类型的方法
        ((Student01) obj).go();

        //子类转换父类 可能丢失自己本来的一些方法!
        Student01 student01 = new Student01();
        Student01.go();
        Person person = student01;
    }
}
/*
1. 父类的引用指向子类的对象
2. 把子类转换为父类  向上转型
3. 把父类转换为子类  向下转型  强制转换 (容易丢失数据)
 4. 方便方法的调用 减少重复代码 简介

 抽象 封装 继承 多态!
 */
posted on   松下的蘑菇  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示