问题
Hello and thanks for reading!
I use a "for" to read every object in an arraylist, and want to check the string that returns the function getType() for each object, which exists in the extended class of the objects.
for(int i=0; i<cat.items.size(); i++)
{
if ( cat.items.get(i).super.getType().equals(type) );
{
However, I get the error
MainApp.java:17: error: '(' expected
if ( cat.items.get(i).super.getType().equals(type) );
^
I think that I don't user "super" correctly. Can you offer an alternative?
Thanks again!
回答1:
Can you offer an alternative?
Assuming that getType is implemented by the class or a suberclass, then this is the way to use it:
cat.items.get(i).getType().equals(type)
Note that if the subclass overrides a getType() method that is implemented in the superclass, the above will call the subclass version of the method. You can't call the overridden method in the superclass1.
1 - Well ... not in this case. An override method can call super.getType(), but that can only be done for the target object; i.e. "this". It doesn't apply to your example.
回答2:
super is a reserved word and cannot be used in a context like that. Meaning, it cannot be a variable name, method name, etc. If what you are trying to do here is say "get item x, then call its parent version of getType()," then that's an invalid use of super as well.
回答3:
This might help you learn more about inheritance and super()...
Source code:
$ 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.
}
}
Compiling and test output:
$ 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.
Hope this helps!
来源:https://stackoverflow.com/questions/29997018/super-issue-error-expected