注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

notes

@-@

 
 
 

日志

 
 
 
 

Comparing the syntax of Java 5 and ActionScript 3  

2008-10-17 19:26:33|  分类: Flash |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

下表列出了两种语言的主要元素、概念的比较,以供参考。你可以从左向右或从右向左进行阅读。该列表并不完善,可在评论中添加。

Concept/Language Construct

Java 5.0

ActionScript 3.0

类库打包

.jar

.swc

继承 Inheritance

class Employee extends Person{…}

class Employee extends Person{…}

变量的声明与初始化 Variable declaration and initialization

String firstName=”John”;

Date shipDate=new Date();

int i;

int a, b=10;

double salary;

var firstName:String=”John”;

var shipDate:Date=new Date();

var i:int;

var a:int, b:int=10;

var salary:Number;

未声明变量 Undeclared variables

n/a

It’s an equivalent to the wild card type notation *. If you declare a variable but do not specify its type, the * type will apply.即任意类型。

A default value: undefined

var myVar:*;

变量作用域 Variable scopes

block: declared within curly braces, local: declared within a method or a block

 

member: declared on the class level

 

no global variables

No block scope: the minimal scope is a function

 

local: declared within a function

 

member: declared on the class level

 

If a variable is declared outside of any function or class definition, it has global scope.

字符串 Strings

Immutable, store sequences of two-byte Unicode characters

Immutable, store sequences of two-byte Unicode characters

语句结束符(;)

必须 A must

可省略 If you write one statement per line you can omit it.

严格相等运算符     Strict equality operator

n/a

===

for strict non-equality use

!==

常数 Constant qualifier

The keyword final

 

final int STATE=”NY”;

The keyword const

 

const STATE:int =”NY”;

类型检查Type checking

Static (checked at compile time)

Dynamic (checked at run-time) and static (it’s so called ‘strict mode’, which is default in Flex Builder)

类型检查运算符Type check operator

instanceof

is – checks data type, i.e. if (myVar is String){…}

The is operator is a replacement of older instanceof

as运算符 The as operator

n/a

Similar to is operator, but returns not Boolean, but the result of expression:

var orderId:String=”123”;

var orderIdN:Number=orderId as Number;

trace(orderIdN);//prints 123

基本数据类型Primitives

byte, int, long, float, double,short, boolean, char

all primitives in ActionScript are objects. Boolean, int, uint, Number, String

The following lines are equivalent;

var age:int = 25;

var age:int = new int(25);

复杂数据类型Complex types

n/a

Array, Date, Error, Function, RegExp, XML, and XMLList

数组声明与初始化Array declaration and instantiation

int quarterResults[];

quarterResults = new int[4];

 

 

int quarterResults[]={25,33,56,84};

 

var quarterResults:Array =new Array();

or

var quarterResults:Array=[];

 

var quarterResults:Array= [25, 33, 56, 84];

关联数组AS3 also has associative arrays that uses named elements instead of numeric indexes (similar to Hashtable).

根类 The top class in the inheritance tree

Object

Object

类型转换 Casting syntax: cast the class Object to Person:

 

Person p=(Person) myObject;

 

var p:Person= Person(myObject);

or

var p:Person= myObject as Person;

类型向上转换upcasting

class Xyz extends Abc{}

Abc myObj = new Xyz();

class Xyz extends Abc{}

var myObj:Abc=new Xyz();

无类型变量Un-typed variable

n/a

var myObject:*

var myObject:

包packages

package com.xyz;

class myClass {…}

package com.xyz{

class myClass{…}

}

ActionScript packages can include not only classes, but separate functions as well

类访问层次Class access levels

public, private, protected

if none is specified, classes have package access level

public, private, protected

if none is specified, classes have internal access level (similar to package access level in Java)

命名空间Custom access levels: namespaces

n/a

Similar to XML namespaces.

namespace abc;

abc function myCalc(){}

or

abc::myCalc(){}

use namespace abc ;

控制台输出Console output

System.out.println();

// in debug mode only

trace();

imports导入

import com.abc.*;

import com.abc.MyClass;

import com.abc.*;

import com.abc.MyClass;

packages must be imported even if the class names are fully qualified in the code.

无序键值对Unordered key-value pairs

Hashtable, Map

 

Hashtable friends = new Hashtable();

 

friends.put(”good”, “Mary”);

friends.put(”best”, “Bill”);

friends.put(”bad”, “Masha”);

 

String bestFriend= friends.get(“best”);

// bestFriend is Bill

Associative Arrays关联数组

 

Allows referencing its elements by names instead of indexes.

var friends:Array=new Array(); friends["good"]=”Mary”;

friends["best"]=”Bill”;

friends["bad"]=”Masha”;

 

var bestFriend:String= friends[“best”]

 

friends.best=”Alex”;

 

Another syntax:

var car:Object = {make:”Toyota”, model:”Camry”};

trace (car["make"], car.model);

// Output: Toyota Camry

Hoisting

n/a

Compiler moves all variable declarations to the top of the function, so you can use a variable name even before it’s been explicitly declared in the code.

对象实例化Instantiation objects from classes

Customer cmr = new Customer();

Class cls = Class.forName(“Customer”);

Object myObj= cls.newInstance();

var cmr:Customer = new Customer();

var cls:Class = flash.util.getClassByName(”Customer”); var myObj:Object = new cls();

私有类Private classes

private class myClass{…}

There is no private classes in AS3.

 

私有构造函数Private constructors

Supported. Typical use: singleton classes.

Not available. Implementation of private constructors is postponed as they are not the part of the ECMAScript standard yet.

To create a Singleton, use public static getInstance(), which sets a private flag instanceExists after the first instantiation. Check this flag in the public constructor, and if instanceExists==true, throw an error.

类和文件名Class and file names

A file can have multiple class declarations, but only one of them can be public, and the file must have the same name as this class.

A file can have multiple class declarations, but only one of them can be placed inside the package declaration, and the file must have the same name as this class.

包What can be placed in a package

Classes and interfaces

Classes, interfaces, variables, functions, namespaces, and executable statements.

动态类 Dynamic classes (define an object that can be altered at runtime by adding or changing properties and methods).

n/a

dynamic class Person {

var name:String;

}

//Dynamically add a variable // and a function

var p:Person = new Person();

p.name=”Joe”;

p.age=25;

p.printMe = function () {

trace (p.name, p.age);

}

p.printMe(); // Joe 25

函数闭包function closures

n/a. Closure is a proposed addition to Java 7.

myButton.addEventListener(“click”, myMethod);

A closure is an object that represents a snapshot of a function with its lexical context (variable’s values, objects in the scope). A function closure can be passed as an argument and executed without being a part of any object

抽象类Abstract classes

supported

n/a

函数覆盖Function overriding

supported

Supported. You must use the override qualifier

函数重载Function overloading

supported

Not supported.

接口Interfaces

class A implements B{…}

interfaces can contain method declarations and final variables.

class A implements B{…}

interfaces can contain only function declarations.

异常处理Exception handling

Keywords: try, catch, throw, finally, throws

 

Uncaught exceptions are propagated to the calling method.

Keywords: try, catch, throw, finally

 

A method does not have to declare exceptions.

Can throw not only Error objects, but also numbers:

 

throw 25.3;

 

Flash Player terminates the script in case of uncaught exception.

 

正则表达式Regular expressions

Supported

Supported

  评论这张
 
阅读(559)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018