类名.class和类名.this
来源:互联网 发布:php重载和重写的区别 编辑:程序博客网 时间:2024/04/30 16:00
- 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
- 类名.class 类名.this 详解
- 类名.class 类名.this
- “类名.this” 与 “类名.class”
- 类名.class 类名.this 详解
- 类名.class和类名.getClass()
- 类对象,类名.class与类名.this
- 类名.class与类名.this详解
- jdk环境变量配置
- SVN设置强制提交者填写注释信息
- netstat
- tomcat8+websocket+myelicpse 创建websocket连接报404问题
- spring 事务的两种处理方式
- 类名.class和类名.this
- Aviator-开源轻量级、高性能的表达式求值器
- hbase插入数据分析
- tomcat 高效配置
- 【TCP】Linux 和WindowsTCP套接字编程—具体流程梳理
- ps
- SVM怎么用
- Android 弹出软键盘遮住Popupwindow的解决
- Typedef用法总结