使用 super 覆盖 java 中的方法
Overriding methods in java using super
因此,我必须使用数据类型 int、float、double 和 short 创建 68 种不同的求和方法。 class 称为 OverloadingSums。这是其中一种方法的示例。
public double sum(double x, float y, int z)
{
return x+y+z;
}
现在我被要求创建一个名为 ZeroSum 的 class,本质上是复制 OverloadingSum 并将所有内容归零。这是一个例子。
public double sum(double x, float y, int z)
{
return 0;
}
然后我被要求创建另一个名为 RealSum 的 class,它将扩展 ZeroSum class。我对这项作业的措辞有点困惑,不确定 Whosebug 社区是否可以提供帮助,但我非常困惑。
作业要求如下:
Now that we have thoroughly explored overloading we are going to
explore overriding. Create a class called ZeroSum.java. Take all of
the methods from OverloadingSum.java and change them to return all
zeros in ZeroSum.java. Next, create a class called RealSum.java. This
class, RealSum, will extend the ZeroSum class. Having all zeros for
our sums isn't very useful. We will need to override the parent, or
super class methods to produce real results. Create the necessary
methods to override all of those in the ZeroSum.java. When you are
done run your classes against DrivingSum.java.
这是我的主要方法:
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
RealSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) );
System.out.println("Calling ZeroSum " + real.sum(x,y) );
}
}
因此,根据我的理解,我编写了以下代码:
public class ZeroSum extends RealSum{
public double sum(double x, float y, int z)
{
return super.sum(x,y,z);
}
这将从 RealSum 中获取方法,而不是使用 ZeroSum class 中的求和方法。所以当我 运行 主要方法时, zero.sum(x,y) 给我 30.
之所以感到困惑,是因为作业要求我将 ZeroSum 中的所有内容设置为返回零。如果我将 ZeroSum 扩展为 RealSum,它实际上并没有什么不同。我这样做正确吗?还是我只是想得太多了?
我相信他是想说明你在父 class 中写的任何内容都会转移(这称为继承)给子。您可以通过使用与父 class 方法相同的名称和参数在子 class 中编写一个方法来覆盖它。
我也相信你看错了作业,它说:
This class, RealSum, will extend the ZeroSum class.
我将其解释为 RealSum 是 ZeroSum 的子代,而不是相反:
public class RealSum extends ZeroSum{
//code code code
}
这意味着 ZeroSum 中的所有内容都设置为 0,RealSum 设置新值,而不是使用 super。我不是打赌这是正确的,但在休息和呼吸新鲜空气后尝试再次阅读作业:)
希望对您有所帮助!
此作业的目标是探索最重要的概念,其中执行的函数取决于它在 运行 时间调用的对象类型,因此您将得到如下内容:
public class ZeroSum {
public double sum(double x, float y)
{
return 0;
}
}
public class RealSum extends ZeroSum{
public double sum(double x, float y)
{
return x+y;
}
}
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
ZeroSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) ); //here the sum will return zero
System.out.println("Calling ZeroSum " + real.sum(x,y) ); //here the sum will return 30
}
因此,我必须使用数据类型 int、float、double 和 short 创建 68 种不同的求和方法。 class 称为 OverloadingSums。这是其中一种方法的示例。
public double sum(double x, float y, int z)
{
return x+y+z;
}
现在我被要求创建一个名为 ZeroSum 的 class,本质上是复制 OverloadingSum 并将所有内容归零。这是一个例子。
public double sum(double x, float y, int z)
{
return 0;
}
然后我被要求创建另一个名为 RealSum 的 class,它将扩展 ZeroSum class。我对这项作业的措辞有点困惑,不确定 Whosebug 社区是否可以提供帮助,但我非常困惑。
作业要求如下:
Now that we have thoroughly explored overloading we are going to explore overriding. Create a class called ZeroSum.java. Take all of the methods from OverloadingSum.java and change them to return all zeros in ZeroSum.java. Next, create a class called RealSum.java. This class, RealSum, will extend the ZeroSum class. Having all zeros for our sums isn't very useful. We will need to override the parent, or super class methods to produce real results. Create the necessary methods to override all of those in the ZeroSum.java. When you are done run your classes against DrivingSum.java.
这是我的主要方法:
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
RealSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) );
System.out.println("Calling ZeroSum " + real.sum(x,y) );
}
}
因此,根据我的理解,我编写了以下代码:
public class ZeroSum extends RealSum{
public double sum(double x, float y, int z)
{
return super.sum(x,y,z);
}
这将从 RealSum 中获取方法,而不是使用 ZeroSum class 中的求和方法。所以当我 运行 主要方法时, zero.sum(x,y) 给我 30.
之所以感到困惑,是因为作业要求我将 ZeroSum 中的所有内容设置为返回零。如果我将 ZeroSum 扩展为 RealSum,它实际上并没有什么不同。我这样做正确吗?还是我只是想得太多了?
我相信他是想说明你在父 class 中写的任何内容都会转移(这称为继承)给子。您可以通过使用与父 class 方法相同的名称和参数在子 class 中编写一个方法来覆盖它。
我也相信你看错了作业,它说:
This class, RealSum, will extend the ZeroSum class.
我将其解释为 RealSum 是 ZeroSum 的子代,而不是相反:
public class RealSum extends ZeroSum{
//code code code
}
这意味着 ZeroSum 中的所有内容都设置为 0,RealSum 设置新值,而不是使用 super。我不是打赌这是正确的,但在休息和呼吸新鲜空气后尝试再次阅读作业:)
希望对您有所帮助!
此作业的目标是探索最重要的概念,其中执行的函数取决于它在 运行 时间调用的对象类型,因此您将得到如下内容:
public class ZeroSum {
public double sum(double x, float y)
{
return 0;
}
}
public class RealSum extends ZeroSum{
public double sum(double x, float y)
{
return x+y;
}
}
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
ZeroSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) ); //here the sum will return zero
System.out.println("Calling ZeroSum " + real.sum(x,y) ); //here the sum will return 30
}