getMethods 與 getDeclaredMethods 的區別

個人理解如下:
public Method[] getMethods()返回某個類的所有公用(public)方法包括其繼承類的公用方法,當然也包括它所實現接口的方法。
public Method[] getDeclaredMethods()返回自身類的所有公用(public)方法包括私有(private)方法,但包括其繼承類的方法,當然也包括它所實現接口的方法。
如下:

package zhuhaifeng.pojo;


public interface IEngine {
        /**
         * 引擎的馬力
         * @return
         */

        public int getPower();

}
Car類實現該接口
package zhuhaifeng.pojo;


public class Car implements IEngine {
        private String driver;


        protected String brand;
        protected String color;
        protected int maxSpeed;

        public Car() {
                System.out.println("Car 的默認構造函數正在運行。。。");
        }

        /**
         * 汽車介紹
         */

        public void introduce() {
                System.out.println(" the brand is :" + brand + "\n the color is : " + color + "\n the maxSpeed is :" + maxSpeed);
        }

        public int getPower() {
                return 10;
        }

        public String getBrand() {
                return brand;
        }

        public void setBrand(String brand) {
                this.brand = brand;
        }

        public String getColor() {
                return color;
        }

        public void setColor(String color) {
                this.color = color;
        }

        public int getMaxSpeed() {
                return maxSpeed;
        }

        public void setMaxSpeed(int maxSpeed) {
                this.maxSpeed = maxSpeed;
        }

      
        private String getDriver() {
                return driver;
        }

        private void setDriver(String driver) {
                this.driver = driver;
        }
}
//其中getDriver,setDriver是私有的.
Truck繼承Car類
package zhuhaifeng.pojo;

import zhuhaifeng.pojo.Car;


public class Truck extends Car {
        private String capacity;    //載重
                                private String driver;
        public void setCapacity(String capacity) {
                this.capacity = capacity;
        }

        public Truck(){
                System.out.println("Truck 的默認構造函數正在運行。。。");
        }

        /**
         *    汽車介紹
         */

        public void introduce(){
                        System.out.println("Truck    :");
                        System.out.println(" the brand is :"+brand+ "\n the color is : "+color+"\n the maxSpeed is :"+maxSpeed);
        }


        public int getPower() {
                return 100;    //To change body of implemented methods use File | Settings | File Templates.
        }
            private String getDriver() {
                return driver;
        }

        private void setDriver(String driver) {
                this.driver = driver;
        }
}
最後還有:
package zhuhaifeng.reflect;

import zhuhaifeng.pojo.Truck;

import java.lang.reflect.Method;

public class MethodsDifferent {
        public static void displayMethods(Class clazz) throws Throwable {

                System.out.println("\ngetMethods方法: ");
                Method[] methods = clazz.getMethods();
                for (int i = 0; i < methods.length; i++) {
                        Method method = methods[i];
                        System.out.println("方法名稱 " + method.getName());
                        //    System.out.println("param type " + method.getParameterTypes().toString());

                }

        }

        public static void displayDeclaredMethods(Class clazz) throws Throwable {
                System.out.println("\ngetDeclaredMethods方法: ");
                Method[] methods = clazz.getDeclaredMethods();
                for (int i = 0; i < methods.length; i++) {
                        Method method = methods[i];
                        System.out.println("方法名稱 " + method.getName());
                        //    System.out.println("param type " + method.getParameterTypes().toString());

                }

        }

        public static void main(String[] args) throws Throwable {
                Class clazz = ReflectDemo.loadClass("zhuhaifeng.pojo.Truck");
                Truck truck = (Truck) ReflectDemo.initDefaultConstructor(clazz);
 
                displayDeclaredMethods(clazz);
                displayMethods(clazz);

        }


}
運行結果如下:

Car 的默認構造函數正在運行。。。
Truck 的默認構造函數正在運行。。。

getDeclaredMethods方法(只顯示當前類的方法,但包括私有方法):
方法名稱 introduce
方法名稱 setCapacity
方法名稱 getPower
方法名稱 getDriver
方法名稱 setDriver

getMethods方法(顯示其繼承類的方法,但不包括私有方法getDriver):
方法名稱 introduce
方法名稱 setCapacity
方法名稱 getPower
方法名稱 setBrand
方法名稱 setMaxSpeed
方法名稱 getBrand
方法名稱 getMaxSpeed
方法名稱 setColor
方法名稱 getColor
方法名稱 hashCode
方法名稱 getClass
方法名稱 wait
方法名稱 wait
方法名稱 wait
方法名稱 equals
方法名稱 toString
方法名稱 notify
方法名稱 notifyAll

Process finished with exit code 0
其他相關類:
package zhuhaifeng.reflect;

import zhuhaifeng.pojo.Car;
import zhuhaifeng.pojo.Truck;

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

/**
* Created by IntelliJ IDEA.
* User: zhuhaifeng
* Date: 2008-10-18
* Time: 20:02:21
* To change this template use File | Settings | File Templates.
*/

public class ReflectDemo {

        /**
         * 通過類裝載器裝載類
         *
         * @param className
         * @return
         * @throws Throwable
         */

        public static Class loadClass(String className) throws Throwable {
                //類裝載器
                ClassLoader loader = Thread.currentThread().getContextClassLoader();
                Class clazz = loader.loadClass(className);
                return clazz;

        }

        /**
         * 通過初始化默認構造函數實例化對象
         *
         * @param clazz
         * @return
         * @throws Throwable
         */

        public static Object initDefaultConstructor(Class clazz) throws Throwable {


                //    獲取類的默認構造器對象並通過它實例化Car
                Constructor cons = clazz.getDeclaredConstructor((Class[]) null);
                return cons.newInstance();


        }

        /**
         * 初始化方法參數
         *
         * @param clazz
         * @param obj
         * @param methodName
         * @param methodParam
         * @throws Throwable
         */

        public static void initMethodParam(Class clazz, Object obj, String methodName, Object methodParam) throws Throwable {

                Method[] methods = clazz.getMethods();
                //遍歷方法
                for (int i = 0; i < methods.length; i++) {
                        Method method = methods[i];
                        //判斷方法名稱是否相同
                        if (method.getName().equals(methodName)) {

                                method.invoke(obj, methodParam);
                        }

                }


        }


        public static void main(String[] args) throws Throwable {
                Class clazz = loadClass("zhuhaifeng.pojo.Truck");
                Truck truck = (Truck) initDefaultConstructor(clazz);
                initMethodParam(clazz, truck, "setBrand", "奧迪A6");
                initMethodParam(clazz, truck, "setMaxSpeed", 30);
                // displayMethod( clazz)         ;

                truck.introduce();
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章