zoukankan      html  css  js  c++  java
  • 强制类型转换

    强制类型转换时,

    如:A a=(B)b;      //b 为B类的instance

    编译器只检查

        1 A是B的父类

        2不检查 (B)b 是否合理。这个检查任务在runtime是执行

    package test2;
    
    public class Demo {
    
        public static void main(String[] args) {
            Parent p=new Parent();
            Child c=new Child();
            Child c1;
            Parent p1;
            p1=(Child)c;
            p1=(Child)p;      //runtime exception :ClassCastException
            
            c1=(Parent)c;       //编译时报错 Type mismatch: cannot convert from Parent to Child
            c1=(Parent)p;      //编译时报错  Type mismatch: cannot convert from Parent to Child
            
            p1=(Parent)c;
            p1=(Child)p;      //runtime exception :ClassCastException
    } } class Parent{} class Child extends Parent{}
  • 相关阅读:
    POJ3666 Making the Grade[动态规划]
    vector内部的实现1
    win32概述
    stl概述
    C++概要简介
    类的常量成员
    模板
    c11标准
    异常处理
    pak文件的打包和解包
  • 原文地址:https://www.cnblogs.com/01picker/p/4316590.html
Copyright © 2011-2022 走看看