关闭

java反射基本概念

标签: java反射
483人阅读 评论(0) 收藏 举报
分类:

java反射基本概念


1.java反射入口

   在java程序中,任何对象都有其对应的Class,而Class是通向java反射的一道大门。
在java中获取Class有三种方式:1.通过对象的getClass()方法;2.通过类的.class属性;3.通过Class的方法
public static Class<?> forName(String className)2

/*
 * Copyright 2016    https://github.com/sdcuike Inc. 
 * All rights reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.doctor.reflect;

import java.util.Vector;

/**
 * @author sdcuike
 *
 *         Created At 2016年8月28日 下午2:55:57
 * 
 *         在java中获取Class有两种方式
 */
public class HowGetClass {

    /**
     * 在java中获取Class有两种方式
     * 
     * @param args
     */
    public static void main(String[] args) {
        // 1.
        @SuppressWarnings("rawtypes")
        Class vectorClass = Vector.class;
        System.out.println(vectorClass);// class java.util.Vector
        System.out.println(vectorClass.getName());// java.util.Vector
        System.out.println(vectorClass.getSimpleName());// Vector
        System.out.println(vectorClass.getCanonicalName());// java.util.Vector

        // 2.
        Vector<Object> vector = new Vector<>();
        @SuppressWarnings("rawtypes")
        Class<? extends Vector> vectorClass2 = vector.getClass();
        System.out.println(vectorClass2);// class java.util.Vector
    }

}

2. Class类的实例有几种类型

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. 

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

上述源自jdk文档。
Class类的实例在java应用中主要体现jdk自带的和我们自定义类的实例及接口。枚举是一种特别的类(反编译后可知枚举是java的语法糖效果),注解其实也是一种特殊的接口。java的原生类型及数组、void关键字都是Class类的实例。

Class类没有构造函数,它的实例化主要由java虚拟机负责。


/**
 * @author sdcuike
 *
 *         Created At 2016年8月28日 下午4:03:21
 */
public class ClassTypeRepresentation {

    public static void main(String[] args) {
        System.out.println(int.class.isPrimitive());// true
        System.out.println(Collection.class.isInterface());// true
        System.out.println(Object[].class.isArray());// true
        System.out.println(int[].class.getComponentType());// int
        System.out.println(DemoInterface.class.isInterface());// true
        System.out.println(DemoInterface.class.isAnnotation());// true
        System.out.println(void.class.isPrimitive());// true

    }

    public static @interface DemoInterface {
        String value() default "";
    }

}



     
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:494158次
    • 积分:7749
    • 等级:
    • 排名:第2694名
    • 原创:252篇
    • 转载:182篇
    • 译文:9篇
    • 评论:44条
    博客专栏
    博客链接