zoukankan      html  css  js  c++  java
  • java学习笔记十一——对象转型

    向上转型:子类对象当做父类对象来使用,因为子类对象拥有父类对象的所有成员,所以不会发生任何错误。
    向下转型:父类对象当做子类对象来使用,因为子类对象部分特性父类并没有,所以需要加强制转换符。
    向上转型直接转就行了
    class TestA {
        int i = 100;
    }
    class TestB extends TestA {
        int y = 200;
    }

    public class Test
    {
        public static void main(String [] args){
            TestA a = new TestB();  //可以直接转
            System.out.println(a.i);
        }
    }
    向下转型加强制转换的例子。
    class TestA { 
        int i = 100

    class TestB extends TestA { 
        int y = 200


    public class Test { 
        public static void main(String [] args){ 
            TestA a = new TestB(); 
            TestB b = (TestB)a;      //需要加强制转换 
            System.out.println(b.y); 
        } 
    }
  • 相关阅读:
    JVM(7) Java内存模型与线程
    JVM(6) 字节码执行引擎
    JVM(5) 类加载机制
    JVM(4) 类文件结构
    JVM(3) 垃圾收集器与内存分配策略
    python的with
    python http server handle json
    c++文件读写
    python字符串处理
    python decorator
  • 原文地址:https://www.cnblogs.com/huanghai/p/2184642.html
Copyright © 2011-2022 走看看