为啥可以不用obj.method()方式调用,直接用method()方式调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * 5、修改指定位置处成绩 * @param f 存储数学成绩的数组 */ public void update( float [] f, Scanner sc) { if (!checkScores(f)) { return ; } System.out.println( "修改前:" ); displayAllScore(f); System.out.println( "请输入要修改数据的位置(从0开始):" ); //修改位置 int index = getInt(sc, f.length, true ); System.out.println( "请输入新数据:" ); //新成绩 float newScore = getFloat(sc); f[index] = newScore; System.out.println( "修改后:" ); displayAllScore(f); } /** * 6、打印输出所有成绩 * @param f 存储数学成绩的数组 */ public void displayAllScore( float [] f) { if (!checkScores(f)) { return ; } System.out.println( "成绩为:" ); for ( float score: f) { System.out.print(score + " " ); } System.out.println(); } |
在update()方法内部我调用了一次displayAllScore()方法,此时我并没有使用 对象.方法() 的方式调用.而是使用 方法() 调用.displayAllScore()方法也不是静态方法.为什么我可以直接调用
20
收起
正在回答
2回答
你好,这里是因为main方法是静态方法,在静态方法中调用普通方法,需要实例对象后,再调用
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9404 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧