zoukankan      html  css  js  c++  java
  • 关于JAVA 向上转型

    最近复习中比较绕的一个地方 通过试验总结一下

    若A为父类 B为子类 声明方式为:

      A t= new B();

    将子类对象赋值给父类对象,它编译时为父类对象,但运行时却是子类对象:

    1)被声明为父类对象;

    2)拥有父类属性,调用父类中非子类覆盖的方法;

    3)占用子类的内存空间;

    4)无法访问子类中特有的变量和方法;

    5)子类覆盖父类的方法时,调用子类的方法。

     1 class Subtest extends Test{
     2     int x,y;
     3     public void printHello(){
     4         System.out.println("Hello!!");
     5     }
     6     public void printhhe(){
     7         
     8     }
     9 }
    10 public class Test {
    11     int me;
    12     public void printHello(){
    13         System.out.println("Hello");
    14     }
    15     public void printHi(){
    16         System.out.println("Hi");
    17     }
    18     public static void main(String[] args){
    19         
    20         Test a=new Subtest();
    21         System.out.println(a.me);
    22         System.out.println(a.x);    //编译出错 无法拥有子类属性
    23         a.printHello();        //此时输出为子类“Hello!!”
    24         a.printHi();
    25         a.printhehe();    //编译出错 无法调用子类方法
    26     }
    27 
    28 }
  • 相关阅读:
    hadoop2.3.0cdh5.0.2 升级到cdh5.7.0
    strace
    ganglia3.7.2,web3.7.1安装
    hadoop balancer
    linux-小命令
    Ceph 架构以及原理分析
    Ceph 文件存储
    Ceph 对象存储
    Ceph 块存储
    Ceph 集群搭建
  • 原文地址:https://www.cnblogs.com/verlen11/p/4215893.html
Copyright © 2011-2022 走看看