1. 程式人生 > >建構函式返回值

建構函式返回值

建構函式返回值
For purposes other than simple initialization, classes can have constructors. Constructors are blocks of statements that can be used to initialize an object before the reference to the object is returned by new. Constructors have the same name as the class they initialize. Like methods, they take zero or more arguments, but constructors are not methods and thus have no return type. Arguments, if any, are provided between the parentheses that follow the type name when the object is created with new. Constructors are invoked after the instance variables of a newly created object of the class have been assigned their default initial values and after their explicit initializers are executed.

譯文------除了簡單的初始化之外,類還可以有建構函式。建構函式是語句塊,可用於在new返回物件引用之前初始化物件。建構函式的名稱與它們初始化的類相同。與方法一樣,它們接受零個或多個引數,但建構函式不是方法,因此沒有返回型別。當用new建立物件時,在緊跟型別名的括號之間提供引數(如果有的話)。建構函式是在類的新建立物件的例項變數被賦值並執行顯式初始化後呼叫的。

構造方法是一種特殊的方法,具有以下特點。
(1)構造方法的方法名必須與類名相同。
(2)構造方法沒有返回型別,也不能定義為void,在方法名前面不宣告方法型別。
(3)構造方法的主要作用是完成物件的初始化工作,它能夠把定義物件時的引數傳給物件的域。
(4)構造方法不能由程式設計人員呼叫,而要系統呼叫。
(5)一個類可以定義多個構造方法,如果在定義類時沒有定義構造方法,則編譯系統會自動插入一個無引數的預設構 造器,這個構造器不執行任何程式碼。
(6)構造方法可以過載,以引數的個數,型別,或排列順序區分。

個人認為。建構函式返回的是類的物件的引用----簡單說就是返回物件的地址