if(obj instanceof Dog) {
Dog d = (Dog)obj;
d.bark();
}
// with reflection
Class <!--?--> c = Class.forName("classpath.and.classname");
Object dog = c.newInstance();
Method m = c.getDeclaredMethod("bark", new Class<!--?-->[0]);
m.invoke(dog);
<bean class="com.programcreek.Foo" id="someID"> <property name="someField" value="someValue"> </property></bean>
<code><code><code><code><code><code><servlet> <servlet name="">someServlet <servlet>com.programcreek.WhyReflectionServlet</servlet> <servlet data-filtered="filtered"></servlet></servlet></servlet></code></code></code></code></code></code>
package com.longluo.java.interview.reflection;
public class ReflectionHelloWorld {
public static void main(String[] args) {
Foo f = new Foo();
System.out.println(f.getClass().getName());
}
}
class Foo {
public void print() {
System.out.println("abc");
}
}
com.longluo.java.interview.reflection.Foo
package com.longluo.java.interview.reflection;
import java.lang.reflect.Method;
public class ReflectionHelloWorld {
/*
* public static void main(String[] args) { Foo f = new Foo();
* System.out.println(f.getClass().getName()); }
*/
public static void main(String[] args) {
Foo f = new Foo();
Method method;
try {
method = f.getClass().getMethod("print", new Class<!--?-->[0]);
method.invoke(f);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Foo {
public void print() {
System.out.println("abc");
}
}
abc
package com.longluo.java.interview.reflection;
import java.lang.reflect.Method;
public class ReflectionHelloWorld {
public static void main(String[] args) {
// create instance of "Class"
Class<!--?--> c = null;
try {
c = Class.forName("com.longluo.java.interview.reflection.Foo");
} catch (Exception e) {
e.printStackTrace();
}
// create instance of "Foo"
Foo f = null;
try {
f = (Foo) c.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
f.print();
}
}
class Foo {
public void print() {
System.out.println("abc");
}
}
package com.longluo.java.interview.reflection;
import java.lang.reflect.Constructor;
public class ReflectionHelloWorld4 {
public static void main(String[] args) {
// create instance of "Class"
Class<!--?--> c = null;
try {
c = Class.forName("com.longluo.java.interview.reflection.Foo4");
} catch (Exception e) {
e.printStackTrace();
}
// create instance of "Foo"
Foo4 f1 = null;
Foo4 f2 = null;
// get all constructors
Constructor<!--?--> cons[] = c.getConstructors();
try {
f1 = (Foo4) cons[0].newInstance();
f2 = (Foo4) cons[1].newInstance("abc");
} catch (Exception e) {
e.printStackTrace();
}
f1.print();
f2.print();
}
}
class Foo4 {
String s;
public Foo4() {
}
public Foo4(String s) {
this.s = s;
}
public void print() {
System.out.println(s);
}
}
null abc
package com.longluo.java.interview.reflection;
import java.lang.reflect.Array;
public class ReflectionHelloWorld5 {
public static void main(String[] args) {
int[] intArray = { 1, 2, 3, 4, 5 };
int[] newIntArray = (int[]) changeArraySize(intArray, 10);
print(newIntArray);
String[] atr = { "a", "b", "c", "d", "e" };
String[] str1 = (String[]) changeArraySize(atr, 10);
print(str1);
}
// change array size
public static Object changeArraySize(Object obj, int len) {
Class<!--?--> arr = obj.getClass().getComponentType();
Object newArray = Array.newInstance(arr, len);
// do array copy
int co = Array.getLength(obj);
System.arraycopy(obj, 0, newArray, 0, co);
return newArray;
}
// print
public static void print(Object obj) {
Class<!--?--> c = obj.getClass();
if (!c.isArray()) {
return;
}
System.out.println("\nArray length:" + Array.getLength(obj));
for (int i = 0; i < Array.getLength(obj); i++) {
System.out.print(Array.get(obj, i) + " ");
}
}
}
Array length:10 1 2 3 4 5 0 0 0 0 0 Array length:10 a b c d e null null null null null
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有