public class People {
private String name;
public People(){
this.name = "默认名字";
}
public People(String str) {
name = str;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void positive_negative() {
}
}
class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Object oj1;
try {
Class class1 = Class.forName("diaoYong.People"); //类的完整路径
oj1 = class1.newInstance(); //默认调用People的无参构造器
People p1 = (People)oj1;
System.err.println(p1.getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class BMW {
private String color = "black";
public BMW(String name, double price, String color) {
super(name, price);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private int hahaha() {
return 5;
}
}
import java.lang.reflect.*;
class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
BMW car = new BMW("bmw320",300000.0,"white");
Class class1 = car.getClass();
try {
Field field = class1.getDeclaredField("color");
field.setAccessible(true);
System.out.println(field.get(car));
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class BMW extends Car{
private String color = "black";
public BMW(String name, double price, String color) {
super(name, price);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private int hahaha(int x) {
return x;
}
}
import java.lang.reflect.Method;
class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BMW b = new BMW("京A", 200.00, "yellow");
try {
Class class1 = b.getClass();
Method method1 = class1.getDeclaredMethod("hahaha",int.class);
method1.setAccessible(true);
int a =(int)method1.invoke(b,5);
System.err.println(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}