think_in_java 14章 类字面常量

1. Class类的学习,  Class.forName()  和 类字面常量 XXX.class 的比较 后者效率更高,建议使用后者。生成对class对象的引用,后者编译器会受到检查,所以更高效。类字面常量还可以用于接口,数组,以及基本数据类型

2.  Class引用指向某个class对象,他可以制造类的实例,并包含这个类的所有方法代码,还包含类的静态成员,Class引用标识的,即是 它所指向的对象的确切类型,而该对象即是class类的一个对象。

3.泛化的class的引用,将类型变得更具体一些,对类型进行限定,用到了泛型语句。

 

        Class intClass = int.class;
        Class<Integer> genricIntclass = int.class;
        genricIntclass = Integer.class;
        intClass = double.class; //無約束
        ///genricIntclass = double.class; //会报错,因为 Class<Integer> 约束了genriclntclass 类型是 Integer
 

4 为了创建一个class引用 它被限定为某种类型,或者该类型的任何子类型,你需要将通配符与extends关键字相结合,创建一个范围,如下:

   

       // Class<Number> numClass1 = double.class; //无法工作
        Class<? extends Number> numClass = double.class;

 

5 Class<?> 优于Class 。

 

6 类的初始化顺序

/**  *  */ package com.bdr.irts.test;

/**  * @author Administrator  *  */

public class InitSequence {     

    public static void main(String[] args)     

    {         ParentClass d = new Son();         d.s();         }

}

class ParentClass {     

    public static int a = 2;     

    public int b = 3;     

{         System.out.println("this is parent anonymity b=" + b);     }     

static     {         a = 4;         System.out.println("this is parent  static and a=" + a);     }         

public ParentClass()     {         

    System.out.println("this is parent gozao");         this.s();     

}         

public void s()     {         System.out.println("this is parent");     } }

class Son extends ParentClass {

    public Son()     {         System.out.println("this is  son gozao");     }   

      public static int c = 3;         

    int d = 66;         

    static     {         c = 6;         System.out.println("this is son static and c = " + 6);     }      

   {         System.out.println("this is son anonymity d=" + d);     }      

   public void s()     {         // super.s();         System.out.println("this is son");     } }


运行结果为:

this is parent  static and a=4
this is son static and c = 6
this is parent anonymity b=3
this is parent gozao
this is son
this is son anonymity d=66
this is  son gozao
this is son


执行顺序为   静态变量  静态块  实例变量  初始化化块  构造函数 ,

如果存在继承关系,顺序为  父类的静态变量,父类静态块,子类静态变量,子类静态块,父类实例变量 父类初始化块  父类构造函数,子类实例变量 子类初始化块  子类构造函数。

 
--------------------- 
作者:彭双宝 
来源:CSDN 
原文:https://blog.csdn.net/sbpeng/article/details/19040363 
版权声明:本文为博主原创文章,转载请附上博文链接!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值