分享
分销 收藏 举报 申诉 / 9
播放页_导航下方通栏广告

类型Java与ActionScript3语法比较.doc

  • 上传人:仙人****88
  • 文档编号:11890795
  • 上传时间:2025-08-18
  • 格式:DOC
  • 页数:9
  • 大小:110KB
  • 下载积分:10 金币
  • 播放页_非在线预览资源立即下载上方广告
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    Java ActionScript3 语法 比较
    资源描述:
    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
    展开阅读全文
    提示  咨信网温馨提示:
    1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
    2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
    3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
    4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
    5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
    6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

    开通VIP折扣优惠下载文档

    自信AI创作助手
    关于本文
    本文标题:Java与ActionScript3语法比较.doc
    链接地址:https://www.zixin.com.cn/doc/11890795.html
    页脚通栏广告

    Copyright ©2010-2026   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


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

    自信网络  |  ZixinNetwork