新手问个关于调用方法的问题

「已注销」 2020-08-19 10:59:49


这里的account为啥就能调用drawing方法
...全文
649 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2020-08-19
  • 打赏
  • 举报
回复
引用 3 楼 大隐藏于寺 的回复:
account是一个引用类型的变量,它保存的是Account类的某个对象的引用,能调用Account类型的public的方法有啥问题么?

你说的这个我知道,但是我就是找不到这种用法的具体介绍。
大隐藏于寺 2020-08-19
  • 打赏
  • 举报
回复
account是一个引用类型的变量,它保存的是Account类的某个对象的引用,能调用Account类型的public的方法有啥问题么?
「已注销」 2020-08-19
  • 打赏
  • 举报
回复
Account是个类,他的drawing方法为什么可以在另个类User中通过类名.方法调用?
明白畅达 2020-08-19
  • 打赏
  • 举报
回复
drawing在account方法内可以通过类名调用
qq_39936465 2020-08-19
  • 打赏
  • 举报
回复
引用 8 楼 口袋玩家 的回复:
[quote=引用 6 楼 qq_39936465 的回复:][quote=引用 4 楼 口袋玩家 的回复:][quote=引用 3 楼 大隐藏于寺 的回复:]account是一个引用类型的变量,它保存的是Account类的某个对象的引用,能调用Account类型的public的方法有啥问题么?
你说的这个我知道,但是我就是找不到这种用法的具体介绍。[/quote] User类里不是引入了一个Acount的变量acount么,该变量被认为是Acount的实例当然能调用自己的方法咯,但是实际用时注意acount需要被传值,不然会报错。[/quote] 为什么这个变量acount就可以被认为是Acount的实例?实例对象不是要new出来的吗?[/quote] 举个例子来说 A a=new A(); A b=a; 所以不一定要new,b被赋值a后也是A的实例。
maradona1984 2020-08-19
  • 打赏
  • 举报
回复
引用 8 楼 口袋玩家 的回复:
[quote=引用 6 楼 qq_39936465 的回复:][quote=引用 4 楼 口袋玩家 的回复:][quote=引用 3 楼 大隐藏于寺 的回复:]account是一个引用类型的变量,它保存的是Account类的某个对象的引用,能调用Account类型的public的方法有啥问题么?
你说的这个我知道,但是我就是找不到这种用法的具体介绍。[/quote] User类里不是引入了一个Acount的变量acount么,该变量被认为是Acount的实例当然能调用自己的方法咯,但是实际用时注意acount需要被传值,不然会报错。[/quote] 为什么这个变量acount就可以被认为是Acount的实例?实例对象不是要new出来的吗?[/quote] 我建议你理解概念的时候要学会自解释,理论解释现象,现象反推理论,相辅相成. 你可以类比下娃娃机,娃娃机投币口算是个方法,他的参数就是硬币,这个硬币在哪个地方new出来的,娃娃机是不管的,他只是接受一个硬币的参数而已,你要搞明白这一点.这仅仅是个简单的语法而已,方法形参跟变量还是有点差异的.
「已注销」 2020-08-19
  • 打赏
  • 举报
回复
引用 6 楼 qq_39936465 的回复:
[quote=引用 4 楼 口袋玩家 的回复:][quote=引用 3 楼 大隐藏于寺 的回复:]account是一个引用类型的变量,它保存的是Account类的某个对象的引用,能调用Account类型的public的方法有啥问题么?

你说的这个我知道,但是我就是找不到这种用法的具体介绍。[/quote]

User类里不是引入了一个Acount的变量acount么,该变量被认为是Acount的实例当然能调用自己的方法咯,但是实际用时注意acount需要被传值,不然会报错。[/quote]
为什么这个变量acount就可以被认为是Acount的实例?实例对象不是要new出来的吗?
「已注销」 2020-08-19
  • 打赏
  • 举报
回复
引用 5 楼 大隐藏于寺 的回复:
对象引用调用实例方法,如果要找具体介绍,最权威的肯定就是官方给的.这是官方的Java SE 8版本的java语言规范

下面是4.3.1章节对Object的介绍,其中第二条就是方法调用,具体在15.12章节


The operators on references to objects are:

Field access, using either a qualified name (§6.6) or a field access expression (§15.11)

Method invocation (§15.12)

The cast operator (§5.5, §15.16)

The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference), and then will produce a newly created String that is the concatenation of the two strings

The instanceof operator (§15.20.2)

The reference equality operators == and != (§15.21.3)

The conditional operator ? : (§15.25).




下面是15.12章节部分内容,这里是链接

A method invocation expression is used to invoke a class or instance method.

MethodInvocation:
MethodName ( [ArgumentList] )
TypeName . [TypeArguments] Identifier ( [ArgumentList] )
ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
Primary . [TypeArguments] Identifier ( [ArgumentList] )
super . [TypeArguments] Identifier ( [ArgumentList] )
TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )
ArgumentList:
Expression {, Expression}

英文的看不懂,谷歌翻译后还是不懂。不过还是很感谢。
qq_39936465 2020-08-19
  • 打赏
  • 举报
回复
引用 4 楼 口袋玩家 的回复:
[quote=引用 3 楼 大隐藏于寺 的回复:]account是一个引用类型的变量,它保存的是Account类的某个对象的引用,能调用Account类型的public的方法有啥问题么?
你说的这个我知道,但是我就是找不到这种用法的具体介绍。[/quote] User类里不是引入了一个Acount的变量acount么,该变量被认为是Acount的实例当然能调用自己的方法咯,但是实际用时注意acount需要被传值,不然会报错。
大隐藏于寺 2020-08-19
  • 打赏
  • 举报
回复
对象引用调用实例方法,如果要找具体介绍,最权威的肯定就是官方给的.这是官方的Java SE 8版本的java语言规范 下面是4.3.1章节对Object的介绍,其中第二条就是方法调用,具体在15.12章节 The operators on references to objects are: Field access, using either a qualified name (§6.6) or a field access expression (§15.11) Method invocation (§15.12) The cast operator (§5.5, §15.16) The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference), and then will produce a newly created String that is the concatenation of the two strings The instanceof operator (§15.20.2) The reference equality operators == and != (§15.21.3) The conditional operator ? : (§15.25). 下面是15.12章节部分内容,这里是链接 A method invocation expression is used to invoke a class or instance method. MethodInvocation: MethodName ( [ArgumentList] ) TypeName . [TypeArguments] Identifier ( [ArgumentList] ) ExpressionName . [TypeArguments] Identifier ( [ArgumentList] ) Primary . [TypeArguments] Identifier ( [ArgumentList] ) super . [TypeArguments] Identifier ( [ArgumentList] ) TypeName . super . [TypeArguments] Identifier ( [ArgumentList] ) ArgumentList: Expression {, Expression}

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧