A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 大漠孤烟 中级黑马   /  2014-4-29 20:50  /  1281 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima.reflect.demo;

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

public class ReflectDemo4 {

        /**
         *@throws Exception
         * @hutian
         */
        //反射获取Class中的公共函数 ,getMethod
        public static void main(String[] args) throws Exception {
       
                 //getMethodDemo3();
                 //getMethodDemo2();
                getMethodDemo1();
        }
    public static void getMethodDemo3() throws Exception{
            // 类名,字符串获取反射信息
            Class clazz=Class.forName("com.itheima.bean.Person");
              
            Method m=clazz.getMethod("paramMethod", String.class,int.class);  //获取方法
           
            Object obj=clazz.newInstance(); //对象初始化
           
            m.invoke("小强", 18);
    }
   
    public static void getMethodDemo2() throws Exception{
            // 类名,字符串获取反射信息
            Class clazz=Class.forName("com.itheima.bean.Person");
              
            Method m=clazz.getMethod("show",null);  //获取空参数一般方法
           
    //Object obj=clazz.newInstance(); //对象初始化
           
            Constructor ct=clazz.getConstructor(String.class,int.class);
           
            Object obj=ct.newInstance("小明",30);
           
            m.invoke(obj, null);
    }
   
    //获取指定Class类中的所有公共函数。。
    public static void getMethodDemo1() throws Exception{
            // 类名,字符串获取反射信息
            Class clazz=Class.forName("com.itheima.bean.Person");
              
            Method[] methods=clazz.getMethods();  //getMethods()获取的是共有的方法
                     methods=clazz.getDeclaredMethods(); //获取本类中所有方法,包括私有。
        
            for(Method m:methods){
                    System.out.println(m);
            }
           
    }
}
我在上班看毕老师笔记偷偷敲的,反射这块感觉很疑惑,求教一下,怎么学习理解它
执行getMethodDemo1() 没问题
结果
public static void com.itheima.bean.Person.Show()
public void com.itheima.bean.Person.setAge(int)
public int com.itheima.bean.Person.getAge()
public java.lang.String com.itheima.bean.Person.getName()
public void com.itheima.bean.Person.setName(java.lang.String)
public static void com.itheima.bean.Person.main(java.lang.String[])

但是执行 getMethodDemo2()、getMethodDemo3()
都报错:
Exception in thread "main" java.lang.NoSuchMethodException: com.itheima.bean.Person.show()
        at java.lang.Class.getMethod(Class.java:1624)
        at com.itheima.reflect.demo.ReflectDemo4.getMethodDemo2(ReflectDemo4.java:34)
        at com.itheima.reflect.demo.ReflectDemo4.main(ReflectDemo4.java:16)

3 个回复

正序浏览
package com.itheima.bean;

public class Person {

        /**
         * @param args
         */
   private String name;//姓名   
   private int age;    //年龄
        public static void main(String[] args) {
                // TODO Auto-generated method stub
        Person p=new Person();
        p.setName("小米");
        p.setAge(22);
        System.out.print("这个人的姓名"+p.getName()+"年龄"+p.getAge());
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
       
        public Person(){
                System.out.print("人出生就会哭");
        }
        public Person(String name,int age){
                 System.out.print("这个人的姓名"+this.name+"年龄"+this.age);
        }
        public static void show(){
                System.out.print("展示才艺");
        }

}
多谢,第二天仔细检查发现Show()方法大写啦,但是打印:这个人的姓名null年龄0展示才艺,不是应该      Object obj=ct.newInstance("小明",30);不是初始化吗,打印还是空?
回复 使用道具 举报
那么明显你的类当中没有show方法
回复 使用道具 举报
你的Person类中找不到show( )方法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马