我希望社区能够给我带来新的眼光,因为我对代码有点盲目了,
我已经将我必须连接在一起的两个类的代码附加在一起,以进行udemy教程。
在我编译它们时,通常会出现错误,变量拼写错误以及缺少半冒号。它还通过处理意外的类型,方法是将int更改为String。
我的问题在第25行,它给我的错误是
不兼容的类型,这里需要某种类型的表达式。您提供的帽子与其他类型的表达式不兼容。 (例如,您在预期为int的位置编写了String。)
据我所知,在所有实例中,被调用的变量都声明为字符串。
我用intelliJ编写了它,并使用它生成了getter / setter方法,因此这些方法都应该是正确的,而我一生都无法知道错误的出处。
我知道这很简单,但看不到树木的树林。
汽车课。
public class Car
{
// instance variables
private int numOfMilesDone; // a car has-a number of miles drive, "20000"
private int yearBought; // a car has-a year it was bought "1997"
private int carValue; // a car has-a value of what it is worth "300"
private Model modelName; // a car has-an model of type Model
/**
* Constructor for objects of class Car
*/
public Car(int aNumMiles, int aYearBought, int aValue, Model aModelName)
{
this.numOfMilesDone = aNumMiles;
this.yearBought = aYearBought;
this.carValue = aValue;
this.modelName = aModelName;
}
public Model getModelName()
{
if (this.modelName == null || this.getModelName() == null )
{
return ("needs to be checked");
}
return modelName;
}
/**
* Getter method for number of miles the car has done
* @return
*/
public int getNumOfMilesDone()
{
return numOfMilesDone;
}
/**
* Setter method for number of miles the car has done
* @return
*/
public void setNumOfMilesDone(int numOfMilesDone)
{
this.numOfMilesDone = numOfMilesDone;
}
/**
* Getter method for the year the car was bought
* @return
*/
public int getYearBought()
{
if (this.yearBought == null)
{
return "Needs to be checked";
}
return yearBought;
}
/**
* Setter method for the year the car was bought
* @return
*/
public void setYearBought(int yearBought)
{
this.yearBought = yearBought;
}
/**
* Getter method for the year the cars value in pounds
* @return
*/
public int getCarValue()
{
return carValue;
}
/**
* Setter method for the year the cars value in pounds
* @return
*/
public void setCarValue(int carValue) {
this.carValue = carValue;
}
public boolean isClassic()
{
return(Integer.parseInt(this.modelName.getYearOfModel()) < 1969);
}
/**
* returns the a String describing the object
* @return
*/
public String toSting()
{
return this.getModelName() + " has done " + this.numOfMilesDone + ", it is worth " + this.carValue + ", it is "
+ this.isClassic() + " it's a classic.";
}
}
我的另一个类Model,这没问题。
public class Model
{
private String modelName; // the model has a model name
private String yearOfModel; // the year the model was created
/**
* constructor method for no model attributes
*/
public Model()
{
this.modelName = null;
this.yearOfModel = null;
}
/**
* constructor method for known modelName attribute, but no yearOfModel attribute
* @param bModelName
*/
public Model(String bModelName)
{
this.modelName = bModelName;
this.yearOfModel = null;
}
/**
* constructor method for known modelName attribute, and known yearOfModel attribute
* @param bModelName
* @param yearOfModel
*/
public Model(String bModelName, String yearOfModel)
{
this.modelName = bModelName;
this.yearOfModel = yearOfModel;
}
/**
* modelName getter method
* @return
*/
public String getModelName() {
return modelName;
}
/**
* modelName setter method
* @param modelName
*/
public void setModelName(String modelName) {
this.modelName = modelName;
}
/**
* yearOfModel setter method
* @return
*/
public String getYearOfModel() {
return yearOfModel;
}
/**
* yearOfModel setter method
* @param yearOfModel
*/
public void setYearOfModel(String yearOfModel) {
this.yearOfModel = yearOfModel;
}
/**
* returns the modelName and yearOfModel variables as comprehensible information.
* @return
*/
public String toString()
{
return this.modelName + " was launched in " + this.yearOfModel;
}
}
预先感谢您的帮助。
答案 0 :(得分:2)
return "Needs to be checked"
,当您的方法签名建议为ModelName
时,您将返回一个字符串。
答案 1 :(得分:1)
您有问题,因为在使用返回值检测问题时,您使用selector
样式。在这种情况下,您将无法使用它,因为返回类型和字符串错误消息是不同的(例如C like
返回position或String.indexOf()
(如果找不到))。
在您的情况下,最好将-1
与消息一起抛出。
NullPointerException
这不是您的问题的答案,但是我认为您的代码还有另一个问题。下面是一些评论。
public Model getModelName() {
Objects.requireNonNull(modelName, "needs to be checked");
return modelName;
}
public int getYearBought() {
Objects.requireNonNull(yearBought, "Needs to be checked");
return yearBought;
}
答案 2 :(得分:0)
您需要更正方法getYearBought()
和getModelName()
中的两个。他们两个都返回字符串,而getYearBought()
期望返回一个int,getModelName()
期望返回Model
类的对象
答案 3 :(得分:0)
我可以看到一些问题。您的方法getModelName()
会自行调用this.getModelName()
它还返回类Model
,而modelName
的类型为Model
。但是然后在if语句中,您返回一个String而不是类Model
if (this.modelName == null || this.getModelName() == null ) {
return ("needs to be checked"); // This needs to return a Model
}
答案 4 :(得分:0)
有些错误跳出来:
您至少在两个地方有错误:
在getModelName()
中,您遇到两个问题:
1)您打算返回Model
,但在错误情况下返回String
。
2)您对getModelName()
进行了递归调用,而没有终止条件。
public Model getModelName()
{
if (this.modelName == null || this.getModelName() == null )
{
// NOTE: This is a String!
return ("needs to be checked");
}
return modelName;
}
可以这样重写:
public Model getModelName() {
return modelName;
}
当modelName
为null时,它将返回null,否则将返回非null Model
。
此外,在getYearBought()
中,您执行相同的操作-当您打算返回String
时,返回int
。
public int getYearBought() { 如果(this.yearBought == null) { //注意:这将返回String 返回“需要检查”; }
return yearBought;
}
不需要检查yearBought
是否为空,这是一个int
,并且不能为空。您可以这样重写它:
public int getYearBought() {
return yearBought;
}