会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132471个问题
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 15631楼
Python 全系列/第一阶段:Python入门/编程基本概念 15632楼
Python 全系列/第九阶段:Python_Django2 框架(隐藏)/Django项目阶段-博客项目 15637楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS选择器 15638楼

User 类:

public class User {

    private String userName;
    private int userAge;
    public String location;

    public User() {
    }

    public User(String userName, int userAge, String location) {
        this.userName = userName;
        this.userAge = userAge;
        this.location = location;
    }

    // private modifier
    private User(int userAge) {
        this.userAge = userAge;
    }

     /*only the name is different, can not constitute an overload,
     so it can not add the constructor that only have a location parameter.*/
    public  User(String userName) {
        this.userName = userName;
    }

    public User(String userName, String location) {
        this.userName = userName;
        this.location = location;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void testMethod () {
        System.out.println("hello!");
    }

    private void testWay () {
        System.out.println("overload");
    }

    public void testWay (int id) {
        System.out.println(id);
    }

    @Override
    public String toString() {
        /* Considering code writing from the bottom level, in order to cope with different functional requirements */
        return "User{" +
                "userName='" + this.getUserName()+ '\'' +
                ", userAge=" + this.getUserAge()+
                ", location='" + this.getLocation() + '\'' +
                '}';
    }
}

通过反射调用方法测试类:

public class GetMethodTest02 {

    public static void main(String[] args) {
        User user01 = new User();
        Class classObject01 = user01.getClass();

        Class classObject02 = User.class;

        Class classObject03 = null;
        try {
            classObject03 = Class.forName("com.bjsxt.demo.User");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        /* call the public method */
        Method method01 = null;
        try {
            method01 = classObject03.getDeclaredMethod("testWay",int.class);   // get designed method object.
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Object user02 = null;
        try {
            user02 = classObject03.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            method01.invoke(user02,3);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        // how call the private method?

    }
}

老师,在调用方法的测试类中,如何调用自定义的用 private 修饰的 testWay() 方法?还是说通过反射只能调用类的 public 方法,不能调用 pirvate 的方法.

JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 15639楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 15640楼
Python 全系列/第五阶段:数据库编程/mysql的使用 15642楼

微信截图_20231104005121.png

Python 全系列/第一阶段:Python入门/Python入门(动画版) 15643楼

game.zip

老师,请您帮我看一下这个代码,我的飞机它不给面子,不动,谢谢

JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 15645楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637