简体   繁体   English

在Java中遇到子类问题

[英]Having trouble with Subclasses in java

I was reviewing one of my textbook's questions and I am slightly confused. 我正在审阅教科书中的一个问题,我有些困惑。 The code is: 代码是:

public class BClass
{
  private int x;

  public void set(int a)
  {
    x=a;
  }

  public void print()
  {
    System.out.print(x);
  }
}

public class DClass extends BClass
  {
    private int y;


    public void set(int a, int b)
    {
      //Postcondition: x = a; y = b;
    }

The questions are: 问题是:

a. 一种。 Write the definition of the print method of DClass that overrides it. 编写覆盖它的DClass打印方法的定义。

b. b。 Write the definition of the method set of the class DClass. 编写类DClass的方法集的定义。

For a, would I be right in saying that putting the following in the subclass would be a successful override? 对于a,我是否正确地说,将以下内容放入子类将是成功的替代?

public void print()
{
  System.out.print(x + " and " + y);
}

I'm also having trouble with b. 我对b也有麻烦。 Because I'm given the postcondition, it's obvious that I should set y, the instance variable in the subclass, equal to b. 因为有了后置条件,所以很明显我应该将y(子类中的实例变量)设置为b。 The problem I'm facing is how to set x equal to a. 我面临的问题是如何将x设置为a。 Because the instance variable x belongs to the parent class, does that make it unable to be directly accessed? 因为实例变量x属于父类,这是否使其无法直接访问? Or am I allowed to do this: 还是我被允许这样做:

public void set(int a, int b)
{
  x = a;
  y = b;
}

Would really appreciate some help, Thanks! 非常感谢您的帮助,谢谢!

Below is correct. 下面是正确的。 A is incorrect. A不正确。 I was just looking to say that is how override works. 我只是想说那是重写的工作方式。 I didn't look at the validity of the method. 我没有看这种方法的有效性。

It should be 它应该是

public void print() {
   super.print();
   System.out.print(" and " + y);
}

For B you need to invoke the super method so 对于B,您需要调用super方法,因此

public void set(int a, int b) {
   super.set(a);
   y = b;
}

x is declared private in the base class. x在基类中声明为private That means that in the derived class, you cannot access it to set it or to print it. 这意味着在派生类中,您将无法访问它进行设置或打印。 However, the base class does have public methods to do both those things. 但是,基类确实具有公共方法来完成这两种操作。 Those methods are visible to the derived class, so you can/must use those. 这些方法在派生类中可见,这样你就可以/必须使用的。

In DClass.print, call the print method of the base class to print x : 在DClass.print中,调用基类的print方法来打印x

public void print()
{
    super.print();
    System.out.print(" and " + y);
}

The super keyword is used to call the implementation of print in the parent class. super关键字用于在父类中调用print的实现。 Without super. 没有super. in front of the call, it would call the same print method of the derived class, which would make the same call again, rapidly crashing with a stack overflow error. 在调用之前,它将调用派生类的相同打印方法,这将再次进行相同的调用,并由于堆栈溢出错误而迅速崩溃。

In DClass.set, you likewise need to call the set method of the base class: 在DClass.set中,您同样需要调用基类的set方法:

public void set(int a, int b)
{
    super.set(a);
    y = b;
}

Here, super. 在这里, super. is optional in front of the set call, because the only method named set which takes exactly 1 argument is in the base class, and it is not overridden (only overloaded ). set调用之前是可选的,因为唯一名为set方法仅接受1个参数,该方法位于基类中,并且不会被覆盖(仅重载 )。 However, including super. 但是,包括super. makes it clearer. 使其更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM