为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我得到,以及如何解决这个“String to object of type”

为什么我得到,以及如何解决这个“String to object of type”

qq_遁去的一_1 2022-12-28 14:29:40
我(作为一个绝对的初学者)正在尝试创建一个简单的工具,它可以创建一些对象并将它们链接起来。这些对象是:客户许可证(2 种类型,扩展类)这个想法是在创建许可证时使用(其中一个)客户公司名称,因此许可证链接到客户。我使用 ArrayLists 来存储数据。我尝试使用 Customer cCompany 的 getter,但是当我尝试实际创建一个新的许可证对象时,我收到有关不兼容类型的错误(String to object of type customer)我该如何修复该错误?非常感谢任何帮助,但请解释清楚,我是一个绝对的初学者。我可能把事情复杂化了……部分代码摘录:从主要: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 packageCustomer 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;        }
查看完整描述

2 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

下面一行是错误的。


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);

}


查看完整回答
反对 回复 2022-12-28
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

您需要做的是在创建对象时使用您已经创建的 Customer 对象之一ElvisLicense。为了更容易地按姓名找到该客户,我建议您将它们存储在地图中,而不是将名称作为键的列表。


Map<String, Customer> customerMap = new HashMap<>();

Customer customer = new Customer("TestCompany","John Doe",1234567890,"John@testcompany.com"));

customerMap.put(customer.getcCompany(), customer);

所以在创建许可证时你要查找客户


List <ElvisLicense> ellicenses = new ArrayList <> (10);

Customer customer = customerMap.get("TestCompany");

if (customer != null) {

    ElvisLicense license = new ElvisLicense(customer,"VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));

    ellicenses.add(license);

} else {

   //If the customer isn't found you need some kind of error handling, better than below :)

   System.out.println("Can't create a license, no customer found");

}


查看完整回答
反对 回复 2022-12-28
  • 2 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信