Haxe java.lang.Object 等效
Haxe java.lang.Object Equivalent
Haxe 允许 class 继承层次结构
class Honda extends Car {
...
}
所有对象是否有一个共同的继承层次根?我有一个可以包含任何对象的通用容器 class,我希望能够声明
var _contents:Object; //Any class instance in _contents
我该怎么做?
类 的继承根是 Class<T>
,因此以下应该有效:
var _contents:Class<T>;
但是,要存储 Enum
,您必须改用 Enum<T>
。
来自manual:
There is a special type in Haxe which is compatible with all classes:
Define: Class<T>
This type is compatible with all class types which means that all classes (not their instances) can be assigned to it. At compile-time, Class<T>
is the common base type of all class types. However, this relation is not reflected in generated code.
This type is useful when an API requires a value to be a class, but not a specific one. This applies to several methods of the Haxe reflection API.
您还可以使用 {}
作为类型,它将接受 class 个实例以及匿名对象:
var _contents:{};
我们还有Dynamic
,这基本上意味着"anything"(不仅是对象,还有Bool
、Int
等基元)。
如果您的 class 是一个通用容器,您可能希望使用类型参数键入其内容:
class Container<T> {
var _contents:T;
public function new(contents:T):Void {
_contents = contents;
}
}
然后:
var arrayContainer = new Container([]);
var stuffContainer = new Container({foo:"bar"});
var classContainer = new Container( new Stuff() );
Haxe 允许 class 继承层次结构
class Honda extends Car {
...
}
所有对象是否有一个共同的继承层次根?我有一个可以包含任何对象的通用容器 class,我希望能够声明
var _contents:Object; //Any class instance in _contents
我该怎么做?
类 的继承根是 Class<T>
,因此以下应该有效:
var _contents:Class<T>;
但是,要存储 Enum
,您必须改用 Enum<T>
。
来自manual:
There is a special type in Haxe which is compatible with all classes:
Define:
Class<T>
This type is compatible with all class types which means that all classes (not their instances) can be assigned to it. At compile-time,
Class<T>
is the common base type of all class types. However, this relation is not reflected in generated code.This type is useful when an API requires a value to be a class, but not a specific one. This applies to several methods of the Haxe reflection API.
您还可以使用 {}
作为类型,它将接受 class 个实例以及匿名对象:
var _contents:{};
我们还有Dynamic
,这基本上意味着"anything"(不仅是对象,还有Bool
、Int
等基元)。
如果您的 class 是一个通用容器,您可能希望使用类型参数键入其内容:
class Container<T> {
var _contents:T;
public function new(contents:T):Void {
_contents = contents;
}
}
然后:
var arrayContainer = new Container([]);
var stuffContainer = new Container({foo:"bar"});
var classContainer = new Container( new Stuff() );