[英]java - Object cannot be resolved to variable
I'm trying to write a very simple, little java program but I'm already stuck at setting the object name. 我正在尝试编写一个非常简单的小型Java程序,但是我已经被困在设置对象名称上。 I have 2 classes, first is Starter: 我有2个课程,第一个是Starter:
public class Starter {
public static void main(String args[]) {
Family tester = new Family();
tester.setName(testers);
}
}
If i'm right I create a Family object called tester, then I use the setName method for giving the family a name. 如果我是对的,我创建了一个称为tester的Family对象,然后使用setName方法为该家族命名。 The Family class lookes like this: Family类看起来像这样:
public class Family{
String Name;
public void setName(String name){
Name = name;
}
}
But in the starter class at the tester.setName I get this error: tester cannot be resolved to a variable. 但是在tester.setName的入门类中,我得到此错误:无法将tester解析为变量。
Thanks in advance for the answers! 预先感谢您的回答!
Replace 更换
tester.setName(testers);
with 同
tester.setName("testers");
As your Family class's setName()
method takes a String
object and String
needs to be created either as above example or as below example: 由于Family类的setName()
方法采用String
对象,因此需要按照上面的示例或下面的示例创建String
:
String testers = new String("testers");
//and then you can use above String object as in your code snippet (as follows)
tester.setName(testers);
Unlike some other programming languages, in Java Strings must be enclosed in double quotes. 与某些其他编程语言不同,在Java中,字符串必须用双引号引起来。
Single quotes are used for chars, a primitive data type. 单引号用于chars(原始数据类型)。
You can change your code within; 您可以在其中更改代码;
tester.setName("testers"); tester.setName( “测试”);
You have some mistake in the setName(): 您在setName()中有一些错误:
public class Starter {
public static void main(String args[]) {
Family tester = new Family();
// tester.setName(testers);
// variable testers is not defined, you means set the name to "testers"? Try this:
tester.setName("testers");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.