[英]Accessing a method defined in the Superclass [Java]
If I define 2 classes; 如果我定义2个类; one called Super, which has a method called potato(), and the second one, Sub, which extends Super.
一个叫做Super的方法,它具有一个称为potato()的方法,第二个叫做Sub的方法,它扩展了Super。
I then declare an object of type Sub; 然后,我声明一个Sub类型的对象;
ie Sub carrot; 子胡萝卜
within a third class/main within the same package. 在同一包内的三等舱/主舱内。
Can I access the potato() method in this third class/main via carrot.potato() ? 我可以通过胡萝卜.potato()访问此第三类/主体中的potato()方法吗?
Sub extends Super. 子扩展超级。 That means that sub is a super with some extra details.
这意味着sub是具有一些额外细节的超级。 That means that you can treat sub exactly as a Super object.
这意味着您可以将sub完全视为Super对象。 So carrot.potato() is correct.
因此,胡萝卜.potato()是正确的。
Since every Child Is-A Parent , you can access that method through Child as well. 由于每个子代都是父代,因此您也可以通过子代访问该方法。 Assuming it is a public method(as you mentioned in the comments).
假设它是一个公共方法(如您在注释中所述)。
If you want to see what I mean in the first comment, have a look here.
如果您想在第一条评论中明白我的意思,
have a look here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.