【发布时间】:2014-01-29 10:55:49
【问题描述】:
我有两个类:“Add.java”和“Subtract.java”。我想在“subfn”方法(类Subtract)中使用“addfn”方法(类Add)的结果。我该怎么做?
Add.java
public class Add
{
public double a, b;
public double addfn(double a, double b)
{
return (a+b);
}
}
Subtract.java
public class Subtract
{
double c, d;
public double subfn(double d)
{
//
//I want the "addfn" from class "Add" to accept the variables *a* and *b*
//and then use "subfn" to use the result of "addfn" and assign it to *c*
//
Add obj1 = new Add();
//Can I somehow access the "addfn" method here?
c = a+b; //and then assign its result to c here?
return (c-d);
}
}
我尝试使用类 Subtract 扩展类 Add,如下所示:
public class Subtract extends Add
但 Java 只是将空值分配给 a 和 b,因此 c 始终为 0。它也无法让我访问Add 类中的“addfn”。
我该怎么做?谢谢。
编辑:感谢您的所有回答,但我怎样才能专门运行另一个类的函数?以及如何立即连续运行这两个功能?再次感谢。
【问题讨论】: