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会记住指向一段内存的句柄的数目,如果一段内存没有一个句柄指向它,那么它就会被自动释放掉。

 

 

真理剑客是个武功高强的朝圣者,独自战胜了三个匪徒,在基督徒到达终点之前加入了他的朝圣队伍。
原文地址:https://www.cnblogs.com/east1203/p/11598033.html