检查构造函数中的条件的方法

时间:2014-10-26 10:57:22

标签: methods constructor initialization

这是我写的代码:

class Car
public Car(String Company,String Model,int Color,String FuelType,double Price,String Engine)
{
boolean  check =  ConstInitCheck(Company,FuelType,Engine,Color);
if((check))
{
this.Company = Company;
this.Model = Model;
this.Color = Color;
this.FuelType = FuelType;
this.Price = Price;
this.Engine = Engine;
}
else System.out.println("Sorry One of the Inputs were wrong! Please Try Again!");
}


public Car(String Company,String Model,int Color,String FuelType,double Price,String Engine,String VIP_Features)
{
boolean  check =  ConstInitCheck(Company,FuelType,Engine,Color);
if((check))
{
this.Company = Company;
this.Model = Model;
this.Color = Color;
this.FuelType = FuelType;
this.Price = Price;
this.Engine = Engine;
if(!VIP_Features.equals(""))this.VIP_Features = VIP_Features;
else this.VIP_Features="None";
}
else System.out.println("Sorry One of the Inputs were wrong! Please Try Again!");
}



private boolean ConstInitCheck(String Company,String FuelType,String Engine,int Color)
{
boolean a = ((Company.toUpperCase().equals("SUZUKI"))||(Company.toUpperCase().equals("TOYOTA"))||(Company.toUpperCase().equals("HYUNDAI"))||
(Company.toUpperCase().equals("VOLKSWAGEN"))||(Company.toUpperCase().equals("FORD"))||(Company.toUpperCase().equals("HONDA"))||(Company.toUpperCase().equals
("CHEVROLET"))||(Company.toUpperCase().equals("SKODA"))||(Company.toUpperCase().equals("FIAT")));
boolean b = ((FuelType.toUpperCase().equals("DIESEL"))||(FuelType.toUpperCase().equals("PETROL"))||(FuelType.toUpperCase().equals("GAS")));
boolean c = ((Engine.toUpperCase().equals("V2"))||(Engine.toUpperCase().equals("V4"))||(Engine.toUpperCase().equals("V6"))||(Engine.toUpperCase().equals("V8"))||

(Engine.toUpperCase().equals("V12")));
boolean d = Color >= 0 & Color <= 255;
boolean initcheck = a && b && c && d;
return initcheck;
}

我在这里读到的一些帖子反对在构造函数中执行方法,因为对象没有完全初始化。 但由于我调用的方法只检查输入的实际参数是否正确 这是正确的方法吗? 如果没有,请告诉我最好的方法。 对代码的其他方面的建议或批评也是受欢迎的。

1 个答案:

答案 0 :(得分:1)

使用工厂方法。将检查移动到方法和构造函数外部。 结果是有效对象或根本没有对象。

    class Car{

       //private constructor to prevent creating objects from outside the factory method
       private Car(String Company,String Model,int Color,String FuelType,double Price,String Engine,String VIP_Features)
       {
          this.Company = Company;
          this.Model = Model;
          this.Color = Color;
          this.FuelType = FuelType;
          this.Price = Price;
          this.Engine = Engine;
       }

       //static method that creates the object after functional checks
       public static Car Create(String Company,String Model,int Color,String FuelType,double Price,String Engine,String VIP_Features)
       {
          if(ConstInitCheck(Company,FuelType,Engine,color)){
              return new Car(Company,Model,Color,FuelType,Price,Engine,VIP_Features);
          }
          return null; // or throw exception
       }

       private static boolean ConstInitCheck(String Company,String FuelType,String Engine,int Color){
          ...
       }

    }

用法:

var car = Car.Create(Company,Model,Color,FuelType,Price,Engine,VIP_Features);