分享
 
 
 

As3.0与java 5语法对比

王朝java/jsp·作者佚名  2006-11-16
窄屏简体版  字體: |||超大  

Below is a short comparison table of major elements/concepts of these two languages for a quick reference.

You can read this table either left-to-right or right-to-left, depending on what’s your primary programming language is today.

This list is not complete, and your input is appreciated.

Concept/Language Construct

Java 5.0

ActionScript 3.0

Class library packaging

.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

Terminating statements with semicolons

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

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 declaration 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

Person p= 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

Thanks,

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有