Joh*_*ith 0 java eclipse methods class this
除了getPrice()我返回的最后一个方法之外,一切都工作得很好,int但我一直得到同样的错误.此外,如果我将保修设为假,它仍然返回(基数+((基数/ 100)*10))
public class Machinery extends SaleGroup {
private float serial;
public int base;
private static boolean hasWarranty;
public Machinery(String newItemDescription, float newProductCode,
float newSerial, int newBasePrice) {
super(newItemDescription, newProductCode);
serial = newSerial;
base = newBasePrice;
}
public boolean IncludeWarranty() {
return hasWarranty=true;
}
public boolean ExcludeWarranty() {
return hasWarranty=false;
}
public float getSerial() {
return serial;
}
public int getPrice()
{
if (hasWarranty==true)
{
return (base+((base/100)*10));
}
else if (hasWarranty==false)
{
return base;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有3个类,SaleGroup.java,Machinery.java和MachineryTest.java
public abstract class SaleGroup {
private String item;
private float code;
//Constructor with name and code parameters for specifying
//access methods that return the name and code
public SaleGroup(String newItemDescription, float newProductCode)
{
item = newItemDescription;
code = newProductCode;
}
public String getItemDescription()
{
return item;
}
public float getProductCode()
{
return code;
}
public abstract int getPrice();
public String toString()
{
return "Item " + item + "has product code " + code + " and price is" + getPrice();
}
}
Run Code Online (Sandbox Code Playgroud)
MachineryTest.java
import javax.swing.JOptionPane;
public class MachineryTest {
public static void main(String[] args) {
String newItemDescription = "Item";
float newSerial = 4234;
float newProductCode = 3424;
int newBasePrice = 1000;
boolean hasWarranty=true;
Machinery test1 = new Machinery(newItemDescription, newProductCode,
newSerial, newBasePrice);
JOptionPane.showMessageDialog(
null,
"Item: " + test1.getItemDescription() + " Serial: "
+ test1.getSerial() + " Code: "
+ test1.getProductCode() + " Warranty Included: "
+ hasWarranty + " Price " + test1.getPrice());
}
}
Run Code Online (Sandbox Code Playgroud)
*更新:*
除了最后一个方法getPrice()之外,一切正常.我返回int但是我一直得到同样的错误.此外,如果我将保修设为假,它仍然返回(基数+((基数/ 100)*10))
如果您的if条件都不成立,则该方法不会返回任何内容.
编译器的可达性分析不够聪明,不能意识到bools必须始终是true或false(特别是因为那不是真的)
您可以通过删除iffrom else子句来解决此问题.
当你在它的时候,你也可以删除那个== true没用的部分.
| 归档时间: |
|
| 查看次数: |
3723 次 |
| 最近记录: |