标签:catch row edit OLE private trace 自定义 保留 扩展名
导读:很多优秀的高级框架都是通过反射完成的,反射的重要性,由此可见一斑。反射机制可以使得程序更加灵活,只有学习好反射的基础语法,这样才能自己写出优秀的框架。好了一起打卡学习吧,别忘记了素质三连哦!
往期精彩回放:一文搞定Java的输入输出流等常见流
package Day2;
public class ReflectTest1 {
public static void main(String[] args) {
Class s1 = null;
try {
//通过反射机制获取Class
s1= Class.forName("Day2.User");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Object a=null;
try {
a =s1.newInstance();//用反射获得的Class,创建一个对象
System.out.println(a);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
class User{
public User() {
System.out.println("无参构造方法!");
}
}
package Day2;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class ReflectTest2 {
public static void main(String[] args) throws Exception {
//通过IO流读取flie.properties文件
FileReader reader = new FileReader("file.properties");
//创建属性类对象(Properties对象,Map集合,key和value都是字符串类型
Properties pro = new Properties();
//将文件中的数据加载到Map集合中加载
pro.load(reader);
reader.close();//这个流不能在没加载数据之前就关闭了
//通过key获取value
String a = pro.getProperty("ClassName");
Class bb = Class.forName(a);//反射获得Class
Object c = bb.newInstance();//通过Class,新建一个对象
System.out.println(c);
}
}
class User1 {
public User1() {
System.out.println("无参构造方法!");
}
}
public class Path1 {
public static void main(String[] args) {
String path = Thread.currentThread().getContextClassLoader().getResource("File2").getPath();
System.out.println(path);
}
}
package Day2;
import java.io.InputStream;
import java.util.Properties;
public class Path2 {
public static void main(String[] args) throws Exception {
//创建一个流,读取文件
InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream("File2");
//创建一个Properties集合,保存数据
Properties pro = new Properties();
pro.load(reader);//将文件数据加载到集合中
reader.close();
String f =pro.getProperty("NewName");
Class bb = Class.forName(f);//反射获得Class
Object cc = bb.newInstance();//通过Class实例化对象
System.out.println(cc);
}
}
class User2 {
public User2() {
System.out.println("无参构造方法!");
}
}
import java.util.ResourceBundle;
public class Path3 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("File2");
String Name = bundle.getString("NewName");
System.out.println(Name);
}
}
package Day1;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class FieldTest1 {
public static void main(String[] args) {
Class First=null;
try {
First = Class.forName("Day1.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field[] fields = First.getDeclaredFields();
System.out.println(fields.length);
for(Field arg:fields) {
int a = arg.getModifiers();
System.out.println(Modifier.toString(a));//Modefier为reflect的子类
Class ff = arg.getType();
System.out.println(ff.getName());
System.out.println(arg.getName());
}
}
}
class Student{
public String name;
private int age;
protected double scroe;
boolean flag;
}
package Day1;
import java.lang.reflect.Field;
public class FieldTest2 {
public static void main(String[] args) {
Class fio = null;//获取Class
Object ff = null;
{
try {
fio = Class.forName("Day1.Student");
ff = fio.newInstance();//实例化对象
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
//获取属性
Field fl = null;
{
try {
fl = fio.getDeclaredField("name");//通过类获取属性,无法通过对象获取
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
//给fio对象的fl属性赋值
try {
fl.set(ff,"sfdsa");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
//获取属性的值
try {
System.out.println(fl.get(ff));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
反射Mothod
package Day1;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MethodTest1 {
public static void main(String[] args) {
Class fos =null;
try {
fos = Class.forName("Day1.Student1");//获取Class
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Object fis =null;
try {
fis = fos.newInstance();//创建对象
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Method method =null;
try {
method = fos.getDeclaredMethod("GetScore",String.class,double.class);//获取方法
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Object value = null;
try {
value = method.invoke(fis,"jia",520);//调用方法
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(value);
}
}
class Student1{
private String name;
private int age;
private double score;
public String GetScore(String name,double score){
if(score>500){
System.out.println("恭喜成功!");
}
else {
System.out.println("继续加油!");
}
return name;
}
}
package Day1;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ConstructorTest1 {
public static void main(String[] args){
Class fio=null;
Constructor con=null;
Object cc = null;
try {
//获取类Class
fio = Class.forName("Day1.Student3");
//获取构造方法,参数很重要
con =fio.getDeclaredConstructor(int.class,String.class);
cc= con.newInstance(11,"Jia");//调用构造方法new对象,这种方法没有过期
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//输出对象
System.out.println(cc);//自动调用toString方法
}
}
class Student3{
private int age;
private String name;
//构造方法
public Student3() {
}
public Student3(int age, String name) {
this.age = age;
this.name = name;
System.out.println("调用成功!");
}
}
package Day1;
public class SuperClassTest1 {
public static void main(String[] args){
Class fos =null;
Class superClass=null;
try {
//获取类的Class
fos= Class.forName("Day1.Student5");
//获取父类的Class
superClass = fos.getSuperclass();
//实例化父类对象
Object cc = superClass.newInstance();
System.out.println(cc);
//获取子类的所有接口
Class[] interfaces = superClass.getInterfaces();
for(Class ff : interfaces){
//获取接口的简称,即不含包名
System.out.println(ff.getSimpleName());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
//学生类
class Student5 extends Parents{
private int age;
private String name;
//构造方法
public Student5() {
}
public Student5(int age, String name) {
this.age = age;
this.name = name;
System.out.println("调用成功!");
}
}
//家长类
class Parents implements Study,Sport{
public Parents() {
super();
}
}
//接口
interface Study{
}
interface Sport{
}
如何获取注解类的属性?
1.获取类Class
* Class dd = Class.forName(“完整类名”);
2.判断类上面是否存在特定的注解。
* if(dd(类).isAnnotationPresent(注解类名.class)
3.如果存在特定的注解,则获取注解对象。
* 注解类名 fs = (注解类名)dd.getAnnotation(注解类名.class);
4.获取注解对象的属性。
* 注解对象.属性();
如果获取方法上的注解信息?
1.获取类Class。
2.获取方法。
3.判断方法上是否存在该注解。存在的话,获取方法上的注解。调用方式为:方法.getAnnotation(注解类名.class)
4.再通过得到的注解获得属性。
注解在开发中的使用场景:
举个栗子:假设存在一个注解,只能出现在类上面;当这个类有这个注解:类必须有int类型的属性,否则报异常。
原创不易,码字不易,点个赞再走吧!
往期精彩回放:一文搞定Java的输入输出流等常见流
标签:catch row edit OLE private trace 自定义 保留 扩展名
原文地址:https://www.cnblogs.com/huangjiahuan1314520/p/12698754.html