简体   繁体   English

关于字符串和 object 返回的基本 Class 问题

[英]A Basic Class Problem about String and the object returned

This is a question about Java Class from my school previous quiz.这是我学校以前的测验中关于 Java Class 的问题。 I cannot handle it and can anyone help or offer some tips please?我无法处理它,任何人都可以帮助或提供一些提示吗? Thanks a lot.非常感谢。

在此处输入图像描述

For my answer in class A:对于我在 class A 中的回答:

public String A (String name){
     return name;
}

Since 'A's are an object, you need a constructor.由于'A'是一个object,你需要一个构造函数。 So you would likely have something like:所以你可能会有类似的东西:

public A(String name){
    this.name = name;
}

After this, you need to overwrite toString(), since 'A's are objects and printing them normally will not work.在此之后,您需要覆盖 toString(),因为 'A' 是对象并且通常无法打印它们。 Thus, you would then need:因此,您将需要:

public String toString(){
    return this.name;
}

Thus, when you call SoPL(), it will run this version instead of Object.toString()因此,当您调用 SoPL() 时,它将运行此版本而不是 Object.toString()

Consider method with name A, where you are creating an object of A and can pass the parameter as a string, then the method A can return a string which belongs to the variable考虑名称为 A 的方法,您在其中创建 A 的 object 并且可以将参数作为字符串传递,然后方法 A 可以返回属于该变量的字符串

called name = myvalue;称为名称= myvalue;

It Means它的意思是

public A(String name){
this.name = name;}
String name = myvalue;

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

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