类:
package echo;
public class GetClass {
private String name;
private String age;
private String sex;
public GetClass(){
this.name="zhangsan";
this.age="12";
this.sex="1";
}
public String getPerson(){
String person="";
person=this.getName()+" "+this.getAge()+" "+this.getSex();
return person;
}
public String getNameAndSex(){
String info = "";
info = this.getName()+" "+this.getSex();
return info;
}
public String getNameAndAge(){
String info = "";
info = this.getName()+" "+this.getAge();
return info;
}
public void printSuccessful(){
System.out.println("print successful......!");
}
public void printSuccessful(String str1, String str2){
System.out.println("print2 successful......!");
System.out.println(str1 + "," + str2);
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试类:
package test;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import echo.GetClass;
public class Test extends TestCase {
public void testGetClass(){
GetClass gc = new GetClass();
String gClass = "echo.GetClass";
try {
//得到类对象
Class c = Class.forName(gClass);
//得到类实例
Object obj = c.newInstance();
//通过类对象获得类的内部方法
Method m = c.getMethod("printSuccessful",null);
//得到方法对象后用类实例执行方法
m.invoke(obj,null);
//多个参数的写法
m = c.getMethod("printSuccessful",new Class[]{String.class,String.class});
m.invoke(obj,new String[]{"111","222"});
//得到类名包含包在内 例如:echo.GetClass
System.out.println(c.getName());
} catch(Exception e){
System.out.println(e.toString());
}
}
}
package echo;
public class GetClass {
private String name;
private String age;
private String sex;
public GetClass(){
this.name="zhangsan";
this.age="12";
this.sex="1";
}
public String getPerson(){
String person="";
person=this.getName()+" "+this.getAge()+" "+this.getSex();
return person;
}
public String getNameAndSex(){
String info = "";
info = this.getName()+" "+this.getSex();
return info;
}
public String getNameAndAge(){
String info = "";
info = this.getName()+" "+this.getAge();
return info;
}
public void printSuccessful(){
System.out.println("print successful......!");
}
public void printSuccessful(String str1, String str2){
System.out.println("print2 successful......!");
System.out.println(str1 + "," + str2);
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试类:
package test;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import echo.GetClass;
public class Test extends TestCase {
public void testGetClass(){
GetClass gc = new GetClass();
String gClass = "echo.GetClass";
try {
//得到类对象
Class c = Class.forName(gClass);
//得到类实例
Object obj = c.newInstance();
//通过类对象获得类的内部方法
Method m = c.getMethod("printSuccessful",null);
//得到方法对象后用类实例执行方法
m.invoke(obj,null);
//多个参数的写法
m = c.getMethod("printSuccessful",new Class[]{String.class,String.class});
m.invoke(obj,new String[]{"111","222"});
//得到类名包含包在内 例如:echo.GetClass
System.out.println(c.getName());
} catch(Exception e){
System.out.println(e.toString());
}
}
}