试图实现一个类

时间:2013-02-03 04:31:46

标签: java

我试图获得帮助以运行我的汽车编译器我查了几件事我有1个错误

public class AutomobileDescription
{ 
    /**
    Constructor to display the make, model and price the new automobile I wish to purchase
   */

    public AutomobileDescription(String carMake, String carModel, carPrice) 
    {
        make = m; 
        model = mo;
        price = p; 
    } 
     public String m =("Toyota");
     public String mo =("Camry");
     public String p =("22055");

     public String getAutomobileinfo()
     {  
     return m + mo +  p;
     Automobile myAutomobile = new Automobile(Toyota, Camry, 22055);
     System.out.println("The Make, Model and Price of the car is: m + mo + p "); 

    }
}

---- jGRASP exec:javac -g AutomobileDescription.java

AutomobileDescription.java:7:错误:预期     public AutomobileDescription(String carMake,String carModel,carPrice)                                                                           ^ 1错误

---- jGRASP wedge2:进程的退出代码为1。  ---- jGRASP:操作完成。

3 个答案:

答案 0 :(得分:1)

public AutomobileDescription(String carMake, String carModel, carPrice) 
                                                              ^^^^^^^^

您已省略参数carPrice类型。很可能你想要

public AutomobileDescription(String carMake, String carModel, BigDecimal carPrice) 

另一个问题......

 public String getAutomobileinfo()
 {  
     return m + mo +  p;
     Automobile myAutomobile = new Automobile(Toyota, Camry, 22055);
     System.out.println("The Make, Model and Price of the car is: m + mo + p "); 
}

return语句意味着永远无法达到以下两个语句,这将在您纠正第一个问题后导致编译错误。

答案 1 :(得分:1)

这里有多个问题:

public class AutomobileDescription
{ 
    /**
    Constructor to display the make, model and price the new automobile I wish to purchase
   */

public AutomobileDescription(String carMake, String carModel, /*no return type*/ carPrice) 
{
    make = m; 
    model = mo;
    price = p; 
} 
 public String m =("Toyota");
 public String mo =("Camry");
 public String p =("22055");

    public String getAutomobileinfo()
    {  
     return m + mo +  p; /*return? then why statements after this?*/
     Automobile myAutomobile = new Automobile(Toyota, Camry, 22055);
     System.out.println("The Make, Model and Price of the car is: m + mo + p "); 

    }
}

解决方案:

public class AutomobileDescription{ 
/**
Constructor to display the make, model and price the new automobile I wish to purchase
*/

public AutomobileDescription(String carMake, String carModel, String carPrice) 
{
    m = make;
    mo = model;
    p = carPrice;
} 
 private String m;
 private String mo;
 private String p;

 public String getAutomobileinfo()
 {  
    return m + mo +  p;
 }
 public static void main(String[] args){
    AutomobileDescription myAutomobile = new AutomobileDescription("Toyota", "Camry", "22055");
    System.out.println("The Make, Model and Price of the car is: " + myAutomobile.getAutomobileinfo()); 
 }
}

答案 2 :(得分:0)

这不是有效的方法名称:

public String getMake + getModel + getPrice;

解决这个问题。如果您仍有问题,请更详细一点。也许甚至发布错误信息!