`

class literal & instance.getClass() & Class.forName(String className)

阅读更多
常用的几种取得Class类实例的方式:
1 class literal (class字面量, 如String.class/int.class/void.class)
2 instanceOfClass.getClass();
3 Class.forName(String className)
4 classLoaderInstance.loadClass(String name, boolean resolve)

3 4 为显式的动态加载,关于动态加载,我要记得看附件中的Understanding Class.forName.pdf!关于ClassLoader的更多知识参阅 http://wuaner.iteye.com/admin/blogs/1011036,这里不再详述。


class literal:
Java Language Specification -> 15.8.2 Class Literals :
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.2
引用
A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.
The type of C.class, where C is the name of a class, interface, or array type (§4.3), is Class<C>.
The type of p.class, where p is the name of a primitive type (§4.2), is Class<B>, where B is the type of an expression of type p after boxing conversion (§5.1.7).
The type of void.class (§8.4.5) is Class<Void>.



class字面量只能作用在type名上(这里的type包括class, interface, array, primitive type,void)!Object的方法getClass()作用在类实例上!它们返回的都是Class类的实例!
对一个Class类的实例clazz,可以一直调用getClass方法!而.class因为不能作用在类实例上,所以不能一直调下去,因为第一次调SomeClass.class时,返回的就已经是个Class类的实例了!


JDK中关于Class类:
引用
public final class Class<T>
extends Object
implements Serializable, GenericDeclaration, Type, AnnotatedElement

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.

The following example uses a Class object to print the class name of an object:

         void printClassName(Object obj) {
             System.out.println("The class of " + obj +
                                " is " + obj.getClass().getName());
         }
    

It is also possible to get the Class object for a named type (or for void) using a class literal (JLS Section 15.8.2). For example:

         System.out.println("The name of class Foo is: "+Foo.class.getName());
    

关于Class.forName():
引用
public static Class<?> forName(String name,
                               boolean initialize,
                               ClassLoader loader)
                        throws ClassNotFoundException

    Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

    If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void.

    If name denotes an array class, the component type of the array class is loaded but not initialized.

    For example, in an instance method the expression:

          Class.forName("Foo")
        

    is equivalent to:

          Class.forName("Foo", true, this.getClass().getClassLoader())
        

    Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. Note that this method does not check whether the requested class is accessible to its caller.

    If the loader is null, and a security manager is present, and the caller's class loader is not null, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the bootstrap class loader.

    Parameters:
        name - fully qualified name of the desired class
        initialize - whether the class must be initialized
        loader - class loader from which the class must be loaded
    Returns:
        class object representing the desired class

使用clazz(a instance of Class).newInstance()时要注意:
引用
if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason,将会抛出InstantiationException



关于Object的getClass() 方法:
引用
public final Class<?> getClass()

    Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.

    The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment:

    Number n = 0;
    Class<? extends Number> c = n.getClass();

    Returns:
        The Class object that represents the runtime class of this object.



What is a class literal in Java ?
http://stackoverflow.com/questions/2160788/what-is-a-class-literal-in-java
Understanding Class.forName()(请见附件pdf文档):
http://www.theserverside.com/news/1365412/Understanding-ClassforName-Java



例子:
public class Parent {

    public static void main(String[] args) throws Exception {
        
        
        Class c1 = void.class;
        Class c2 = int.class;
        Class c3 = Integer.class;
        System.out.println(c2 == c3); //false
        //Integer i = (Integer)c3.newInstance(); //会报InstantiationException,原因是Integer没有无参构造方法
        
        Class clazz1 = String.class;
        String string = (String)clazz1.newInstance();
        string = "ooo";
        System.out.println(string);
        Class clazz2 = Class.class;
        ///Class clazz3 = Class.forName("package.MyClass"); //必须处理ClassNotFoundException
        
        String str = "abc";
        Class clazz4 = str.getClass();
        
        Class clazz5 = clazz1.getClass().getClass().getClass();
        //Class clazz6 = String.class.class; //编译错误
        
        Class clazz7 = System.out.getClass().getClass();
        //Class clazz8 = System.out.class; //编译错误
        
        System.out.println(String.class == "abc".getClass()); //true
        System.out.println(Class.class == clazz1.getClass()); //true
        System.out.println(Class.class == clazz4.getClass()); //true
        System.out.println(String.class == "abc".getClass().getClass()); //false
        System.out.println(Class.class == clazz7.getClass().getClass()); //true
        
        Parent p = new Parent();
        System.out.println(p.getClass() == Parent.class); //true
        System.out.println(p.getClass().getClass() == Parent.class); //false
        
        Parent p2 = new Child();
        System.out.println(p2.getClass() == Parent.class); //false
        System.out.println(p2.getClass() == Child.class); //true
        System.out.println(p2.getClass().getClass() == Child.class); //false
        
        //System.out.println(String.class == Parent.class); //编译错误:Incompatible operand types  Class<String> and Class<Parent>
        System.out.println("abc".getClass() == p.getClass()); //false
    }

}

class Child extends Parent {
    
}



http://juixe.com/techknow/index.php/2006/05/08/javaclass/
引用
In Java, given an object, an instance of a class, you can get the class name by coding the following:

Class clazz = obj.getClass();
String clazzName = clazz.getName();
Sometimes you want you want to create a Class object for a given class. In this case you can do so by writing code similar to the following example:

Class clazz = MyClass.class;
Many plugin frameworks, including the JDBC Driver Manager will create a Class object without having the knowledge of what class name at compile time. There might be a case where you know a class implements a given interface but you don’t know the class name of the implementation until at runtime when you read it from a properties file. In situations like this you can do the following:

String clazzName = "com.juixe.techknow.MyClass";
...
Class clazz = Class.forName(clazzName);
Sometimes you will need a Class object for a primitive type. You might need a Class object for an int or boolean when dealing with reflection. In this case you do so using the dot class notation on a primitive type. Here is a more elaborate example where we create a Class object for an int primitive:

int newValue = ...
Class clazz = obj.getClass();
Method meth = clazz.getMethod("setValue", new Class[]{int.class});
meth.invoke(obj, new Object[]{new Integer(newValue)});
You can also get the class for an array. The only place where I have ever need the class of an array is when working with reflection. Here is how you can get the class for an array:

Class clazz = String[].class;

分享到:
评论

相关推荐

    java中的Class类和反射.docx

    - **`public static Class&lt;?&gt; forName(String className)`**:这是一个原生方法,用于动态加载类。这个方法非常关键,比如在SQL中动态加载数据库驱动时会用到:`Class.forName("com.mysql.jdbc.Driver")`。 - **`...

    实训商业源码-百川多公众号集字福袋 2.0.5开源-论文模板.zip

    实训商业源码-百川多公众号集字福袋 2.0.5开源-论文模板.zip

    S7-200 Smart PLC在卷材与造纸设备中的速度频率同步控制程序应用

    内容概要:本文详细介绍了基于S7-200 Smart PLC的速度与频率同步控制程序及其在卷板材生产线和造纸设备中的应用。该程序旨在确保生产设备间的同步运行,提高生产效率和产品质量。文中涵盖了程序的总体架构、关键变量定义、主程序流程及其实现方式,并讨论了多机同步控制的具体方法。此外,还提供了部分关键代码示例,帮助读者更好地理解和实施该程序。最后,强调了编写和调试过程中应注意的问题,并对未来的发展进行了展望。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是对PLC编程和变频器控制有一定了解的专业人士。 使用场景及目标:①适用于卷板材生产线和造纸设备的同步控制;②确保多个设备之间的速度和频率保持一致,提升生产效率和产品品质;③支持多种品牌变频器(如ABB、英威腾等),满足不同应用场景的需求。 其他说明:本文不仅提供了理论指导,还包括具体的操作步骤和代码实例,有助于读者快速上手并在实践中不断优化和完善程序。

    毕业论文-柚子社区团购 1.3.17-整站商业源码.zip

    毕业论文-柚子社区团购 1.3.17-整站商业源码.zip

    simscape 单质量弹簧阻尼模型

    simscape 单质量弹簧阻尼模型

    基于Matlab Simulink的柴油发电机与风光储能微电网仿真技术研究

    内容概要:本文详细介绍了柴油发电机在微电网中的仿真技术,特别是结合了风光发电与储能技术的研究。首先,文章阐述了微电网作为未来能源系统重要组成部分的背景,强调了柴油发电机的关键作用及其仿真技术的重要性。接着,文章具体讲解了如何利用Matlab Simulink构建柴油发电机仿真模型,涵盖模型建立、参数设置和优化改进等方面。此外,还讨论了风光柴储微电网仿真技术,重点在于应对风光发电的不确定性和储能电池的充放电特性。最后,通过具体案例分析,展示了微电网中风机光伏柴油储能系统的复杂性和挑战性,突出了仿真技术在微电网建设与运行中的重要作用。 适合人群:从事电力系统、微电网研究的技术人员和研究人员,尤其是对柴油发电机仿真技术和风光储能感兴趣的学者。 使用场景及目标:适用于希望深入了解柴油发电机仿真技术及其在微电网中应用的专业人士。目标是掌握如何使用Matlab Simulink进行柴油发电机仿真,理解风光柴储微电网的工作原理和优化策略。 阅读建议:读者可以通过本文全面了解柴油发电机仿真技术的基本概念、建模方法和应用场景,尤其关注仿真参数的设置和优化改进部分,以便更好地应用于实际项目中。

    电子工程领域锁相环(PLL)设计与进阶应用:理论与实践结合

    内容概要:本文详细介绍了锁相环(PLL)的工作原理及其设计方法,涵盖基本组成部分(鉴相器、环路滤波器、压控振荡器),以及设计流程(确定指标、选型、参数计算)。此外还讨论了PLL的高级特性,如低相位噪声设计、宽带PLL设计和数字化PLL的趋势。文中不仅提供了理论解释,还有具体的实例和Python代码演示,帮助读者全面掌握PLL的设计要点和技术细节。 适合人群:电子工程专业学生、从事通信与时钟同步相关工作的工程师。 使用场景及目标:适用于希望深入了解PLL工作原理及其设计方法的专业人士,旨在提高他们在实际项目中应用PLL的能力。 其他说明:文章结合了大量图表和公式推导,有助于加深理解和记忆。同时给出了具体的应用案例,便于读者将所学应用于实践。

    毕业设计-家政小程序V6.1.2+分销插件V1.0.2-整站商业源码.zip

    毕业设计-家政小程序V6.1.2+分销插件V1.0.2-整站商业源码.zip

    实训商业源码-砍价宝6.4.0小程序前端+后端-论文模板.zip

    实训商业源码-砍价宝6.4.0小程序前端+后端-论文模板.zip

    光伏用户群电能共享与需求响应模型:基于市场模式的定价策略、纳什均衡与分布式优化

    内容概要:本文研究了市场模式下光伏用户群的电能共享与需求响应模型,重点探讨了定价策略、需求响应机制以及博弈论的应用。首先,介绍了光伏用户通过集群方式实现电能共享,可以在光伏上网电价低于市电电价的环境中获得更高的经济效益。其次,提出了一种基于光伏电能供需比的内部价格模型,以引导电能交易并最大化经济效益。接着,构建了一个考虑经济性和舒适度的需求响应效用成本模型,以鼓励用户参与需求响应。最后,通过引入博弈论,分析了需求响应行为形成的非合作博弈问题,并提出了分布式优化算法求解纳什均衡策略。通过实际算例验证了模型的有效性,减少了用电成本并提高了光功率互用水平。 适合人群:从事新能源研究、电力系统优化、智能电网设计的专业人士,以及对光伏技术和市场需求响应感兴趣的学者和技术人员。 使用场景及目标:适用于研究和设计光伏用户群的电能共享与需求响应系统,帮助优化能源结构,降低用电成本,提升经济效益。目标是在市场模式下实现光伏用户间的高效电能交易,推动绿色能源的发展。 其他说明:本文不仅提供了理论分析,还通过MATLAB仿真进行了实证研究,为实际应用提供了有力支持。

    基于滑动平均算法的功率波动处理及优化以满足国标并网标准

    内容概要:本文详细介绍了如何利用滑动平均算法(MA)处理电力系统中的功率波动,确保其符合国家并网标准。首先解释了功率波动的概念及其对电力系统的影响,接着阐述了滑动平均算法的基本原理和实现步骤,包括设置不同时间窗口(1分钟和10分钟)来平滑功率数据。随后讨论了如何计算滑动后的最大功率波动以及如何调整滑动窗口参数以达到最佳效果。最后,提出了合理的功率分配策略,以确保最终输出既稳定又高效地满足国家标准。 适合人群:从事电力系统研究和技术实施的专业人士,尤其是关注功率波动处理和并网标准的技术人员。 使用场景及目标:适用于需要解决电力系统中功率波动问题的实际工程环境,旨在帮助技术人员理解和应用滑动平均算法,从而提升电力系统的稳定性和效率。 其他说明:文中提供了详细的理论背景和技术细节,有助于深入理解滑动平均算法的应用,并指导具体的工程实践。

    实训商业源码-扫码点餐小程序5.9.8 跑腿1.9.5-论文模板.zip

    实训商业源码-扫码点餐小程序5.9.8 跑腿1.9.5-论文模板.zip

    毕业设计-百川多公众号集字福袋V2.2.9全开源解密版-整站商业源码.zip

    毕业设计-百川多公众号集字福袋V2.2.9全开源解密版-整站商业源码.zip

    毕业论文-中秋博饼V4.1.5 开源版-整站商业源码.zip

    毕业论文-中秋博饼V4.1.5 开源版-整站商业源码.zip

    实训商业源码-webstack开源导航源码本地静态化版-论文模板.zip

    实训商业源码-webstack开源导航源码本地静态化版-论文模板.zip

    基于庞特里亚金极小值原理的燃料电池混合动力系统能量管理策略及其MATLAB实现

    内容概要:本文探讨了基于庞特里亚金极小值原理的燃料电池混合动力系统能量管理策略的设计与实现。首先介绍了庞特里亚金极小值原理作为一种优化方法的应用背景,然后详细阐述了燃料电池混合动力系统的组成和特点。接着,文章提出了一个综合考虑系统能耗、排放和性能衰退的目标函数,并建立了相应的约束条件。利用庞特里亚金极小值原理,在MATLAB平台上实现了这一能量管理策略,确保其能在多种工况下有效运行。最后,通过对策略的测试和调试,验证了其可行性和优越性。 适合人群:从事新能源汽车研究、混合动力系统设计以及对优化理论感兴趣的科研人员和技术开发者。 使用场景及目标:①为燃料电池混合动力系统提供高效的能量管理解决方案;②帮助研究人员理解和应用庞特里亚金极小值原理于实际工程问题;③促进新能源车辆的技术进步和发展。 其他说明:文中提供的MATLAB代码具有良好的扩展性和适应性,可以根据不同的应用场景调整参数设置,满足多样化的研究需求。

    FPGA编程VHDL实践:RS422/485串口通信设计与测试

    内容概要:本文详细介绍了利用FPGA和VHDL语言实现RS422与RS485串口通信的方法。首先阐述了设计前准备工作的必要性,包括系统要求、接口规格、时钟频率以及数据处理方式等关键要素的确立。接着深入探讨了VHDL程序的编写流程,涵盖信号属性定义、模块构建、逻辑电路设计等方面的内容。随后强调了仿真环节对于检测程序缺陷的重要性,最后讲述了上板测试阶段的操作要点,确保最终成果满足预期性能指标。 适合人群:从事嵌入式系统开发的技术人员,尤其是对FPGA编程感兴趣的工程师。 使用场景及目标:适用于希望掌握FPGA编程技巧,特别是针对串行通信接口(RS422/485)的实际项目开发者。目标是在实践中学会从零开始创建完整的FPGA应用程序,包括但不限于设计规划、代码编写、模拟验证和实物部署。 其他说明:文中不仅提供了理论指导,还给出了具体的实施路径,有助于读者更好地理解和应用相关知识。

    表贴式永磁同步电机一阶线性非线性自抗扰(ADRC)Matlab Simulink模型及其ESO应用研究

    内容概要:本文详细探讨了表贴式永磁同步电机在一阶线性非线性自抗扰(ADRC)控制下的Matlab Simulink建模与仿真。首先介绍了表贴式永磁同步电机的工作原理及其优势,随后重点阐述了一阶线性非线性自抗扰技术的特点和应用场景。文中展示了如何在Matlab Simulink平台上构建电机模型,包括基本结构、控制算法实现及反馈信息处理等方面的内容。特别强调了扩张状态观测器(ESO)的应用,它用于实时估计电机状态并提升系统的稳定性和动态响应。最后通过对仿真实验的数据分析验证了所提模型的有效性。 适合人群:从事电机控制系统研究的专业人士、高校师生及相关科研工作者。 使用场景及目标:适用于需要深入了解表贴式永磁同步电机控制机制的研究项目;旨在探索更优的电机控制策略以提高系统效率和可靠性。 其他说明:文章不仅提供了理论分析,还有具体的模型实例和实验数据支持,便于读者理解和实践。

    基于阶梯碳交易的P2G-CCS与燃气掺氢虚拟电厂优化调度研究及MATLAB复现

    内容概要:本文详细探讨了如何结合P2G-CCS(Power-to-Gas with Carbon Capture and Storage)技术和燃气掺氢技术,进行虚拟电厂的阶梯碳交易优化调度。文章首先介绍了背景,强调了在全球环保趋势下,阶梯碳交易机制的重要性。接着,分别建立了掺氢燃气轮机、掺氢燃气锅炉、两段式电转气和碳捕集系统的数学模型,描述了各组件的运行特性和碳排放情况。随后,构建了阶梯碳交易模型,设定了不同碳排放量的价格阶梯,以激励减排。基于这些模型,提出了以碳交易成本、购气和煤耗成本、碳封存成本、机组启停成本和弃风成本之和最低为目标函数的优化调度策略。最后,利用Matlab、Yalmip和Cplex进行了详细的复现过程,展示了不同情景下的优化调度结果。 适合人群:从事能源管理、电力系统优化、碳交易研究的专业人士,以及对虚拟电厂和低碳技术感兴趣的科研人员。 使用场景及目标:适用于希望深入了解虚拟电厂优化调度策略的研究人员和技术人员,旨在帮助他们掌握P2G-CCS和燃气掺氢技术在低碳政策下的应用方法,从而制定有效的节能减排措施。 其他说明:本文提供了详细的复现过程和丰富的图表数据,便于读者理解和验证优化调度策略的有效性。

Global site tag (gtag.js) - Google Analytics