gpt4 book ai didi

java - 作为集合的一部分创建后,如何在创建的对象中引用值

转载 作者:行者123 更新时间:2023-12-01 17:35:02 24 4
gpt4 key购买 nike

作为单位的一部分[原文如此],我们正在创建可由用户购买的保险单。所有不同类型的保险都可以在一份保单下购买。大部分基本代码已经提供给我们了。

我已经创建了一组 CoverType 类的对象,然后我尝试访问该对象中的 get 方法以添加在一个保单下购买的所有保险的费用,但它不允许我访问 setter/getter 。它回复错误“找不到交易品种方法 getPrice”。

这是保险单的代码

import java.util.*;
// Class: InsurancePolicy
// Purpose: To represent a particular customer's insurance agreement
public class InsurancePolicy {


//ArrayList<Product> orderList = new ArrayList<Product>();
private static double totalPolicyCost;

private static String name = null;

private static int price = 0;


private static Set<CoverType> TheCoverType = new HashSet<CoverType>();


// Each instance will have a unique policy number.
private int policyNumber;


private static int nextPolicy = 1;

public InsurancePolicy()
{

this.policyNumber = nextPolicy;
nextPolicy++;
}

// Determine this policy's number
public int getPolicyNumber()
{

return policyNumber;
}

// Method: getPolicyCost
// Purpose: to report to the caller, the total cost of this policy, based on the covers
// that are selected
public int getPolicyCost()
{
// Student must replace the following...

Iterator<CoverType> iter = TheCoverType.iterator();

while (iter.hasNext())
{

int cash = TheCoverType.getPrice();
int total = total + cash;
// int cost = TheCoverType.getPrice().next();
// if theCover


}
totalPolicyCost = totalPolicyCost + 100;
return 0;
}

// Method: includeCover
// Purpose: To allow the caller to specify a type of insurance cover to be included for this policy.
// Returns: True if the cover is now included; if it was already included or was unable to
// be included this time, false is returned.
public static boolean includeCover(CoverType which)
{


//CoverType initialCoverType = new CoverType(name,price);
//initialCoverType = which();
// Student must replace this following:

//TheCoverType = which;

for (CoverType element : TheCoverType)
{
if (!TheCoverType.contains(which))
{
TheCoverType.add(which);
return true;
}
else
{
System.out.println("The specified insurance has already been added");
return false;
}
}

System.out.println(TheCoverType);


return true;

}

// Method: lodgeAnotherClaim
// Purpose: To increase the counter of how many claims have been made against this policy.
// Parameter: lodgedType - specifies the type of cover being claimed. But only the types which
// have been included so far, will actually be countable.
public void lodgeAnotherClaim(CoverType lodgedType)
{
// Student must complete
for (CoverType element : TheCoverType)
{
if (!TheCoverType.contains(lodgedType))
{
TheCoverType.add(lodgedType);
}
else
{
System.out.println("The specified insurance has already been added");
}
}

System.out.println(TheCoverType);


}

// Method: toString
// Purpose: Display a textual summary of this object's state
@Override public String toString()
{
// Student must complete

return "Something";
}
}

错误出现在第 50 行,提示“找不到符号方法 getPrice()”。我想要的只是从 CoverType 类引用此方法

    public int getPrice()
{
return price;
}

最佳答案

变量 TheCoverType 的类型为 Set。这意味着它是 CoverType 对象的集合。 getPrice() 方法是在 CoverType 上定义的,而不是在 Set 上定义的。您需要在迭代器循环中将实际对象从 Set 中取出。类似CoverType type = iter.next()。然后您调用 type.getPrice() 而不是 TheCoverType.getPrice()

关于java - 作为集合的一部分创建后,如何在创建的对象中引用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7402748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com