需要对我的 Java 代码有一双全新的眼睛



我希望社区能为我提供一双新鲜的眼睛,因为我有点代码盲目,

我已经附加了我必须连接在一起的两个类的代码 udemy 教程。

当我编译它们时,出现了常见的错误,拼写错误的变量和缺少分号。它还通过一个意外的类型,这是通过将 int 更改为 String 来解决的。

我遇到的问题在第 25 行,它给我的错误是

不兼容的类型,这里需要某种类型的表达式。您提供的不同类型的帽子的表达式不兼容。(例如,您编写了一个需要 int 的字符串。

但是据我所知,被调用的变量被声明为字符串。

我用 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.";
}
}

我的另一个类,模型,编译没有问题。

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;
}
}

提前感谢您的帮助。

return "Needs to be checked"

当您的方法签名建议是ModelName时返回一个字符串。

您遇到了问题,因为当您使用返回值来检测问题时,您使用C like样式。在这种情况下,您不能使用它,因为返回类型和字符串错误消息不同(例如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;
}

这不是您的问题的答案,但我认为您的代码还有另一个问题。下面几条评论。

// it's better to check value when set it, but not when get (class instance should always contains correct value, this is plain old dto)
// do hot use useless JavaDoc: make code self documented
class Car {
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 value;  // a car has-a value of what it is worth "300"
private final Model model; // a car has-an model of type Model
// the name of method's parameters and local ones usually the same (use this for local ones)
public Car(int numOfMilesDone, int yearBought, int value, Model model) {
setNumOfMilesDone(numOfMilesDone);
setYearBought(yearBought);
setValue(value);
// use null object instead of null
this.model = model != null ? model : Model.NULL;
}
public Model getModel() {
return model;
}
public int getNumOfMilesDone() {
return numOfMilesDone;
}
public void setNumOfMilesDone(int numOfMilesDone) {
this.numOfMilesDone = Math.max(0, numOfMilesDone);
}
public int getYearBought() {
return yearBought;
}
public void setYearBought(int yearBought) {
this.yearBought = Math.max(0, yearBought);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return model + " has been done " + numOfMilesDone + ", it is worth " + value + ", it is " + model.isClassic() + " it's a classic.";
}
}
// you retrieve instance from `Car` class. It is better to make `Model` immutable and do not worry about encapsulation
final class Model {
public static final Model NULL = new Model(null, null);
// no need to use `Mode` in the name of internal properties
private final String name;
// usually this is integer, not a string
private final int year;
public Model(String name) {
this(name, 0);
}
public Model(String name, int year) {
this.name = name;
this.year = Math.max(0, year);
}
public String getName() {
return name;
}
public int getYear() {
return year;
}
// this method belongs to the Model, but not to a Car
public boolean isClassic() {
return this != NULL && year < 1969;
}
@Override
public String toString() {
return name + " was launched in " + year;
}
}

您需要更正getYearBought()getModelName()中的两种方法。它们都返回字符串,而getYearBought()期望返回一个 int,getModelName()期望返回Model类的对象

我可以看到一些问题。您的方法getModelName()称自己为this.getModelName()

此外,它还返回类ModelmodelName的类型为Model。但是在 if 语句中,您返回一个字符串而不是类Model

if (this.modelName == null || this.getModelName() == null ) {
return ("needs to be checked"); // This needs to return a Model
}

有几个错误跳出来了:

您至少在两个地方有错误:

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,否则Model非空。

此外,在getYearBought()中,您执行相同的操作 - 当您打算返回int时返回String

public int getYearBbuy() { if (this.yearBuy== null) { 注意:这将返回字符串 返回"需要检查"; }

return yearBought;

}

不需要检查yearBought

是否为 null,这是一个int,不能为 null。你可以这样重写它:

public int getYearBought() {
return yearBought;
}

最新更新