亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我得到,以及如何解決這個“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,"[email protected]"));....//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,"[email protected]"));

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 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號