在java语言中万事万物皆为对象,例如咱们写一个person类那么咱们就能够经过这个类来创造一些实例,这里的“实例”就是咱们平时使用的对象。java
public class Person { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
ppublic class Referce { public static void main(String[] args) { Person wangwu=new Person(); Person zhaosi=new Person(); wangwu.setName("王五"); wangwu.setAge(18); wangwu.setSex("male"); zhaosi.setName("赵四"); zhaosi.setAge(22); zhaosi.setSex("female"); } }
在上面的代码中的“wangwu”、“zhaosi”就是咱们所创造出来的对象,也就是说“Person”至关于一个模板,咱们能够经过这个模板来创造“wangwu”、“zhaosi”这些具体的对象。举个通俗的栗子“Person”就至关于咱们用来制做月饼的模具,“wangwu”、"zhaosi"就是咱们能吃的月饼了。this
但人是一种喜欢思考的动物(因此智人才在尼安德特人 、鲁道夫人 、直立人等中被“天然选择”了出来,站到食物链顶端),那么咱们不由要想Person这个类模板是由谁创造的呢? 也就是说制做月饼模具的机床又在哪呢?
spa
那么下来就该咱们的Class类粉墨登场了,它位于java.lang这个包下因为这个源码过长故在此贴出它的部分源码:code
public final class Class<T> implements java.io.Serializable, GenericDeclaration, Type, AnnotatedElement { private static final int ANNOTATION= 0x00002000; private static final int ENUM = 0x00004000; private static final int SYNTHETIC = 0x00001000; private static native void registerNatives(); static { registerNatives(); } /* * Private constructor. Only the Java Virtual Machine creates Class objects. * This constructor is not used and prevents the default constructor being * generated. */ private Class(ClassLoader loader) { // Initialize final field for classLoader. The initialization value of non-null // prevents future JIT optimizations from assuming this final field is null. classLoader = loader; } /** * Converts the object to a string. The string representation is the * string "class" or "interface", followed by a space, and then by the * fully qualified name of the class in the format returned by * {@code getName}. If this {@code Class} object represents a * primitive type, this method returns the name of the primitive type. If * this {@code Class} object represents void this method returns * "void". * * @return a string representation of this class object. */ public String toString() { return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) + getName(); }
咱们能够看出它是一个final类(整个类都是final,就代表不但愿从这个类被继承,不但愿被修改)下面咱们将具体操做这个类看看它是怎么来生产咱们的类模具的。orm
public class Test { public static void main(String[] args) { Class<Person> clazz=Person.class; System.out.println(clazz.getName()); } } 输出结果是:com.tl.referce.Person
从输出结果上咱们能够看出这个clazz对象就是Person的类模具了,那么既然如今获取到了Person的类模具了那么是否是就能够利用它(clazz)来生产Person对象了呢?对象
public class Test { public static void main(String[] args) { Class<Person> clazz=Person.class; try { Person wangwu=clazz.newInstance(); wangwu.setName("王五"); wangwu.setAge(18); wangwu.setSex("male"); System.out.println("姓名:"+wangwu.getName()+",年龄:"+wangwu.getAge()+",性别:"+wangwu.getSex()); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } 输出结果是:姓名:王五,年龄:18,性别:male
从输出结果咱们能够看出,咱们已经正确的拿到了Person类的类模具并且成功的利用这个模具创造出了“wangwu”这个对象。
继承
实际中咱们还能够经过其余两种方法来获取Person类的类模具,下面咱们来具体演示:get
public class Test { public static void main(String[] args) { Person wangwu=new Person(); Class<Person> clazz1=(Class<Person>) wangwu.getClass(); System.out.println(clazz1.getName()); System.out.println("========================================="); try { Class<Person> clazz2=(Class<Person>) Class.forName("com.tl.referce.Person"); System.out.println(clazz2.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } 输出结果: com.tl.referce.Person ========================================= com.tl.referce.Person
从输出结果能够看出这三种方法均可以获取Person类的类模具,“wangwu.getClass()”这种方法是咱们经过一个具体的对象来获取类模具,至关于经过一个五仁月饼来反向制造出五仁月饼的具。“Class.forName("com.tl.referce.Person")”至关于咱们把一个生产模具的图纸来交给机床,机床根据这个图纸来生产对应的模具。源码