Java 扩展了奇怪的输出
我用Java写了一个小程序,有一些东西我不明白。
我编写的程序有 3 个类 kingClass、masterClass 和 workClass。 workClass 扩展了 masterClass。
程序:在主类(kingClass)中我声明了masterClass和workClass,并且在masterClass中我给变量x和y赋予了值。在 kingClass 的末尾,我调用了一个 addNum 函数,该函数将 masterClass 中的两个数字相加。
现在的问题是:我期望当我运行程序时,它会给我输入的两个数字的总和,而不是我在构造函数中给出的数字的总和。
如何编写这个程序,以便 addNum 返回我输入的数字之和的值。
抱歉英语不好,谢谢..
kingClass
public class kingClass
{
public static void main(String[] args)
{
masterClass mClass=new masterClass();
mClass.setX(10);
System.out.println(mClass.getX());
mClass.setY(5);
System.out.println(mClass.getY());
workClass wClass = new workClass();
System.out.println(wClass.addNum());
}
}
masterClass
public class masterClass
{
private int x;
private int y;
masterClass()
{
x=0;
y=0;
}
public void setX(int a) {x=a;}
public void setY(int a) {y=a;}
public int getX() {return x;}
public int getY() {return y;}
}
workClass
public class workClass extends masterClass
{
int num=getX()+getY();
public int addNum() {return num;}
}
I have written a small program in Java, a there is something I just do not understand.
Program that I have wrote has 3 classes kingClass, masterClass, and workClass. workClass extends masterClass.
Program: in the main class (kingClass) I have declared masterClass and workClass, and with the masterClass I have given values to variables x, and y. In the end of kingClass I have called a addNum function that sum two numbers from the masterClass.
Now the problem: I expected when I run the program that it will give me a sum of two numbers I have given with input, not the sum of number that I have given value in constructor.
How can write this program so that addNum returns the value of the sum of number I have enterd.
Sorry for bad english, Thank you..
kingClass
public class kingClass
{
public static void main(String[] args)
{
masterClass mClass=new masterClass();
mClass.setX(10);
System.out.println(mClass.getX());
mClass.setY(5);
System.out.println(mClass.getY());
workClass wClass = new workClass();
System.out.println(wClass.addNum());
}
}
masterClass
public class masterClass
{
private int x;
private int y;
masterClass()
{
x=0;
y=0;
}
public void setX(int a) {x=a;}
public void setY(int a) {y=a;}
public int getX() {return x;}
public int getY() {return y;}
}
workClass
public class workClass extends masterClass
{
int num=getX()+getY();
public int addNum() {return num;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面两条语句:
创建两个完全独立的对象。当您修改其中一个时,不会影响另一个。
你的意思:
?
事实上,
workClass
扩展了masterClass
,使您能够在workClass
实例上使用masterClass
的公共方法。您的
addNum()
方法中还存在一个错误:您当前的实现在构造时将数字相加,忽略对
x
和y
所做的任何更改代码> 构建后。The following two statements:
create two objects that are completely independent from one another. When you modify one, this has no effect on the other.
Did you mean:
?
The fact that
workClass
extendsmasterClass
enables you to usemasterClass
's public methods on an instance ofworkClass
.There is also a bug in your
addNum()
method:Your current implementation adds the numbers together at construction time, disregarding any changes made to
x
andy
post-construction.创建
WorkClass
对象时,没有为x
和y
赋值。扩展一个类仅仅意味着继承数据成员。这并不意味着当您创建派生类的新对象时,您创建的所有基类对象都会自动复制到派生类。You don't assign the value to
x
andy
when you create the object ofWorkClass
. Extending a class just means that the data members are inherited. It doesn't mean that all the objects that you create of the base class will automatically be copied to the derived class when you create a new object of derived class.您已在 mClass 实例上而不是 wClass 上为 x 和 y 赋值,因此 num 将始终为零。
You've assign values to x and y on the mClass instance, not on wClass, so num will always be zero.
那里有两个错误:
如何修复它:
以及:
You have two errors there:
How to fix it:
And:
mClass
和wClass
是 2 个不同的对象。此外,添加是在对象创建期间发生的,因此 10 和 5 不会被添加。
因此,您需要对
kingClass
和workClass
进行修改。mClass
andwClass
are 2 different objects.Also the addition happens during object creation so 10 and 5 don't get added.
So you need to make modifications to
kingClass
and alsoworkClass
.