如何在超负荷的CFSCRIPT init()函数

问题描述 投票:2回答:1

这个例子是什么,我要怎样做,但ColdFusion的说`程序只能申报一次。 ColdFusion的可以做这样的事情?

/**
* @hint Handles vehicles 
*/
component Vehicle
{

    this.stock = "";
    this.year = "";
    this.make = "";
    this.model = "";

    public Vehicle function init()
    {
        return this;
    }

    public Vehicle function init(string stock)
    {
        this.stock = stock;
        //Get the year, make model of the stock number of this vehicle
        return this;
    }

    public string function getYearMakeModel() 
    {
        var yearMakeModel = this.year & " " & this.make & this.model;
        return yearMakeModel;
    }

}

奇怪的是,如果我拿出第一init(),我可以使用new Vehicle()new Vehicle(stocknumber)并呼吁init(string stocknumber)无论哪种方式,但是这不是我想要的行为......

coldfusion coldfusion-10
1个回答
3
投票

这是不可能过载使用日常使用ColdFusion。但是是可能的单一功能与不同的参数集(required=false)工作。这开辟了很多方法,你可以使用相同的功能,以用于不同的目的。

例如,下面的功能应该成为你试图同时实现构造函数的目的。

public Vehicle function init(string stock=''){
  if(len(trim(arguments.stock))){
    this.stock = arguments.stock;
  }
  return this;
}
© www.soinside.com 2019 - 2024. All rights reserved.