简体   繁体   中英

A Basic Class Problem about String and the object returned

This is a question about Java Class from my school previous quiz. I cannot handle it and can anyone help or offer some tips please? Thanks a lot.

在此处输入图像描述

For my answer in class A:

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

Since 'A's are an object, you need a constructor. 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. 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()

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

called name = myvalue;

It Means

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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