Java 反射机制 .class .getClass() Class.forName("") .TYPE int void
2016-02-23 09:47
609 查看
package com.age.www;
public class ReflectionDemo {
public static void main(String[] args) {
//对象名.getClass()产生Class对象
Emploree1 e=new Emploree1();
Class<?> emploree=e.getClass();
System.out.println(emploree.getName());
//Class.forName("")静态方法:
try {
try {
Emploree1 emp1=(com.age.www.Emploree1)Class.forName("com.age.www.Emploree1").newInstance();
emp1.tell();
} catch (InstantiationException | IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// .class直接生成Class对象
try {
Emploree1.class.newInstance().tell();
} catch (InstantiationException | IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//8中包装类的.TYPE属性 也报错8中基本类型都可以用.class ...表示基本类型double int 等实例。 没有父类,就是基本类型实例。 不能有newInstance方法。
//System.out.println(Boolean.TYPE.getSuperclass().getName());
Class cl=Boolean.TYPE;
Class<?> c2=Double.TYPE;
//c2.newInstance();
//System.out.println(c2.getSuperclass());
System.out.println("Double.TYPE.getName() "+Double.TYPE.getName());
System.out.println("Double.class.getName() "+Double.class.getName());
Class c3=Double.class; // .class总能找到父类,基本是基本数据类型的包装类型。
System.out.println(c3.getSuperclass().getName());
// try {
// //Number n1=(Number)c2.newInstance();
//
//
// //Boolean bl=(Boolean)cl.newInstance();
// //System.out.println(bl.toString());
// //System.out.println(bl.hashCode());
// } catch (InstantiationException | IllegalAccessException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
}
}
class Emploree1 {
private String name="zhangsan";
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;
}
private int age=20;
public void tell(){
System.out.println(name+age);
}
}
public class ReflectionDemo {
public static void main(String[] args) {
//对象名.getClass()产生Class对象
Emploree1 e=new Emploree1();
Class<?> emploree=e.getClass();
System.out.println(emploree.getName());
//Class.forName("")静态方法:
try {
try {
Emploree1 emp1=(com.age.www.Emploree1)Class.forName("com.age.www.Emploree1").newInstance();
emp1.tell();
} catch (InstantiationException | IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// .class直接生成Class对象
try {
Emploree1.class.newInstance().tell();
} catch (InstantiationException | IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//8中包装类的.TYPE属性 也报错8中基本类型都可以用.class ...表示基本类型double int 等实例。 没有父类,就是基本类型实例。 不能有newInstance方法。
//System.out.println(Boolean.TYPE.getSuperclass().getName());
Class cl=Boolean.TYPE;
Class<?> c2=Double.TYPE;
//c2.newInstance();
//System.out.println(c2.getSuperclass());
System.out.println("Double.TYPE.getName() "+Double.TYPE.getName());
System.out.println("Double.class.getName() "+Double.class.getName());
Class c3=Double.class; // .class总能找到父类,基本是基本数据类型的包装类型。
System.out.println(c3.getSuperclass().getName());
// try {
// //Number n1=(Number)c2.newInstance();
//
//
// //Boolean bl=(Boolean)cl.newInstance();
// //System.out.println(bl.toString());
// //System.out.println(bl.hashCode());
// } catch (InstantiationException | IllegalAccessException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
}
}
class Emploree1 {
private String name="zhangsan";
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;
}
private int age=20;
public void tell(){
System.out.println(name+age);
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序