使用带有自定义类型特征参数的方法
Using a method with a custom type Feature parameter
所以我有三个 class 因为我正在尝试练习如何使用自定义类型对象并且我 运行 遇到了问题。在我的 Auto class 中,我创建了一个名为 showFeature() 的方法,我希望该方法接受一个参数,该参数将是自定义类型 Feature,它会打印出功能的名称和成本(例如:功能裤子成本:3000.0)。但是当我制作这个方法时,它一直在 Feature 的最后一个引用中说预期。谁能帮我解决这个问题
public class Feature
{
public String name;
public double cost;
public Feature(String s, double d){
//step 1
name = s;
cost = d;
}
//step 4
}
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 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){
System.out.println("The feature " +s+" costs: "+d);
}
public Feature discountValue(String s, double c, double d){
//Step 3: method to give discount on cost
double f = c - d;
return f;
}
}
现在您已经接收了一个没有附加变量的特征。您有一个功能 class,它创建了一个接受 String 和 Double 的功能对象,这可能是出现预期注释的原因,因为您没有为该功能提供任何输入。
//step 2: method showFeature()
public Feature showFeature(Feature f){
System.out.println("The feature " + f +" costs: "+ f.cost);
}
所以我有三个 class 因为我正在尝试练习如何使用自定义类型对象并且我 运行 遇到了问题。在我的 Auto class 中,我创建了一个名为 showFeature() 的方法,我希望该方法接受一个参数,该参数将是自定义类型 Feature,它会打印出功能的名称和成本(例如:功能裤子成本:3000.0)。但是当我制作这个方法时,它一直在 Feature 的最后一个引用中说预期。谁能帮我解决这个问题
public class Feature
{
public String name;
public double cost;
public Feature(String s, double d){
//step 1
name = s;
cost = d;
}
//step 4
}
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 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){
System.out.println("The feature " +s+" costs: "+d);
}
public Feature discountValue(String s, double c, double d){
//Step 3: method to give discount on cost
double f = c - d;
return f;
}
}
现在您已经接收了一个没有附加变量的特征。您有一个功能 class,它创建了一个接受 String 和 Double 的功能对象,这可能是出现预期注释的原因,因为您没有为该功能提供任何输入。
//step 2: method showFeature()
public Feature showFeature(Feature f){
System.out.println("The feature " + f +" costs: "+ f.cost);
}