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
版权声明:本文为博主原创文章,转载请附上博文链接!