java - 为什么我会得到,我该如何解决这个“字符串到对象类型“ 错误
问题描述
我(作为一个绝对的初学者)正在尝试创建一个简单的工具,它可以创建一些对象并将它们链接起来。对象是:客户许可证(2 种类型,扩展类)
这个想法是在创建许可证时使用(其中一个)客户公司名称,因此许可证链接到客户。我使用 ArrayLists 来存储数据。
我尝试为客户 cCompany 使用 getter,但是当我尝试实际创建新的许可证对象时,我收到有关不兼容类型的错误(字符串到客户类型的对象)
我该如何解决这个错误?
任何帮助都非常感谢,但请解释清楚,我是一个绝对的初学者。我可能过于复杂的东西......
一些代码摘录:
从主要:
public class Main {
public static void main(String[] args) {
//Create customers
List <Customer> customers = new ArrayList <> (10);
customers.add(new Customer("TestCompany","John Doe",1234567890,"John@testcompany.com"));
....
//Create Elvis licenses (based on superclass License)
List <ElvisLicense> ellicenses = new ArrayList <> (10);
ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
类别: 客户:
class Customer {
String cCompany;
private String cName;
private int cPhone;
private String cEmail;
public Customer( String cCompany, String cName,int cPhone, String cEmail)
{
this.cCompany = cCompany;
this.cName = cName;
this.cPhone = cPhone;
this.cEmail = cEmail;
}
//This getter should be used to link the license to the customer (Done in License.java)
public String getcCompany() {
return cCompany;
}
类许可证(超类)
class License {
// Used no modifier to set access for Class/Package and Subclass inside the package
Customer licenseCompany;
String lVendor;
int lContractNumber;
String lCertificateNumber;
String lProductName;
String lLicenseKey;
int lNumberOfSeats;
public License(Customer cCompany, String lVendor, int lContractNumber, String lCertificateNumber,
String lProductName, String lLicenseKey, int lNumberOfSeats)
{
licenseCompany = cCompany;
this.lVendor = lVendor;
this.lVendor = lVendor;
this.lContractNumber = lContractNumber;
this.lCertificateNumber = lCertificateNumber;
this.lProductName = lProductName;
this.lLicenseKey = lLicenseKey;
this.lNumberOfSeats = lNumberOfSeats;
}
public Customer getLicenseCompany() {
return licenseCompany;
}
public void setLicenseCompany(Customer licenseCompany) {
this.licenseCompany = licenseCompany;
}
//preparations to allow for example printing the content of an arraylist element
@Override
public String toString(){
return "Customer name " + getLicenseCompany() + "\n" + "Vendor name " + getlVendor() + "\n" + "Contract number: " + getlContractNumber() + "\n"
+ "Certificate number: " + getlCertificateNumber() + "\n" +
"Product name " + getlProductName() + "\n" + "Licence key: " + getlLicenseKey() + "\n"
+ "Number of seats: " + getlNumberOfSeats();
}
}
和扩展类:
public class ElvisLicense extends License{
private boolean elIsBundle;
private boolean elIsSubscription;
public ElvisLicense(
Customer licenseCompany,
String lVendor,
int lContractNumber,
String lCertificateNumber,
String lProductName,
String lLicenseKey,
int lNumberOfSeats,
boolean elIsBundle,
boolean elIsSubscription
)
{
super(
licenseCompany,
lVendor,
lContractNumber,
lCertificateNumber,
lProductName,
lLicenseKey,
lNumberOfSeats);
this.elIsBundle = elIsBundle;
this.elIsSubscription = elIsSubscription;
}
.....
@Override
public String toString(){
return "Customer name " + licenseCompany + "\n"
+ "Vendor name " + lVendor + "\n"
+ "Contract number: " + lContractNumber + "\n"
+ "Certificate number: " + lCertificateNumber + "\n"
+ "Product name " + lProductName + "\n"
+ "Licence key: " + lLicenseKey + "\n"
+ "Number of seats: " + lNumberOfSeats + "\n"
+ "Number of seats: " + elIsBundle + "\n"
+ "Number of seats: " + elIsSubscription;
}
}
我希望在创建新许可证时使用客户名称。
解决方案
下面的行是错误的。
ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
作为许可证需要客户对象一个参数。相反,您应该首先创建客户对象。
ellicenses.add(new ElvisLicense(new Customer("TestCompany","VendorA",1234,"1234-A"),"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
重复使用它customer list
以避免创建公司。
for(Customer customer : customers){
// here you need some way to offer other parameters except customer parameter.
License license = new new ElvisLicense(customer,"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true);
ellicenses.add(license);
}
推荐阅读
- python - Python将变量传递到有效负载400错误
- python - 带有颜色编码的动画散点图会失去颜色(Python / Plotly)
- hacklang - 如何在 Hacklang 中将元组转换为 vec 或 varray?
- mobile - 我的引导网站的移动版本不工作
- blazor - 如何在 Blazor 应用程序中实现多选下拉列表?
- bash - 如何在for循环中跳过ls的第一个元素
- mongodb - 如何在数组 Mongoose 的对象中填充 2 个 id
- c++ - 带有自定义散列函数和比较谓词的 unordered_map 会产生编译错误
- c++ - 为什么在 x86 上除以 3 需要右移(和其他奇怪的东西)?
- r - 应用于 R 中数据帧的函数中的嵌套向量