package com.sitech.ordersvc.group.comp.busi.basebusi;
//有关反射学习
public class Test1 {
public void plus(Integer a,Integer b){
Integer c =a+b;
System.out.println("结果是"+c);
}
public static void main(String[] args) {
//第一种
String a ="com.sitech.ordersvc.group.comp.busi.basebusi.Test1";
try {
Class class1 =Class.forName(a);
try {
Test1 test1 = (Test1)class1.newInstance();
test1.plus(1, 2);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//第二种
Class clazz = Test1.class;
try {
Test1 test2 = (Test1) clazz.newInstance();
test2.plus(3, 4);
} catch (Exception e) {
e.printStackTrace();
}
//第三种()第二种类似,更安全一些
Class<Test1> clazz2 =Test1.class;
try {
Test1 a2 =clazz2.newInstance();
a2.plus(5, 6);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//有关反射学习
public class Test1 {
public void plus(Integer a,Integer b){
Integer c =a+b;
System.out.println("结果是"+c);
}
public static void main(String[] args) {
//第一种
String a ="com.sitech.ordersvc.group.comp.busi.basebusi.Test1";
try {
Class class1 =Class.forName(a);
try {
Test1 test1 = (Test1)class1.newInstance();
test1.plus(1, 2);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//第二种
Class clazz = Test1.class;
try {
Test1 test2 = (Test1) clazz.newInstance();
test2.plus(3, 4);
} catch (Exception e) {
e.printStackTrace();
}
//第三种()第二种类似,更安全一些
Class<Test1> clazz2 =Test1.class;
try {
Test1 a2 =clazz2.newInstance();
a2.plus(5, 6);
} catch (Exception e) {
e.printStackTrace();
}
}
}