Java中多态

多态:是一种事物的多种形态和表现形式;

多态的条件:必须要有继承和实现;

多态的最终表现形式:父类的引用指向子类对象(父类引用类型+变量=new+子类引用类型())

多态的调用;父类引用调用;

成员变量:编译运行看父类|左边|类型

成员方法:编译看父类|左边|类型,运行找子类|右边|对象

注意 : 如果没有配合方法的重写,多态就没有意义。

多态解题的四大原则

一是继承链:自己没用找父类;

二是编译看类型、确定方法,运行找对象;

三是就近最优原则

四是父类引用对子类新增方法不可见;

类型的转换

基本数据类型的转换

自动类型提升 : 小的类型 数据---> 大数据类型(long i = 1;)

强制类型转换 : 大的类型数据 ---> 小的数据类型(小范围类型 变量 = (小范围类型)大范围类型数据;int i=(int)3.0)

引用类型的转换:小类型:子类;大类型:父类;

向上转型 : 子类 --> 父类

向下转型 : 父类 --> 子类(子类类型 变量 = (子类类型)父类引用;)

作用 : 当多态调用不能调用子类独有内容时候,可以向下转型,然后调用子类独有内容

java.lang.ClassCastException类型转换异常

在向下转型的时候,如果抓成其他的子类类型,就会遇到这个异常;

instanceof运算符

引用 instanceof 类型 : 判断前面的引用是否指向后面类型的对象或者后面类型的子类对象,如果是返回true,不是返回fales

Object : 老祖宗类:是java中所有类的父类;如果一个类没有显示的继承父类,默认继承自Object类;

toString : 返回对象的字符串表现形式

public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }

1、当输出一个对象 的引用的时候,输出的是对象的引用调用toString方法的返回值==>默认调用toString

2、为什么要在子类中重写toString方法 : 因为输出对应的引用时候不想是打印对象的地址值,想要输出对象的所有属性值,对从Object中继承的toString需要,toString实现不满意,在子类中重写toString

注意:当定义javabean类型,规范都需要重写toString

 

public class Class001_Poly {
    public static void main(String[] args) {
        //对应类型数据赋值给对应类型的变量
        Person p = new Person();
        Student s = new Student();
        //正常调用 : p或者s调用的成员 : 1)自己类中存在的成员  2)从父类中继承的成员


        //多态
        Person  ps=  new Teacher();
        //多态的调用
        System.out.println(ps.str);

        ps.test();

    }
}

class Person{
    String str = "父类";

    public void test(){
        System.out.println("Person----> test");
    }
}

class Student extends Person{
    String str = "子类";

    public void test(){
        System.out.println("Student----> test");
    }
}

class Teacher extends Person{
    String str = "子类2";

    public void test(){
        System.out.println("Teacher----> test");
    }
}


class Animal{}

public class Class002_Practice {
    public static void main(String[] args) {
        A a1=new A(); //A and D   A and A
        //多态
        A a2=new B(); //A and D   B and A
        B b =new B(); //B and B   B and A   A and D
        C c=new C();
        D d =new D();    
        System.out.println(a1.show(b)); //  A and A
        System.out.println(a1.show(c)); //  A and A
        System.out.println(a1.show(d)); //  A and D
        System.out.println(a2.show(b)); //  B and A
        System.out.println(a2.show(c)); //  B and A
        System.out.println(a2.show(d)); //  A and D
        System.out.println(b.show(b));  //  B and B
        System.out.println(b.show(c));  //  B and B
        System.out.println(b.show(d));  //  A and D
    }

}

class A{
    public String show(D obj){
        return ("A and D");
    }
    public String show(A obj){
        return ("A and A");
    }
}

class B extends A{
    //新增方法
    public String show(B obj){
        return ("B and B");
    }
    public String show(A obj){
        return ("B and A");
    }
}

class C extends B{
}

class D extends B{
}


 

public class Class001_Cast {
    public static void main(String[] args) {
        int i = 1;
        //自动类型提升
        long l = i;
        //强制类型转换
        int i2 = (int)l;


        Zi zi = new Zi();
        //向上转型
        Fu fu = zi;
        //多态
        Fu f = new Zi(); //f对子类新增内容不可见

        //向下转型
        //Zi zi2 = (Zi)f;  //Zi zi2 = new Zi();  --> 可以调用子类独有内容
        //转型转错了,转成了其他的子类类型---> java.lang.ClassCastException类型转换异常

        if(f instanceof Brother){
            Brother zi2 = (Brother)f;  //Brother zi2 = new Zi();  --> 可以调用子类独有内容
            zi2.test();
        }else if(f instanceof Zi){
            Zi zi2 = (Zi)f;
            zi2.test();
        }

        System.out.println(f instanceof Fu);  //T
        System.out.println(f instanceof Zi);  //T
        System.out.println(f instanceof Brother);  //F
    }
}
class Fu{}

class Zi extends Fu{
    String str="Zi";
    void test(){
        System.out.println("子类Zi方法test");
    }
}

class Brother extends Fu{
    String str="Brother";
    void test(){
        System.out.println("子类Brother方法test");
    }
}

public class Class001_Object{
    public static void main(String[] args) {
        Student s = new Student(1001,"张三丰",100);
        System.out.println(s);
        System.out.println(s.toString());
    }
}

class  Student{
    int no;
    String name;
    int age;

    public Student(){}
    public Student(int no,String name,int age){
        this.no = no;
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return no+"-->"+name+"-->"+age;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值