简体   繁体   English

从抽象类toString方法扩展的类不起作用:/

[英]Class that is extended from an abstract class toString method doesn't work :/

This is the main demo class 这是主要的演示类

public class Ongoing{

  public static void main(String[] args){

    ExtendsAbstract josh = new ExtendsAbstract(5, "donkey");

    System.out.println(josh.toString());



  }

}

this is the class extended from the abstract class, the one who's tostring method won't work. 这是从抽象类扩展的类,tostring方法不起作用的类。

public class ExtendsAbstract extends Abstract{

  private String t;

  public ExtendsAbstract(int y, String t){
    super(y);
    this.t = t;
  }

  public String getString(){
    return this.t;  
  }

  public int getInt(){
    return super.getInt();
  }

  public String toString(int y){

    return(/*super.toString(y)+*/"The integer is "+ y) ;

  }

}

This is the abstract class 这是抽象类

public abstract class Abstract{

  private int y;


  public Abstract(int y){
    this.y = y;
  }

  public int getInt(){
    return y;
  }

  public String toString(int y){
    return("The integer is :"+y);
  }


}

Every time i try and access the toString method from the extended class it just prints out what i think is a memory address. 每次我尝试从扩展类访问toString方法时,它只打印出我认为的内存地址。 I even didn't mess with the abstract class and it still did that, does anyone know why? 我甚至没有弄乱抽象类,它仍然这样做,有谁知道为什么? Also another question about abstract classes, what advantages do they bring, is it just memory? 另外一个关于抽象类的问题,它们带来了什么优势,它只是记忆? Because you can't access private members from it so isn't it the same as a normal class, just more restrictions? 因为你不能从中访问私有成员所以它不是普通的类,只是更多的限制?

Currently, what happens is toString() of object is getting called which goes like this: ClassName@HashCode . 目前,发生的事情是toString()的对象被调用,如下所示: ClassName@HashCode

You can do this to print: (Add an @Override at toString()). 您可以执行此操作来打印:(在toString()处添加@Override)。 Also, no need to call josh.toString(). 此外,无需调用josh.toString()。 You can simply call System.out.println(josh); 你可以简单地调用System.out.println(josh); (magic will happen and it will call toString).. (魔法会发生,它会调用toString)..

public class ExtendsAbstract extends Abstract{

private String t;

public ExtendsAbstract(int y, String t){
 super(y);
 this.t = t;
}

 public String getString(){
   return this.t;  
}

public int getInt(){
return super.getInt();
}

@Override
public String toString(int y){

return(/*super.toString(y)+*/"The integer is "+ y) ;
}
}

The "toString()" method must be like this: “toString()”方法必须如下所示:

@Override
public String toString(){
     return("The integer is "+ y) ;
}

The "@Override" annotations is used for compiler checking, is not mandatory. “@Override”注释用于编译器检查,不是必需的。 More info about the annotation here: https://docs.oracle.com/javase/7/docs/api/java/lang/Override.html 有关此注释的更多信息,请访问: https//docs.oracle.com/javase/7/docs/api/java/lang/Override.html

Say that you have classes Dad and Son which are defined like this 假设您有类DadSon这样的定义

public class OverLoadedToString {

    public static void main(String[] args) {
        Son son = new Son();
        son.test();
        son.test(90);
    }
}

class Dad {

    void test() {
        System.out.println("Dad - test");
    } 
}

class Son extends Dad {

    void test(int testTime) {
        System.out.println("Son - test1" + testTime);
    }
}

The Son class is extending Dad so the test() with no arguments is inheriting to Son , just like your ExtendsAbstract class is having toString() with no arguments inheriting from Object class (every class in Java inherits Object class). Son类正在扩展Dad所以没有参数的test()继承到Son ,就像你的ExtendsAbstract类有toString()而没有继承自Object类的参数(Java中的每个类都继承了Object类)。

Then in Son class I added new method test(int testTime) , which has got an argument, that makes test() and test(int testTime) different which is called method overloading . 然后在Son类中我添加了新方法test(int testTime) ,它有一个参数,使test()test(int testTime)不同,称为方法overloading In you ExtendsAbstract class there are two toString methods one is the no-arg inherited and the other you defined. 在你的ExtendsAbstract类中有两个toString方法,一个是no-arg继承而另一个是你定义的。

Now let me show you the inheritance flow 现在让我告诉你继承流程

Object--->Abstract--->ExtendsAbstract 对象--->抽象---> ExtendsAbstract

Object class toString() methods prints the memory address, we can override it in our classes to change its definition, you can return any string that you want. 对象类toString()方法打印内存地址,我们可以在我们的类中重写它来改变它的定义,你可以返回你想要的任何字符串。 But you haven't overridden it anywhere in either Abstract class or ExtendsAbstract class, so in both classes it will print the memory address. 但是你没有在Abstract类或ExtendsAbstract类中的任何地方重写它,所以在这两个类中它都会打印内存地址。

Now in your Ongoing class you are calling that Object class toString() method which always prints memory address. 现在在你的Ongoing类中,你调用的是Object类toString()方法,该方法总是打印内存地址。 Your confusion is that you think you have overridden the toString() method but actually you have just overloaded it and you are calling the wrong method for your expected output. 您的困惑在于您认为已经覆盖了toString()方法,但实际上您刚刚重载它并且您正在为您的预期输出调用错误的方法。

Reference : Java overloading and overriding 参考: Java重载和覆盖

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

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