java基础3-多态

向上类型转换:

public class Main {
    public static void main(String[] args) {
    Ason ason = new Ason(); //先构造一个子类对象
    A a2 = ason;  //子类对象向上转换不需要显示声明 以上两步可以简写为A a2=new Ason();
    a2.aMethod1();//向上类型转换可以方便地在父类对象里调用子类的方法
}
}

class A {
    public void aMethod1() {
        System.out.println("aMethod1");
    }
}

class Ason extends A {
    public void aMethod1() {
        System.out.println("aSonMethod1");
    }

    public void aMethod2() {
        System.out.println("aSonMethod2");
    }
}

输出结果:

aSonMethod1
Ason ason=new Ason();

通过构造方法生成一个ason对象
A a2=ason;

A a2=(A)ason;
结果都是一样只是前者隐藏了一个显示的转换
经过转换后父类A的对象a2可以调用子类的方法aMethod1
得到结果aSonMethod1
因为(A)ason;是一个子类转为父类的转换 所以是向上类型转换
a2.aMethod2();//报错
向上类型转换后得到的对象a2不可以调用子类中父类没有的方法

 Ason ason =new Ason();
        A a2=ason;

可以简写为:

A a2=new Ason();

这样同样可以实现向上类型转化

向下类型转换

向下类型转换是向上类型转换的逆过程 但是只能逆转换不能转换成其他子类

public class Main {
    public static void main(String[] args) {
        A a = new Ason(); //向上类型转化
        Ason ason = (Ason) a;//向下类型转化 ason成为Ason的一个对象 
        ason.aMethod2();
        ason.aMethod1();
    }
}

class A {
    public void aMethod1() {
        System.out.println("aMethod1");
    }
}

class Ason extends A {
    public void aMethod1() {
        System.out.println("aSonMethod1");
    }

    public void aMethod2() {
        System.out.println("aSonMethod2");
    }
}

输出结果:

aSonMethod2
aSonMethod1

一道题 来自原作者海子 http://www.cnblogs.com/dolphin0520/p/3803432.html

public class test {
    public static void main(String[] args) {
        Shape shape = new Circle(); 
//先构造Shape();   输出shape constructor   ----1 
// 再构造Circle();    输出circle constructor   ----2
//Circle向上转化为Shape
        System.out.println(shape.name);
//shape.name是隐藏成员 输出shape   ----3
        shape.printType();   //printType();被覆盖了输出this is circle ----4          
        shape.printName();  //printName()被隐藏 输出shape   ----5
    }                           
class Shape {
    public String name = "shape";//父类的成员不会被覆盖而是隐藏

    public Shape() {
        System.out.println("shape constructor");
    }

    public void printType() {
        System.out.println("this is shape");
    }

    public static void printName() {  //父类的静态方法不会被覆盖 而是隐藏
        System.out.println("shape");
    }
}

class Circle extends Shape {
    public String name = "circle";//隐藏父类的成员

    public Circle() {
        System.out.println("circle constructor");
    }

    public void printType() {
        System.out.println("this is circle");
    }

    public static void printName() { //隐藏父类的静态方法
        System.out.println("circle");
    }
}

输出结果为:

shape constructor
circle constructor
shape
this is circle
shape

在向上转化过程中被隐藏的部分没有被丢失 经过向上转化后的对象保留了父类的静态方法和成员变量

下面是验证:

public class test {
    public static void main(String[] args) {
        Shape shape = new Circle();//第一次向上转化   
        System.out.println(shape.name);//隐藏前的成员变量 shape
        shape.printName();//隐藏前的静态方法 shape
        shape.printType();//被覆盖的方法 this is circle
        Circle c = (Circle) shape;//向下转化
        System.out.println(c.name);//隐藏后的普通成员 circle
        c.printName();//隐藏后的静态方法 circle 
        c.printType();//覆盖后的普通方法 this is circle
        Shape shape2 = c;//再一次向上转化
        System.out.println(shape2.name);//隐藏前的成员变量 shape
        shape2.printName();//隐藏前的静态方法 shape
        shape2.printType();//被覆盖的方法 this is circle
    }
}

class Shape {
    public String name = "shape";//父类的成员不会被覆盖而是隐藏

    public Shape() {
    }

    public void printType() {
        System.out.println("this is shape");
    }

    public static void printName() {  //父类的静态方法不会被覆盖 而是隐藏
        System.out.println("shape");
    }
}

class Circle extends Shape {
    public String name = "circle";//隐藏父类的成员

    public Circle() {
    }

    public void printType() {
        System.out.println("this is circle");
    }

    public static void printName() { //隐藏父类的静态方法
        System.out.println("circle");
    }
}

输出结果:

shape
shape
this is circle
circle
circle
this is circle  //普通方法没有被保留
shape  //普通成员
shape //静态方法
this is circle

所以无论经过多少次向上或向下转换 父类的静态方法和成员变量仅仅是被隐藏
但是经过向下类型转换后 子类覆盖的普通方法并没有被保留下来

转载于:https://www.cnblogs.com/Salaku/p/5204589.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值