java “不兼容的类型:字符串不能转换为人”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32447190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Incompatible types: String cannot be converted to Person"?
提问by Tom K
For class I have to create a Java class definition to describe a car loan taken by two people. It has to contain:
对于类,我必须创建一个 Java 类定义来描述两个人借用的汽车贷款。它必须包含:
- bankName (a string)
- accountNumber (an integer)
- signer (a Person)
- coSigner (a Person)
- 银行名称(字符串)
- accountNumber(整数)
- 签字人(一个人)
- 共同签名者(一个人)
It also has to have a getInfo() method which concatenates bankName and accountNumber.
它还必须有一个 getInfo() 方法来连接 bankName 和 accountNumber。
Below is my what I have for the car loan class in which I'm trying to create, but I also have to create another class file called Person.
下面是我尝试创建的汽车贷款类的内容,但我还必须创建另一个名为 Person.class 的类文件。
CarLoan.java:
CarLoan.java:
package carloandemo.csc212hw01;
public class CarLoan {
public static void main(String[] args) {
String bankName;
bankName = "Chase";
int accountNumber;
accountNumber = 123456;
Person signer;
signer = "Rich";
Person coSigner;
coSigner = "Tim";
String print = getInfo(bankName, accountNumber, signer, coSigner);
System.out.println(print);
// System.out.println(getInfo() );
}
public static String getInfo(String bankName, int accountNumber, String signer, String coSigner) {
return "The bank is " + bankName + " and the account number is " + accountNumber + ". " + "The signer of the loan is " + signer + " and the co-signer is " + coSigner + ".";
}
}
Person.java:
人.java:
package carloandemo.csc212hw01;
public class Person {
public String signer;
public String coSigner;
}
For the code where I assign the Person variables signer and coSigner values, I am recieving an error that says "Incompatible types: String cannot be converted to Person". Because of this I am also receiving that error on the String print statement. How can I fix this? What am I doing wrong?
对于分配 Person 变量 signer 和 coSigner 值的代码,我收到一条错误消息,指出“类型不兼容:字符串无法转换为 Person”。因此,我也在 String 打印语句中收到该错误。我怎样才能解决这个问题?我究竟做错了什么?
回答by Gabriele Mariotti
You can't do it.
你不能这样做。
Person signer;
signer = "Rich";
You are defining signer as a Person
, and you are assigning a String
value.
您将签名者定义为 a Person
,并且您正在分配一个String
值。
First of all You have to init the signer
to a new Person()
, then you can assign the variable inside the new instance.
首先,您必须将 a 初始化signer
为 a new Person()
,然后您可以在新实例中分配变量。
For example:
例如:
Person signer = new Person();
signer.signer = "Rich";
May be you can change your code in this way:
也许您可以通过这种方式更改代码:
public class Person {
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
private String name;
private String surname;
//getter
}
public class CarLoan {
public static void main(String[] args) {
String bankName;
bankName = "Chase";
int accountNumber;
accountNumber = 123456;
Person signer = new Person("Rich" , "xxx");
Person cosigner = new Person("Tim" , "xxx");
//...
}
}
回答by Eric J.
This is a case where the error message is telling you exactly what the problem is
在这种情况下,错误消息准确地告诉您问题是什么
“Incompatible types: String cannot be converted to Person”?
“不兼容的类型:字符串不能转换为人”?
Your problem is here
你的问题在这里
Person signer;
signer = "Rich";
"Rich" is a string, not a person.
“富”是一个字符串,而不是一个人。
Create a new instance of Person and then assign "Rich" to the signer
property of that instance.
创建一个新的 Person 实例,然后将“Rich”分配给signer
该实例的属性。
Person person = new Person();
person.signer = "Rich";
Note though looking at this code:
请注意,虽然查看此代码:
Person coSigner;
coSigner = "Tim";
String print = getInfo(bankName, accountNumber, signer, coSigner);
you probably don't actually want the Person
object to contain the signer
and coSigner
properties. A better definition is probably
您可能实际上并不希望Person
对象包含signer
和coSigner
属性。更好的定义可能是
public class Person {
public String name;
// Other things that describe a person here
}
If you change Person that way, your final code would be
如果您以这种方式更改 Person,您的最终代码将是
Person signer = new Person();
signer.name = "Rich";
Person coSigner = new Person();
coSigner.name = "Tim";
String print = getInfo(bankName, accountNumber, signer, coSigner);