收藏 分销(赏)

Java与ActionScript3语法比较.doc

上传人:仙人****88 文档编号:11890795 上传时间:2025-08-18 格式:DOC 页数:9 大小:110KB 下载积分:10 金币
下载 相关 举报
Java与ActionScript3语法比较.doc_第1页
第1页 / 共9页
Java与ActionScript3语法比较.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
Java 5与ActionScript3、JavaScript 语法比较 语言结构/语法 Java 5.0 ActionScript 3.0 类包 .jar .swc 继承 class Employee extends Person{…} class Employee extends Person{…} 变量声明及初始化 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; 未声明的变量 n/a var myVar:*; 变量作用域 声明在花括号内的,作用域也在括号内。 声明在函数里的,作用域即在函数里。 成员变量:声明在类里。 没有全局变量。 没有花括号作用域,最小的作用域范围是方法。 声明在函数里的,作用域即在函数里。 成员变量:声明在类里。 如果一个变量的声明不在任何类或方法里,那么它就是全局变量。 字符串 不可变类 不可变类 分号终结符 必须的 如果一条语句一行的话,你可以省略分号。 严格等于号 n/a === 严格不等号:!== 常量符号 关键字 final final int STATE= 1; 关键字const const STATE:int = 1; 类型检查 静态的 (在编译时检查) 动态的 (在运行时检查) 和静态的(也叫‘strict mode’, 在 Flex Builder中是默认的) 类型检查符 instanceof is – 检查数据类型, 例如 if (myVar is String){…} as操作符 n/a 很像is操作符, 但是返回值不是布尔型而是表达式: var orderId:String=”123”; var orderIdN:Number=orderId as Number; trace(orderIdN);//prints 123 原始类型 byte, int, long, float, double,short, boolean, char 所有的原始类型在ActionScript中都是对象。 Boolean, int, uint, Number, String 下面两行是相同的 var age:int = 25; var age:int = new int(25); 复杂类型 n/a Array, Date, Error, Function, RegExp, XML, and XMLList 数组的声明与实例化 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也有以命名的方式代替元素序号的数组,类似Hashtable。 最基础的对象 Object Object 类型转换语法: 将Object类转为Person Person p=(Person) myObject; var p:Person= Person(myObject); 或者 var p:Person= myObject as Person; 向上声明 class Xyz extends Abc{} Abc myObj = new Xyz(); class Xyz extends Abc{} var myObj:Abc=new Xyz(); 包 package com.xyz; class myClass {…} package com.xyz{ class myClass{…} } ActionScript的包不仅能包含类,也能包含独立的方法。 类访问级别 public, private, protected 如果没有说明,该类就是能在包内访问。 public, private, protected 如果没有说明,类就是internal访问级的(类似Java中的包访问级)。 常规的访问级别:命名空间 n/a 类似XML的命名空间。 namespace abc; abc function myCalc(){} 或 abc::myCalc(){} use namespace abc ; 控制台打印 System.out.println(); // 只在debug模式下有效 trace(); Imports导入 import com.abc.*; import com.abc.MyClass; import com.abc.*; import com.abc.MyClass; 无顺序的键值对 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 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 提升 n/a 编译器会将方法中所有变量声明提到顶端,所以你可以用一个变量名甚至在它声明之前。 实例化 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 class myClass{…} AS3中没有私有类 私有的构造函数 支持 不支持 类和文件名 一个文件可以有多个类声明,但是只能有一个是public,并且这个文件名必须和这个类名一致。 一个文件可以有多个类声明 包里能放什么 类和接口 类, 接口, 变量, 方法, 命名空间, 和可执行的声明 动态类 (定义一个可以在运行时动态添加修改属性和方法的类). n/a dynamic class Person { var name:String; } //动态的添加变量和方法 Person p= new Person(); p.name=”Joe”; p.age=25; p.printMe = function () { trace (p.name, p.age); } p.printMe(); // Joe 25 方法 closure n/a. myButton.addEventListener(“click”, myMethod); 抽象类 支持 n/a 方法重载 支持 支持. 必须使用override声明 方法过载 支持 不支持 接口 class A implements B{…} 接口可以包括方法的声明和静态变量。 class A implements B{…} 接口只能存放方法的声明。 异常处理 关键字: try, catch, throw, finally, throws 未捕捉的异常会向上传递到调用它的方法。 关键字: try, catch, throw, finally 方法不必声明异常。 不仅可以抛出Error对象,也能抛出数字: throw 25.3; 一旦有未捕捉的异常Flash Player立即停止脚本执行。 正则表达式 支持 支持 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 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 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
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服