【发布时间】:2021-02-15 16:17:01
【问题描述】:
我一直在做这个项目,所以我知道如何使用自定义对象和自定义对象类型。我遇到的问题是当我写出 System.out.println(fe1.TestAuto);它说需要标识符,我不知道为什么。我试图打印出该对象,但它不起作用。我遇到的另一个问题是,当我想使用 discountValue() 方法对成本进行折扣时,它说 double 不能转换为 Feature,我不知道为什么。
discountValue() 方法从双精度 c 中获取输入,并用要素类中的双精度 d 减去该输入。
下面是我的代码。有人可以帮助我吗?
public class Auto
{
public String name;
public double size;
public Auto(String s, double sz){
name = s;
size = sz;
}
//step 2: method showFeature()
public Feature showFeature(Feature f){
System.out.println("The feature " +f+" costs: "+f.cost);
}
public Feature discountValue(String s, double c, double d){
//Step 3: method to give discount on cost
double z = c - d.cost;
return z;
}
}
public class TestAuto
{
public static void main(String args[]){
//instantiate an Auto object
Auto at = new Auto("AMG", 73.5);
//instantiate a Feature object
Feature fe = new Feature("Leather Seat", 3000);
//execute method addFeature with Feature object
at.showFeature(fe);
//prepare a new Feature object for receving information
Feature fe1 = new Feature("", 0);
//Run method discountValue with a feature and a new value
fe1 = at.discountValue("GPS", 15000, 0.2);
//print out the object
System.out.println(fe1);
}
}
public class Feature
{
public String name;
public double cost;
public Feature(String s, double d){
//step 1
name = s;
cost = d;
}
//step 4
System.out.println(fe1.TestAuto);
}
【问题讨论】:
-
Class Feature 没有 TestAuto 字段。
标签: java dereference custom-object