[英]How can I pass parameters to second level super class
I am trying to understand inheritence and have some problems to understand. 我试图了解继承性并且有一些问题需要理解。
public class SiteTemplate extends SiteTemplateMethods
public SiteTemplate(String country, String language, HttpServletRequest request){
super();
}
public class SiteTemplateMethods extends Resources
public class Resources extends PropertyFiles
1) class PropertyFiles
all methods will be accessible from classes Resources
, SiteTemplateMethods
and SiteTemplate
. 1)类
PropertyFiles
所有方法都可以从类Resources
, SiteTemplateMethods
和SiteTemplate
。 Is that right? 那正确吗?
2) super();
2)
super();
in SiteTemplate
constructor will pass all parameters to SiteTemplateMethods
but these parameters are not required here in class SiteTemplateMethods
how can I get the parameters of the SiteTemplate
class to second level super class Resource
class? 在
SiteTemplate
构造函数中将所有参数传递给SiteTemplateMethods
但这些参数在类SiteTemplateMethods
不需要这样我如何将SiteTemplate
类的参数获取到二级超类Resource
类?
Best regards 最好的祝福
1) class
PropertyFiles
all methods will be accessible from classesResources
,SiteTemplateMethods
andSiteTemplate
.1)类
PropertyFiles
所有方法都可以从类Resources
,SiteTemplateMethods
和SiteTemplate
。 Is that right?那正确吗?
No, it is not right. 不,这不对。 All
public
and protected
members of PropertyFiles
will be accessible to descendant classes, including Resources
, SiteTemplateMethods
, and SiteTemplate
, but the private
methods of PropertyFiles
will not be accessible to subclasses. PropertyFiles
所有public
成员和protected
成员都可以访问后代类,包括Resources
, SiteTemplateMethods
和SiteTemplate
,但子类不能访问PropertyFiles
的private
方法。 Package-private methods will be accessible to descendant classes only if they are in the same package as Resources
. 仅当后代类与
Resources
包在同一个包中时,才能访问包私有方法。 See https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html . 请参阅https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html 。
There are additional limitations on accessing methods that have been overridden (as first noted by @Gergely). 访问已被覆盖的方法还有其他限制(正如@Gergely首先指出的那样)。
2)
super();
2)
super();
in SiteTemplate constructor will pass all parameters toSiteTemplateMethods
在SiteTemplate构造函数中将所有参数传递给
SiteTemplateMethods
No, it won't. 不,它不会。
super()
with no arguments invokes the no-argument constructor of the immediate superclass, passing no arguments to it. 没有参数的
super()
调用直接超类的无参数构造函数,不向它传递任何参数。 If the immediate superclass does not have an accessible no-arg constructor then the invocation is invalid. 如果直接超类没有可访问的no-arg构造函数,则调用无效。
To invoke a different constructor, ie as one that accepts arguments, you must invoke super()
with a corresponding argument list. 要调用不同的构造函数,即接受参数的构造函数,必须使用相应的参数列表调用
super()
。
but these parameters are not required here in class
SiteTemplateMethods
how can I get the parameters of theSiteTemplate
class to second level super classResource
class?但是在
SiteTemplateMethods
类中不需要这些参数如何将SiteTemplate
类的参数获取到二级超类Resource
类?
If SiteTemplateMethods
has an accessible constructor that will pass the arguments on to an accessible (to it) constructor of Resource
, then you can use a corresponding super()
invocation (with arguments) at the beginning of SiteTemplate
's constructor. 如果
SiteTemplateMethods
有一个可访问的构造函数,它将参数传递给Resource
的可访问(to)构造函数,那么您可以在SiteTemplate
的构造函数的开头使用相应的super()
调用(带参数)。 Otherwise, SiteTemplate
's constructor will need to apply the parameters directly, in a manner appropriate to class Resource
, such as by invoking property setter methods. 否则,
SiteTemplate
的构造函数将需要以适合类Resource
的方式直接应用参数,例如通过调用属性setter方法。
Let's declare some methods. 让我们宣布一些方法。
public class Resources extends PropertyFiles{
public Resources(String country,String language){
super();
//...
}
public void a(){
//...
}
public void b(){
//...
}
public void c(){
//...
}
}
public class SiteTemplateMethods extends Resources{
public SiteTemplateMethods(){
super("foo","bar");
//...
}
public void a(){
//...
}
public void b(){
super.b();//This will call the superclass's method
//...
}
}
1) In SiteTemplate
, you can access c()
from Resources
but not a()
and b()
, because they were overridden by SiteTemplateMethods
. 1)在
SiteTemplate
,您可以从Resources
访问c()
,但不能访问a()
和b()
,因为它们被SiteTemplateMethods
覆盖。 Calling a()
will call the a()
method in SiteTemplateMethods
instead. 调用
a()
将调用SiteTemplateMethods
的a()
方法。 If you have an instance of SiteTemplate
then you can't call the a()
method of Resources
. 如果您有
SiteTemplate
的实例,则无法调用Resources
的a()
方法。 The b()
method in SiteTemplateMethods
however, calls the superclass method, and executes additional code after it. 但是,
SiteTemplateMethods
的b()
方法调用超类方法,并在其后执行其他代码。
2) In constructor calls you can only access constructors declared by the class that your class directly extends. 2)在构造函数调用中,您只能访问类直接扩展的类声明的构造函数。 In the given example You can't call
super(String country,String language)
in SiteTemplate
, unless SiteTemplateMethods
declares the following: 在给定示例中,除非
SiteTemplateMethods
声明以下内容,否则不能在SiteTemplate
调用super(String country,String language)
:
public SiteTemplateMethods(String country,String language){
super(country,language);
//...
}
Also note that methods with different parameters are treated as two different methods. 另请注意,具有不同参数的方法被视为两种不同的方法。 so
a(String str)
does not override a()
. 所以
a(String str)
不会覆盖a()
。 However, this is not true, if the overriding method is fully applicable for the arguments of the previous method. 但是,如果覆盖方法完全适用于前一个方法的参数,则情况并非如此。 So
a(Object o)
does override a(String str)
. 所以
a(Object o)
会覆盖a(String str)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.