Java 扩展了奇怪的输出

发布于 2025-01-08 16:02:18 字数 1161 浏览 1 评论 0原文

我用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丢了幸福的猪 2025-01-15 16:02:18

下面两条语句:

masterClass mClass=new masterClass();
workClass wClass = new workClass();

创建两个完全独立的对象。当您修改其中一个时,不会影响另一个。

你的意思:

public static void main(String[] args) 
{
    workClass wClass = new workClass();

    wClass.setX(10);
    System.out.println(wClass.getX());

    wClass.setY(5);
    System.out.println(wClass.getY());

    System.out.println(wClass.addNum());
}

事实上,workClass 扩展了 masterClass,使您能够在 workClass 实例上使用 masterClass 的公共方法。

您的 addNum() 方法中还存在一个错误:

public class workClass extends masterClass 
{   
    public int addNum() {return getX() + getY();}
}

您当前的实现在构造时将数字相加,忽略对 xy 所做的任何更改代码> 构建后。

The following two statements:

masterClass mClass=new masterClass();
workClass wClass = new workClass();

create two objects that are completely independent from one another. When you modify one, this has no effect on the other.

Did you mean:

public static void main(String[] args) 
{
    workClass wClass = new workClass();

    wClass.setX(10);
    System.out.println(wClass.getX());

    wClass.setY(5);
    System.out.println(wClass.getY());

    System.out.println(wClass.addNum());
}

?

The fact that workClass extends masterClass enables you to use masterClass's public methods on an instance of workClass.

There is also a bug in your addNum() method:

public class workClass extends masterClass 
{   
    public int addNum() {return getX() + getY();}
}

Your current implementation adds the numbers together at construction time, disregarding any changes made to x and y post-construction.

眼睛会笑 2025-01-15 16:02:18

创建WorkClass 对象时,没有为xy 赋值。扩展一个类仅仅意味着继承数据成员。这并不意味着当您创建派生类的新对象时,您创建的所有基类对象都会自动复制到派生类。

You don't assign the value to x and y when you create the object of WorkClass. 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.

如痴如狂 2025-01-15 16:02:18

您已在 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.

绝情姑娘 2025-01-15 16:02:18

那里有两个错误:

  • 首先,您在一个对象(第一个 masterClass 实例)上设置值,并添加另一个对象(workClass,这是一个不同的对象实例)的值。
  • 其次:您的 num 属性将在创建时设置,并且以后不会重新评估。这是因为您已经定义了它并分配了值。

如何修复它:

public class kingClass 
{
   public static void main(String[] args) 
   {
      //fix the first error: set and calculate at the very same object
       workClass mClass=new workClass();

       mClass.setX(10);
       System.out.println(mClass.getX());

       mClass.setY(5);
       System.out.println(mClass.getY());

       System.out.println(mClass.addNum());
    }
}

以及:

public class workClass extends masterClass  {   

   public int addNum() {
      //fix the second point: don't save the value, but recalc every time you need with the actual current value of x and y
      return getX()+getY();
   }
}

You have two errors there:

  • First, you are setting the values on one object (first masterClass instance), and adding values from another one (the workClass, which is a different object instance).
  • Second: Your num attribute will be set at the creating time, and not reevaluated later. This is because you have defined it assigning the value.

How to fix it:

public class kingClass 
{
   public static void main(String[] args) 
   {
      //fix the first error: set and calculate at the very same object
       workClass mClass=new workClass();

       mClass.setX(10);
       System.out.println(mClass.getX());

       mClass.setY(5);
       System.out.println(mClass.getY());

       System.out.println(mClass.addNum());
    }
}

And:

public class workClass extends masterClass  {   

   public int addNum() {
      //fix the second point: don't save the value, but recalc every time you need with the actual current value of x and y
      return getX()+getY();
   }
}
暗喜 2025-01-15 16:02:18

mClasswClass 是 2 个不同的对象。

此外,添加是在对象创建期间发生的,因此 10 和 5 不会被添加。

因此,您需要对 kingClassworkClass 进行修改。

class kingClass 
{
public static void main(String[] args) 
{
    workClass wClass=new workClass();

    wClass.setX(10);
    System.out.println(wClass.getX());

    wClass.setY(5);
    System.out.println(wClass.getY());

    System.out.println(wClass.addNum());
}
}
class workClass extends masterClass 
{   
public int addNum() {return getX()+getY();}
}

mClass and wClass 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 also workClass.

class kingClass 
{
public static void main(String[] args) 
{
    workClass wClass=new workClass();

    wClass.setX(10);
    System.out.println(wClass.getX());

    wClass.setY(5);
    System.out.println(wClass.getY());

    System.out.println(wClass.addNum());
}
}
class workClass extends masterClass 
{   
public int addNum() {return getX()+getY();}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文