简体   繁体   English

需要重新审视我的 Java 代码

[英]Need a fresh set of eyes with my Java code

I am hoping the community can provide me with a fresh set of eyes, as i have gone a bit code blind,我希望社区能为我提供一双新的眼睛,因为我对代码有点盲目,

I have attached my code from both the classes that I have to connect together for a udemy tutorial.我已经附上了我必须连接在一起以获取 udemy 教程的两个类的代码。

As I compiled them, the usual errors came up, mis-spelled variables and missing semi colons.当我编译它们时,出现了常见的错误、拼写错误的变量和缺少分号。 It also through up an unexpected type, which was solved by changing a int to a String.它还通过了一个意想不到的类型,这是通过将 int 更改为 String 来解决的。

The problem I have is on line 25, the error it gives me is我的问题在第 25 行,它给我的错误是

incompatible types, There was an expression of a certain type required here.不兼容的类型,这里需要某种类型的表达式。 You provided an expression of a different type hat is not compatible.您提供的不同类型的表达式不兼容。 (eg you wrote a String where an int was expected.) (例如,您在需要 int 的地方编写了一个 String。)

But variable being called is declared a string as far as I can see in all instances.但是就我在所有实例中看到的而言,被调用的变量被声明为一个字符串。

I wrote it in intelliJ and used that to generate the getter/setter methods so these should all be correct, and I just can't for the life of me see where the error is coming from.我在 intelliJ 中编写了它并用它来生成 getter/setter 方法,所以这些都应该是正确的,而且我一生都无法看到错误的来源。

I know it will be something simple but just can't see the woods for the trees.我知道这将是一些简单的事情,但只是见树不见林。

Car class.汽车类。

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

}

My other class, Model, this compiles no problem.我的另一个类 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;
    }
}

Thanks in advance for your help.在此先感谢您的帮助。

return "Needs to be checked"当您的方法签名建议是ModelName时,您将返回一个字符串。

You have a problem because you use C like style, when return value is used to detect a problem.当使用返回值来检测问题时,您会遇到问题,因为您使用了C like样式。 In this given case, you cannot use it, because return type and string error message are not the same (eg String.indexOf() returns either position or -1 if not found).在这种给定的情况下,您不能使用它,因为返回类型和字符串错误消息不相同(例如String.indexOf()返回位置或-1如果未找到)。

In yous situation it's better to throw NullPointerException with the message.在你的情况下,最好用消息抛出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;
}

This is not an answer your question, but I think you have another problem with your code.这不是您的问题的答案,但我认为您的代码还有另一个问题。 Below a few comments.下面发表几点意见。

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

You need to correct two of the methods getYearBought() and getModelName() .您需要更正getYearBought()getModelName()方法中的两个。 Both of them are returning strings whereas getYearBought() expects an int to be returned and getModelName() expects an object of Model class to be returned它们都返回字符串,而getYearBought()期望返回一个 int 而getModelName()期望返回一个Model类的对象

A few issues I can see.我能看到的几个问题。 Your method getModelName() calls itself this.getModelName()你的方法getModelName()调用自己this.getModelName()

Also it returns the class Model and modelName is of type Model .它还返回类ModelmodelNameModel类型。 But then in you if statement you return a String and not the class Model但是在你的 if 语句中你返回一个 String 而不是类Model

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

There are a couple of things wrong that jump out:有一些错误会跳出来:

You have errors in at least two places:您至少在两个地方有错误:

In getModelName() you have two problems:getModelName()有两个问题:

1) You intend to return a Model but return a String instead in error cases. 1) 您打算返回一个Model但在错误情况下返回一个String

2) You make a recursive call to getModelName() without a terminating condition. 2)您在没有终止条件的情况下递归调用getModelName()

 public Model getModelName()
 {
  if (this.modelName == null || this.getModelName() == null )
  {
     // NOTE: This is a String!
     return ("needs to be checked");
  }

  return modelName;
 }

This could be rewritten like this:这可以像这样重写:

public Model getModelName() {
    return modelName;
}

It would return null when modelName is null, and a non-null Model otherwise.modelName为 null 时,它将返回 null, modelName返回非 null Model

Also, in getYearBought() , you do the same thing - return a String when you intend to return an int .此外,在getYearBought() ,您做同样的事情 - 当您打算返回一个int时返回一个String

public int getYearBought() { if (this.yearBought == null) { // NOTE: This returns String return "Needs to be checked"; public int getYearBought() { if (this.yearBought == null) { // 注意:这会返回字符串 return "Needs to be checked"; } }

  return yearBought;

} }

Checking that yearBought is null isn't required, this is an int and it can't be null.不需要检查yearBought是否为 null,这是一个int并且不能为 null。 You can rewrite it like this:你可以像这样重写它:

public int getYearBought() {
    return yearBought;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM