-------------------------------------------
关于多态、接口继承、抽象类继承等实例
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...");
}
}
Java多态、接口继承、抽象类继承、向上向下转型实例
最新推荐文章于 2023-03-31 16:45:48 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)