Java多态、接口继承、抽象类继承、向上向下转型实例

本文深入探讨Java中的多态性,包括接口、抽象类的继承特性,以及如何进行向上和向下转型的实际操作。通过实例讲解,帮助读者理解这些概念在实际编程中的应用。
摘要由CSDN通过智能技术生成
-------------------------------------------
关于多态、接口继承、抽象类继承等实例
1、通过多态,抽象类也可以被实现
2、通过多态,接口也可以被实现
3、向上转型、向下转型需要注意的事情
-------------------------------------------
package com.company;
public class duoTai{

    public static void main(String[] args){
        //1 抽象父类或接口引用指向子类对象,多态必要条件之一。 继承 重写 父或接口指向子
        Animal first = new Cat();//显然看,抽象类也是可以被实例化的,只是需要让抽象类的引用指向子类的实例化对象即可。
        first.eat();
        first.tell();

        //2 普通父类或接口引用指向子类对象
        Shape shape = new TriAngle();
        shape.draw();
        shape.tell(); //如果是用这种多态形式,只能调用父类拥有的函数
        doFunc(shape);
        Shape shape2 = new TectAngle();
        doFunc(shape2);

        //3 接口类型多态演示,所以接口类型也是可以被实现的,被new的
        Bottle bottle = new smallBottle();
        bottle.say();
        bottle.changet();
        doGudge(bottle);

    }

    public static void doGudge(Bottle b){
        if(b instanceof smallBottle){  //类型判断并转换
            smallBottle s = (smallBottle)b;
            s.say();
            s.changet();
        }
    }

    public static void doFunc(Shape a){
        if(a instanceof TriAngle){ //向下转型前要先判断一下是否可转,只有指向子类对象的父类引用才可以向下转
            TriAngle tri = (TriAngle)a;
            tri.draw();
            tri.tryTri();
            tri.tell();
            tri.c=1;  //先向上转型,后向下转型过后,子类就可以拥有父类的属性(方法+参数)了。
            System.out.println(tri.c);
        }
        if(a instanceof TectAngle){ //向下转型前要先判断一下是否可转,只有指向子类对象的父类引用才可以向下转
            TectAngle tri = (TectAngle)a;
            tri.draw();
            tri.tryTect();
            tri.tell();
            tri.c=1;  //先向上转型,后向下转型过后,子类就可以拥有父类的属性(方法+参数)了。
            System.out.println(tri.c);
        }
    }
}

interface Bottle{
    public int t=1;  //自动会被转换为static final
    public void say();
    public void changet();
}

class bigBottle implements Bottle{
    public void say(){
        System.out.println("i am bigBottle...");
    }
    public void changet(){
        System.out.println(t);
    }
}
class smallBottle implements Bottle{
    public void say(){
        System.out.println("i am smallBottle...");
    }
    public void changet(){
        System.out.println(t);
    }
}


abstract class Animal{  //抽象类多态演示
    public Animal(){
        System.out.println("Animal被实例化。。。");
    }

    public void tell(){
        System.out.println("Animal tell...");
    }

    public abstract void eat();
}

class Cat extends Animal{

    public Cat(){
        System.out.println("Cat 实例化...");
    }

    @Override
    public void eat(){
        System.out.println("Cat eat fish...");
    }

    public void work(){
        System.out.println("Cat catchint mouse...");
    }
}

class Dog extends Animal{
    public Dog(){
        System.out.println("Dog 实例化...");
    }

    @Override
    public void eat(){
        System.out.println("Dog eat bone...");
    }

    public void work(){
        System.out.println("Dog watching home...");
    }
}

class Shape{  //普通类多态演示以及向上和向下转型
    public Shape(){
        System.out.println("Shape被实例化...");
    }
    public void draw(){
        System.out.println("I am drawing a Shape...");
    }
    public void tell(){
        System.out.println("I am a Shape...");
    }
    public int c=0;
}

class TriAngle extends Shape{
    public TriAngle(){
        System.out.println("TriAngle被实例化...");
    }
    public void draw(){
        System.out.println("I am drawing a TriAngle...");
    }
    public void tryTri(){
        System.out.println("tryTri...");
    }
}

class TectAngle extends Shape{
    public TectAngle(){
        System.out.println("TectAngle被实例化。。。");
    }
    public void draw(){
        System.out.println("I am drawing a TectAngle...");
    }
    public void tryTect(){
        System.out.println("tryTect...");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值