类对象,类名.class与类名.this
来源:互联网 发布:js控制input 框不可写 编辑:程序博客网 时间:2024/05/18 02:18
转自:http://blog.csdn.net/cto_51/article/details/8962357
- public class TestClass {
- public static void main(String[] args) {
- // 在运行期间,如果我们要产生某个类的对象,java虚拟机会检测该类型的Class对象是否已被加载
- <span style="white-space:pre"> </span>// 如果没有加载,java虚拟机会根据类的名称找到.class文件并加载它。
- // 当new Point()的时候加载这个类,用forName构造实例的时候也加载该类。 只加载一次
- System.out.println("before new Point()");
- new Point();
- System.out.println("after new Point()");
- try {
- Class.forName("Line");
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 利用对象调用getClass()方法获取该对象的Class实例
- Point pt = new Point();
- Class c1 = pt.getClass();
- System.out.println(c1.getName()); // 结果:Point
- // 使用Class的静态方法forName(),用类的名字获取一个Class实例
- try {
- Class c2 = Class.forName("Point");
- System.out.println(c2.getName()); // 结果:Point
- // 一旦某个类型的Class对象已经被加载到内存,就可以用它来产生该类型的所有对象。
- Point pp = (Point) c2.newInstance(); // newInstance()调用类中缺省的构造方法
- pp.output();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 运用.class的方式获取Class实例(类)
- Class c3 = Point.class;
- System.out.println(c3.getName()); // 结果:Point
- // 运用.calss的方式获取Class实例(基本类型)
- Class c4 = int.class;
- System.out.println(c4.getName()); // 结果:int
- // 运用.class的方式获取Class实例(基本数据类型的封装类)
- Class c5 = Integer.TYPE;
- System.out.println(c5.getName()); // 结果:int
- Class c6 = Integer.class;
- System.out.println(c6.getName()); // 结果:java.lang.Integer
- }
- }
- class Point {
- static {
- System.out.println("loading point");
- }
- void output() {
- System.out.println("x=" + x + ",y=" + y);
- }
- int x, y;
- }
- class Line {
- static {
- System.out.println("loading Line");
- }
- }
0 0
- 类对象,类名.class与类名.this
- 类名.this与类名.class
- 类名.this与类名.class (
- 类名.this与类名.class
- 类名.this与类名.class
- 类名.this与类名.class
- 类名.this与类名.class
- 类名.this与类名.class
- 类名.this与类名.class
- 类名.this与类名.class
- 类名.this与类名.class
- “类名.this” 与 “类名.class”
- 类名.class与类名.this详解
- 类名.class 类名.this 详解
- 类名.class和类名.this
- 类名.class 类名.this
- 类名.class 类名.this 详解
- class 类名 与inlcude"类名"
- 在Ubuntu上使用eclipse开发C++[持续更新...]
- Matlab计算运行时间方法
- nyoj 传纸条(一) 61 双线DP
- SQL exists/not exists语句
- 剑指Offer-15-链表中的倒数第k个节点
- 类对象,类名.class与类名.this
- UI第三天:⾃定义视图、视图控制器
- Android防360水波进度
- Rebar:Erlang构建工具
- PostgreSQL学习手册(数据表)
- Oracle 删除表中记录 如何释放表及表空间大小
- 动力电池随想
- 数列的定义如下: 数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和.多组数据,要求精度保留2位小数
- 20151112 oracle数据库德启动