01 |
public class Test
{ |
02 |
//
需要反射的方法 |
03 |
public Iphone5S
test4(Iphone5S iphone){ |
04 |
|
05 |
06 |
System.out.println("test4
运行了!!!!"); |
07 |
return iphone; |
08 |
} |
09 |
10 |
} |
01 |
public class Iphone5S
{ |
02 |
private int price; |
03 |
private String
volume ; |
04 |
|
05 |
public int getPrice()
{ |
06 |
return price; |
07 |
} |
08 |
public void setPrice(int price)
{ |
09 |
this.price
= price; |
10 |
} |
11 |
public String
getVolume() { |
12 |
return volume; |
13 |
} |
14 |
public void setVolume(String
volume) { |
15 |
this.volume
= volume; |
16 |
} |
17 |
|
18 |
} |
01 |
public static void main(String[]
args) throws Exception
{ |
02 |
|
03 |
|
04 |
Class
iphone5sClass = ClassUtils.getClass("test.Iphone5S"); |
05 |
|
06 |
Object
iphone5s = iphone5sClass.newInstance(); |
07 |
ClassUtils.setPropertyValue(iphone5s, "price", 5899); |
08 |
ClassUtils.setPropertyValue(iphone5s, "volume", "16g"); |
09 |
|
10 |
|
11 |
Class
clazz = ClassUtils.getClass("test.Test"); |
12 |
Method
test4 = clazz.getMethod("test4",
iphone5sClass); |
13 |
14 |
|
15 |
test4.invoke(clazz.newInstance(),
iphone5s); |
16 |
17 |
18 |
} |
异常信息:
Exception in thread "main" java.lang.NoSuchMethodException: test.Test.test4(test.Iphone5S)
at java.lang.Class.getMethod(Class.java:1605)
at Test.demo.Main.main(Main.java:33)
451

被折叠的 条评论
为什么被折叠?



