"Super" issue: "error: '(' expected"
"Super" issue: "error: '(' expected"
您好,感谢阅读!
我使用 "for" 读取数组列表中的每个对象,并想检查 returns 每个对象的函数 getType() 的字符串,它存在于扩展 class 个对象。
for(int i=0; i<cat.items.size(); i++)
{
if ( cat.items.get(i).super.getType().equals(type) );
{
但是,我得到了错误
MainApp.java:17: error: '(' expected
if ( cat.items.get(i).super.getType().equals(type) );
^
我认为我没有正确使用 "super"。你能提供替代方案吗?
再次感谢!
super
是保留字,不能在这样的上下文中使用。意思是,它不能是变量名、方法名等。如果你在这里尝试做的是说 "get item x, then call its parent version of getType()," 那么这也是对 super
的无效使用。
Can you offer an alternative?
假设getType
是由class或suberclass实现的,那么使用方法是这样的:
cat.items.get(i).getType().equals(type)
注意,如果 subclass 覆盖了 superclass 中实现的 getType()
方法,上面将调用该方法的 subclass 版本.您不能在 superclass1.
中调用覆盖的方法
1 - 嗯......不是在这种情况下。覆盖方法可以调用 super.getType()
,但只能针对目标对象进行;即 "this"。它不适用于您的示例。
这可能会帮助您了解有关继承和 super() 的更多信息...
源代码:
$ cat Inheritance.java
class ParentClass {
private String privateParentProperty;
public ParentClass() {
this.privateParentProperty = "Hi, this is ParentClass, my instance came from " + String.valueOf(this.getClass()) + "!";
}
public String getPrivateParentProperty() {
return this.privateParentProperty;
}
}
class ChildClass extends ParentClass {
private String childProperty;
public ChildClass() {
super(); // I can explictly call the parent constructor in a child class by calling super in the first line of the child class...
this.childProperty = "Hi, I came from " + String.valueOf(this.getClass()) + "!";
}
public String getChildProperty() {
return this.childProperty;
}
@Override
public String getPrivateParentProperty() {
String parentProperty = super.getPrivateParentProperty(); // I can override a parent method in a child class and call that method from the parent in the first line of the child's class.
parentProperty += " I, " + String.valueOf(this.getClass()) + " am intercepting this call and adding my own message to my parent."; // Note: I am not modifying the private property from the parent, but rather appending to a new String local to this method.
return parentProperty;
}
}
class MainApp {
public static void main(final String[] args) {
ParentClass parentClassInstance = new ParentClass();
System.out.println ( parentClassInstance.getPrivateParentProperty() );
ChildClass childClassInstance = new ChildClass();
System.out.println( childClassInstance.getChildProperty() );
System.out.println( ( (ParentClass)childClassInstance ).getPrivateParentProperty() ); // I can cast a child to its parent class and invoke the method... however, because the child overrode the method and the instance is the child instance, even with the cast, the method from the child will execute.
System.out.println( childClassInstance.getPrivateParentProperty() ); // I can also call this method directly from the childwithout needing to cast, because it inherits it from the parent.
}
}
编译和测试输出:
$ javac Inheritance.java
$ java MainApp
Hi, this is ParentClass, my instance came from class ParentClass!
Hi, I came from class ChildClass!
Hi, this is ParentClass, my instance came from class ChildClass! I, class ChildClass am intercepting this call and adding my own message to my parent.
Hi, this is ParentClass, my instance came from class ChildClass! I, class ChildClass am intercepting this call and adding my own message to my parent.
希望对您有所帮助!
您好,感谢阅读!
我使用 "for" 读取数组列表中的每个对象,并想检查 returns 每个对象的函数 getType() 的字符串,它存在于扩展 class 个对象。
for(int i=0; i<cat.items.size(); i++)
{
if ( cat.items.get(i).super.getType().equals(type) );
{
但是,我得到了错误
MainApp.java:17: error: '(' expected
if ( cat.items.get(i).super.getType().equals(type) );
^
我认为我没有正确使用 "super"。你能提供替代方案吗?
再次感谢!
super
是保留字,不能在这样的上下文中使用。意思是,它不能是变量名、方法名等。如果你在这里尝试做的是说 "get item x, then call its parent version of getType()," 那么这也是对 super
的无效使用。
Can you offer an alternative?
假设getType
是由class或suberclass实现的,那么使用方法是这样的:
cat.items.get(i).getType().equals(type)
注意,如果 subclass 覆盖了 superclass 中实现的 getType()
方法,上面将调用该方法的 subclass 版本.您不能在 superclass1.
1 - 嗯......不是在这种情况下。覆盖方法可以调用 super.getType()
,但只能针对目标对象进行;即 "this"。它不适用于您的示例。
这可能会帮助您了解有关继承和 super() 的更多信息...
源代码:
$ cat Inheritance.java
class ParentClass {
private String privateParentProperty;
public ParentClass() {
this.privateParentProperty = "Hi, this is ParentClass, my instance came from " + String.valueOf(this.getClass()) + "!";
}
public String getPrivateParentProperty() {
return this.privateParentProperty;
}
}
class ChildClass extends ParentClass {
private String childProperty;
public ChildClass() {
super(); // I can explictly call the parent constructor in a child class by calling super in the first line of the child class...
this.childProperty = "Hi, I came from " + String.valueOf(this.getClass()) + "!";
}
public String getChildProperty() {
return this.childProperty;
}
@Override
public String getPrivateParentProperty() {
String parentProperty = super.getPrivateParentProperty(); // I can override a parent method in a child class and call that method from the parent in the first line of the child's class.
parentProperty += " I, " + String.valueOf(this.getClass()) + " am intercepting this call and adding my own message to my parent."; // Note: I am not modifying the private property from the parent, but rather appending to a new String local to this method.
return parentProperty;
}
}
class MainApp {
public static void main(final String[] args) {
ParentClass parentClassInstance = new ParentClass();
System.out.println ( parentClassInstance.getPrivateParentProperty() );
ChildClass childClassInstance = new ChildClass();
System.out.println( childClassInstance.getChildProperty() );
System.out.println( ( (ParentClass)childClassInstance ).getPrivateParentProperty() ); // I can cast a child to its parent class and invoke the method... however, because the child overrode the method and the instance is the child instance, even with the cast, the method from the child will execute.
System.out.println( childClassInstance.getPrivateParentProperty() ); // I can also call this method directly from the childwithout needing to cast, because it inherits it from the parent.
}
}
编译和测试输出:
$ javac Inheritance.java $ java MainApp Hi, this is ParentClass, my instance came from class ParentClass! Hi, I came from class ChildClass! Hi, this is ParentClass, my instance came from class ChildClass! I, class ChildClass am intercepting this call and adding my own message to my parent. Hi, this is ParentClass, my instance came from class ChildClass! I, class ChildClass am intercepting this call and adding my own message to my parent.
希望对您有所帮助!