我只是在了解继承,并且陷入了一个简单的问题。 我想重写一个类中的print()语句,并在它的子类中使用新的语句。 我不确定我会如何处理它,因为它是一个没有参数的void语句。
public class One { private String name; private int age; public human(String n, int a) name = n; age = a; } public void print() { System.out.println("Name: " + name); System.out.println("Age: " + age); }第二课:
public class Two extends One { private double gpa; public two(String n, int a, double g) { super(n,a); gpa = g; public double getGPA (){ return gpa; } public void print() { // override code here to include code from class one + displaying GPA.. } }例如,example.print()会打印
Name: Jack Age: 34我想要的输出是
Name: Jack Age: 34 GPA: 3.20我想我必须使用超级方法,但无法找到正确合并它的方法。 任何提示将不胜感激!
I'm just learning about inheritence and am stuck on a simple problem. I want to override the print() statement in the one class with a new one in it's subclass two. I'm not sure how I would approach it as it is a void statement with no parameters.
public class One { private String name; private int age; public human(String n, int a) name = n; age = a; } public void print() { System.out.println("Name: " + name); System.out.println("Age: " + age); }Second class:
public class Two extends One { private double gpa; public two(String n, int a, double g) { super(n,a); gpa = g; public double getGPA (){ return gpa; } public void print() { // override code here to include code from class one + displaying GPA.. } }So for example, example.print() would print
Name: Jack Age: 34The desired output I want is
Name: Jack Age: 34 GPA: 3.20I am thinking I have to use the super method but cannot find a way to incorporate it correctly. Any tips would be appreciated!
最满意答案
关键字super允许您访问超类的(可见)成员。 所以,你可以调用超类的print()方法,然后执行子类的具体工作:
public void print() { super.print(); System.out.println ("GPA: " + gpa); }有关更多关于super用法的信息,请参阅Java教程的这一简要部分: 使用Keyword super 。 它用一个简单的例子解决了你所问的同一个问题。
请注意,如果你写了
public void print() { print(); System.out.println ("GPA: " + gpa); }如果没有指定要调用super.print() ,则两者的print()方法将无限期地调用自身,直到出现OutOfMemoryError 。
值得注意的是,Vash在他的回答中提到了@Override注释。
The keyword super gives you access to (visible) members of the superclass. So, you could call the print() method of the superclass, and then just do the specific work of the subclass:
public void print() { super.print(); System.out.println ("GPA: " + gpa); }See this brief section of the Java Tutorials for more on the usage of super: Using the Keyword super. It does address with a simple example the very same problem you were asking about.
Please note that if you wrote
public void print() { print(); System.out.println ("GPA: " + gpa); }Without specifying that you want to call super.print(), the print() method of two would be calling itself indefinitely until you got an OutOfMemoryError.
It is also worth noting what Vash mentioned about the @Override annotation in his answer.
Java继承和重写方法(Java inheritance and overriding a method)我只是在了解继承,并且陷入了一个简单的问题。 我想重写一个类中的print()语句,并在它的子类中使用新的语句。 我不确定我会如何处理它,因为它是一个没有参数的void语句。
public class One { private String name; private int age; public human(String n, int a) name = n; age = a; } public void print() { System.out.println("Name: " + name); System.out.println("Age: " + age); }第二课:
public class Two extends One { private double gpa; public two(String n, int a, double g) { super(n,a); gpa = g; public double getGPA (){ return gpa; } public void print() { // override code here to include code from class one + displaying GPA.. } }例如,example.print()会打印
Name: Jack Age: 34我想要的输出是
Name: Jack Age: 34 GPA: 3.20我想我必须使用超级方法,但无法找到正确合并它的方法。 任何提示将不胜感激!
I'm just learning about inheritence and am stuck on a simple problem. I want to override the print() statement in the one class with a new one in it's subclass two. I'm not sure how I would approach it as it is a void statement with no parameters.
public class One { private String name; private int age; public human(String n, int a) name = n; age = a; } public void print() { System.out.println("Name: " + name); System.out.println("Age: " + age); }Second class:
public class Two extends One { private double gpa; public two(String n, int a, double g) { super(n,a); gpa = g; public double getGPA (){ return gpa; } public void print() { // override code here to include code from class one + displaying GPA.. } }So for example, example.print() would print
Name: Jack Age: 34The desired output I want is
Name: Jack Age: 34 GPA: 3.20I am thinking I have to use the super method but cannot find a way to incorporate it correctly. Any tips would be appreciated!
最满意答案
关键字super允许您访问超类的(可见)成员。 所以,你可以调用超类的print()方法,然后执行子类的具体工作:
public void print() { super.print(); System.out.println ("GPA: " + gpa); }有关更多关于super用法的信息,请参阅Java教程的这一简要部分: 使用Keyword super 。 它用一个简单的例子解决了你所问的同一个问题。
请注意,如果你写了
public void print() { print(); System.out.println ("GPA: " + gpa); }如果没有指定要调用super.print() ,则两者的print()方法将无限期地调用自身,直到出现OutOfMemoryError 。
值得注意的是,Vash在他的回答中提到了@Override注释。
The keyword super gives you access to (visible) members of the superclass. So, you could call the print() method of the superclass, and then just do the specific work of the subclass:
public void print() { super.print(); System.out.println ("GPA: " + gpa); }See this brief section of the Java Tutorials for more on the usage of super: Using the Keyword super. It does address with a simple example the very same problem you were asking about.
Please note that if you wrote
public void print() { print(); System.out.println ("GPA: " + gpa); }Without specifying that you want to call super.print(), the print() method of two would be calling itself indefinitely until you got an OutOfMemoryError.
It is also worth noting what Vash mentioned about the @Override annotation in his answer.
发布评论