haXe学习笔记(二)

一、基本数据类型

1. Bool

enum Bool {

true;

false;

}

2. Float

class Float { }

3. Int(比较奇怪,Int继承于Float,这样所有用到Float参数的地方都可以用Int,而其他语言则会把Int自动转换为Float)

class Int extends Float { }

4. String(有点不一样的是String可以换行),比如:

"This one has a line-break here"

5. EReg(Regular expressions)

~/[aZ]/g

new EReg("[aZ]","g")

6. Void

enum Void {

}

7. Dynamic

enum Dynamic<T> { }

8. null,类型为Unknown<0>,Flash9以上,基本类型不能赋值为null,但可以这样:

var e : Null<Int>;

二、操作符:

=

+=、*=、/=、%=、&=、|=、^=、<<=、>>=、>>>=

||

&&

e1...e2

==、!=、>、<、>=、<=

|、&、^

<<、>>、>>>

+

-

*

/

%

!、-、++、--、~

三、块Block

Block用{}表示,最后一行代码的值即Block的值,有点像Python:

var s:String = if(true) { "Vrai";} else { "Faux"; };

空Block({})的类型为Void

四、变量

1. 类变量

[public|private] [static] var varName [: varType] [= someValue];

默认为private(类似于其他语言的Protected、子类可以调用);如果类实现Public接口,则默认为public

2. 局部变量

var varName [: Type] [= varValue];

解析变量的顺序为

local variables

class members

current class static fields

enum

3. 函数变量

var func:String->Int->Bool = function(p:String, c:Int):Bool { return true; }

五、方法调用

instance.viriableName instance.methodName

六、创建实例

new ClassName(parameters);

七、数组Array

var a : Array<Int> = [1,2,3,4];

八、条件语句

1. if

if(condition) {

}

[else{

}]

2. switch(和其他语言不一样,case后面不需要也不能有break,可以用String、Int、Float、enum作为变量类型)

switch(variable){

case value1:

case value2, value3:

default:

}

九、循环

1. while

while( expr-cond ) expr-loop; do expr-loop while( expr-cond );

2. for

for( i in 0...a.length ) {}

这里变量i不需要声明

3. break、continue、return

十、异常

try{}catch(e:Error){}

try和catch都必须返回同样类型的值,或者不返回值

十一、匿名对象

比如:{ age : 12, name : "Benjamin" };

其类型为{age :Int, name : String}

十二、构造函数

构造函数只能有一个,可以用默认值overloading

public function new( x : Int, ?y : Int ) { this.x = x; this.y = (y == null) ? 0 : y; // "y" is optional }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值