大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
给你一个小的实例代码:
创新互联建站是一家专业提供巴东企业网站建设,专注与成都网站建设、网站制作、成都h5网站建设、小程序制作等业务。10年已为巴东众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class test {
public static void main(String args[]) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
Foo foo = new Foo("这个一个Foo对象!");
Class clazz = foo.getClass();
Method m1 = clazz.getDeclaredMethod("outInfo");
Method m2 = clazz.getDeclaredMethod("setMsg", String.class);
Method m3 = clazz.getDeclaredMethod("getMsg");
m1.invoke(foo);
m2.invoke(foo, "重新设置msg信息!");
String msg = (String) m3.invoke(foo);
System.out.println(msg);
}
}
class Foo {
private String msg;
public Foo(String msg) {
this.msg = msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void outInfo() {
System.out.println("这是测试Java反射的测试类");
}
}
把 人 封装成一个类Person,继承这个类 变有了 人应该有的属性
把 家庭成员关系封装一个类Family,继承这个类有了家庭关系的属性
把工作封装成一个接口Jop,继承这个接口实现了工作的方法
由于Person 和 Family 属性都是固定的,即每个人的情况基本都一样所以封装成类
Jop 则是因为 每个人的工作不一样,所有做成接口,究竟是什么样的工作让这个人来实现
这里Person 和 Family需要的属性并不多,所以我统一将他们封装了一个类 Person 实现代码如下:
public class XiaoHong extends Person implements Jop {
public XiaoHong(String name, String sex, String Father, String Mother,
String Son) {
super(name, sex, Father, Mother, Son);
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
XiaoHong xiaohong = new XiaoHong("小红","女","李雷", "韩梅梅", "讨厌啦!人家还小!");
System.out.println(xiaohong);
xiaohong.work();
}
@Override
public void work() {
System.out.println("我是一个高中生");
}
}
/********************************************************************************/
public class Person {
private String name = "I Don't Know";
private String sex = "I Don't Know";
private String Father = "I Don't Know";
private String Mother = "I Don't Know";
private String Son = "I Don't Know";
public Person(String name, String sex, String myFather, String myMother,
String mySon) {
super();
this.name = name;
this.sex = sex;
this.Father = myFather;
this.Mother = myMother;
this.Son = mySon;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getMyFather() {
return Father;
}
public String getMyMother() {
return Mother;
}
public String getMySon() {
return Son;
}
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + ", Father="
+ Father + ", Mother=" + Mother + ", Son=" + Son
+ "]";
}
}
/******************************************************************************/
public interface Jop {
public void work();
}
第一题
class MyValue{
private int value;
public void setValue(int x) {
value = x;
}
public int getValue(){
return value;
}
}
public class UseValue{
public static void main (String [] args){
//创建一个MyValue类的对象myValue
MyValue myValue = new MyValue();
//为myValue对象中的value赋值10
myValue.setValue(10);
//使用getVaule()方法获得myValue对象中的数据并将它打印在控制台上
System.out.println(myValue.getValue());
}
}
第二题
public class Computer {
//以整数为例
private int counterValue;
public int getCounterValue() {
return counterValue;
}
public void setCounterValue(int counterValue) {
this.counterValue = counterValue;
}
public int increment(){
counterValue = counterValue + 1;
return counterValue;
}
public int decrement(){
counterValue = counterValue - 1;
return counterValue;
}
public void reset(){
counterValue = 0;
}
public static void main(String[] args){
Computer c = new Computer();
c.setCounterValue(9);
System.out.println(c.increment());
System.out.println(c.decrement());
c.reset();
System.out.println(c.getCounterValue());
}
}