猿问

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

我(作为一个绝对的初学者)正在尝试创建一个简单的工具,它可以创建一些对象并将它们链接起来。这些对象是:客户许可证(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 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;    

    }


qq_遁去的一_1
浏览 150回答 2
2回答

FFIVE

下面一行是错误的。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){&nbsp; &nbsp;// here you need some way to offer other parameters except customer parameter.&nbsp; &nbsp;License license = new new ElvisLicense(customer,"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true);&nbsp; &nbsp;ellicenses.add(license);}

慕田峪9158850

您需要做的是在创建对象时使用您已经创建的 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) {&nbsp; &nbsp; ElvisLicense license = new ElvisLicense(customer,"VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));&nbsp; &nbsp; ellicenses.add(license);} else {&nbsp; &nbsp;//If the customer isn't found you need some kind of error handling, better than below :)&nbsp; &nbsp;System.out.println("Can't create a license, no customer found");}
随时随地看视频慕课网APP

相关分类

Java
我要回答