我无法使用访问器方法将toString中的字符返回到主类中。
public class Alpha
{
.
.
.
public char getLetter()
{
return letter;
}
public String toString()
{
String str = "This is the letter " + letter;
return str;
}
第二个程序
public class AlphaDemo
{
public static main........
{
Alpha xxNewLetter = new Alpha(.....);
System.out.println(xxNewLetter);
}
}
我不明白我做错了什么。
答案 0 :(得分:0)
您的课程应如下所示。
public class Alpha {
char letter;
public Alpha(char letter) {
this.letter = letter;
}
public char getLetter() {
return letter;
}
public String toString() {
String str = "This is the letter " + letter;
return str;
}
}
和Alpha演示
public class AlphaDemo {
public static void main(String arg[]) {
Alpha xxNewLetter = new Alpha('A');
System.out.println(xxNewLetter);
}
}