Comparing the syntax of Java 5 and ActionScript 3

2006.11.14 09:15 [ 工作 ] 评论(0) , 阅读(3084) | |
From:http://flexblog.faratasystems.com/?p=115

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 ConstructJava 5.0ActionScript 3.0
Class library packaging.jar.swc
Inheritanceclass Employee extends Person{…}class Employee extends Person{…}
Variable declaration and initializationString 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 variablesn/aIt’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 scopesblock: 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.
StringsImmutable, store sequences of two-byte Unicode charactersImmutable, store sequences of two-byte Unicode characters
Terminating statements with semicolonsA mustIf you write one statement per line you can omit it.
Strict equality operatorn/a===
for strict non-equality use
!==
Constant qualifierThe keyword final
final int STATE=”NY”;
The keyword const
const STATE:int =”NY”;
Type checkingStatic (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 operatorinstanceofis – checks data type, i.e. if (myVar is String){…}
The is operator is a replacement of older instanceof
The as operatorn/aSimilar 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
Primitivesbyte, int, long, float, double,short, boolean, charall 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 typesn/aArray, Date, Error, Function, RegExp, XML, and XMLList
Array declaration and instantiationint 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 treeObjectObject
Casting syntax: cast the class Object to Person:Person p=(Person) myObject;var p:Person= Person(myObject);
or
var p:Person= myObject as Person;
upcastingclass Xyz extends Abc{}
Abc myObj = new Xyz();
class Xyz extends Abc{}
var myObj:Abc=new Xyz();
Un-typed variablen/avar myObject:*
var myObject:
packagespackage com.xyz;
class myClass {…}
package com.xyz{
class myClass{…}
}
ActionScript packages can include not only classes, but separate functions as well
Class access levelspublic, 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: namespacesn/aSimilar to XML namespaces.
namespace abc;
abc function myCalc(){}
or
abc::myCalc(){}
use namespace abc ;
Console outputSystem.out.println();// in debug mode only
trace();
importsimport 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 pairsHashtable, 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
Hoistingn/aCompiler 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 classesCustomer 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 classesprivate class myClass{…}There is no private classes in AS3.
Private constructorsSupported. 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 namesA 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 packageClasses and interfacesClasses, 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/adynamic 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 closuresn/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 classessupportedn/a
Function overridingsupportedSupported. You must use the override qualifier
Function overloadingsupportedNot supported.
Interfacesclass A implements B{…}
interfaces can contain method declarations and final variables.
class A implements B{…}
interfaces can contain only function declarations.
Exception handlingKeywords: 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 expressionsSupportedSupported
与这篇可能估计大概好像是类似的还有:
Tags: ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   可以直接留言
网址   电邮   [注册]