SV——聲明和例化

1. class constructor ---- new

SV中通過new構造函數來創建對象,在創建對象的過程中,可以做一些初始化工作。

new函數沒有返回值,他的返回類型就是賦值表達式中左值的類型。

如果沒有自己定義new函數,那麼SV會調用默認的new函數;一個派生類的new函數會先調用父類的new函數。

 

2. super

  The super keyword is used from within a derived class to refer to members, class value parameters, or local value parameters of the base class(super關鍵字用來引用父類中的成員、參數等). It is necessary to use super to access members, value parameters, or local value parameters of a base class when those are overridden by the derived class(如果父類中的成員、參數被子類重載了,並且還想在子類中使用重載之前的父類的成員,那麼要用super)。

但是super只能用一層,如果super.super.new()這種方式,不行。

  A super.new call shall be the first statement executed in the constructor. This is because the superclass shall be initialized before the current class and, if the user code does not provide an initialization, the compiler shall insert a call to super.new automatically. super.new()會對父類中定義的成員初始化。

 

3. 聲明和例化

聲明是聲明一個變量,其中保存類對象的句柄。

例化是通過構造函數,創建對象,分配內存空間,並將聲明的句柄指向這段內存空間。

雖然在SV中可以在聲明的時候例化對象,但是不建議如此。一般是在塊語句之外聲明類對象,在塊語句內例化對象;可以控制對象的實例化順序。

 

4. 對象解除

在SV中自動管理對象創建時分配的內存。SV會記住指向一段內存的句柄的數目,如果一段內存沒有一個句柄指向它,那麼它就會被自動釋放掉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章