您的位置:首页 > 编程语言 > Java开发

JavaSE8基础 Class invoke 使用有参数有返回值的非静态函数

2017-10-08 16:32 363 查看
礼悟:
公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。

os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)


被解析类的代码:

package blog.jizuiku4;

/**
*
*
* @author jizuiku
* @version V17.10.02
*/
public class Person {
// 成员变量
public    int age;
private   String password;
protected String name;
String content;

public    static   int ageStatic=13;
private   static String passwordStatic="hello world";
protected static String nameStatic;
static int scoreStatic;

// 构造方法

public    Person(int age) {System.out.println("public GZ:int");}
public    Person(int age,int score) {System.out.println("public GZ:int,int");}
private   Person() {System.out.println("private GZ:");}
protected Person(String content) {this.content=content;System.out.println("protected GZ:String");}
Person(int age,String name){this.age=age;this.name=name;System.out.println("default GZ:int,String");}

// 成员方法

public void sayHello(String name) {}
private int sayHello() {return 1;}
protected String sayHello(int age) {return "";}
Integer	sayHello(int age,String name) {System.out.println("sayHello(int,String)"); return 1;}

public static void sayHelloStatic(String name) {}
private static int sayHelloStatic() {return 1;}
protected static String sayHelloStatic(int age) {return "";}
static void sayHelloStatic(int age,String name) {}

@Override
public String toString() {
return "Person [age=" + age + ", password=" + password + ", name=" + name + ", content=" + content + "]";
}

}


演示类:

package blog.jizuiku4;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
* Class invoke 有参数 有返回值 普通方法(非静态)
*
* @author jizuiku
* @version V17.10.03
*/
public class ClassUseNormalFunDemo {
public static void main(String[] args) throws Exception {
String className = "blog.jizuiku4.Person";
Class c = Class.forName(className);

System.out.println("Person类中的构造方法有:");
Constructor[] cons = c.getDeclaredConstructors();
for (Constructor constructor : cons) {
System.out.println(constructor);
}

System.out.println();

// 要使用的构造函数
// protected blog.jizuiku3.Person(java.lang.String)
Constructor con = c.getDeclaredConstructor(String.class);
// 暴力访问
con.setAccessible(true);
Object obj = con.newInstance("javaer");
System.out.println(obj.toString());

System.out.println();

System.out.println("Person类中的成员方法有:");
Method[] ms = c.getDeclaredMethods();
for (Method method : ms) {
System.out.println(method);
}

System.out.println();

// 要使用的函数
// void blog.jizuiku3.Person.sayHello(int,java.lang.String)
Method m = c.getDeclaredMethod("sayHello", int.class, String.class);
// 暴力访问
m.setAccessible(true);
// 返回值 result
// 相当于 obj.m(1,"hello")
Object result = m.invoke(obj, 1,"hello");
System.out.println(result);
}
}


结果:



Java优秀,值得学习。
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐