1 java世界中万事万物皆对象,除了2个特殊情况
- int float等这些基本数据类型,(但是也都有Integer和Float等封装类做了弥补)
- java staic定义的,它不是属于对象的,而是属于类的
2 类也是对象,类是java.lang.Class类的实例对象 相当于 "There is a class named Class"
下面用代码说明:“任何一个类都是Class类的实例对象,有三种表示方式”
- package com.xsf.Test;
-
- public class classDemo1 {
- public static void main(String[] args) {
- Test1 test1 = new Test1();
-
-
-
-
-
-
-
-
- Class t1 = Test1.class;
- Class t2 = test1.getClass();
-
-
-
-
-
-
-
- System.out.println(t1==t2);
-
- Class t3 = null;
- try {
- t3 = Class.forName("com.xsf.Test.ClassDemo1");
- } catch (ClassNotFoundException e) {
-
- e.printStackTrace();
- }
- System.out.println(t2==t3);
-
- try {
- Test1 t4 = (Test1)t1.newInstance();
- t4.init();
- } catch (InstantiationException e) {
-
- e.printStackTrace();
- } catch (IllegalAccessException e) {
-
- e.printStackTrace();
- }
- }
- }
-
- class Test1{
- void init() {
- System.out.println("Hello World!");
-
- }
- }
转自:http://blog.csdn.net/xsf50717/article/details/44964605