积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承
2007-11-14 08:49
691 查看
[索引页]
[源码下载]
[align=center]积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承[/align]
作者:webabcd
介绍
Flash ActionScript 3.0 是一种面向对象的语言,包、类、包外类、命名空间、属性、方法、接口和继承
示例
FunctionTest.as
package actionScript
{
import flash.display.Sprite;
public class FunctionTest extends Sprite
{
public function FunctionTest()
{
}
// 减法
public function Subtract(a:int, b:int):int
{
// 参数总数
trace(arguments.length);
// output: 2
// 第一个参数
trace(arguments[0]);
// output: “参数 a 的值”
// 第二个参数
trace(arguments[1]);
// output: “参数 b 的值”
// 返回a - b
return a - b;
}
// 加法(args - 任意多参数)
public function Add(s:String, args):String
{
var i:int = 0;
// 枚举出 args 中的所有参数
for each(var v in args)
{
i += v;
}
return s + ": " + i;
}
}
}
PropertyTest.as
package actionScript
{
import flash.display.Sprite;
public class PropertyTest extends Sprite
{
// 属性
public var nickname:String;
public var age:int;
private var _salary:int;
public function PropertyTest()
{
}
// getter方法
public function get salary():int
{
return this._salary;
}
// setter方法
public function set salary(s:int):void
{
this._salary = s;
}
}
}
StaticTest.as
package actionScript
{
import flash.display.Sprite;
public class StaticTest extends Sprite
{
// 静态属性
public static const nickname:String = "webabcd";
public static var age:int;
public function StaticTest()
{
}
// 静态方法
public static function hello(s:String):String
{
return "hello: " + s;
}
}
}
ParentTest.as
package actionScript
{
import flash.display.Sprite;
public class ParentTest extends Sprite
{
public function ParentTest()
{
}
// ParentTest为基类,其内定义了一个名为hello()的方法
public function hello(s:String):String
{
return "hello: " + s;
}
}
}
ChildTest.as
package actionScript
{
import actionScript.ParentTest;
// ChildTest类继承自ParentTest类
// final代表禁止继承
public final class ChildTest extends ParentTest
{
public function ChildTest()
{
}
// 重写基类(ParentTest)中的hello()方法
public override function hello(s:String):String
{
// super为对基类的引用
return "基类的hello()方法 - " + super.hello(s) + ";子类重写后的hello()方法 - 您好: " + s;
}
}
}
china.as
package actionScript
{
// 定义一个名为china的命名空间
// 注:actionScript目录下必须要有名为china.as的文件
public namespace china;
}
usa.as
package actionScript
{
// 定义一个名为usa的命名空间
// 注:actionScript目录下必须要有名为usa.as的文件
public namespace usa;
}
NamespaceTest.as
package actionScript
{
import flash.display.Sprite;
// 使用命名控件
use namespace china;
use namespace usa;
public class NamespaceTest extends Sprite
{
public function NamespaceTest()
{
}
// china命名空间的hello()方法
china function hello(s:String):String
{
return "您好: " + s;
}
// usa命名空间的hello()方法
usa function hello(s:String):String
{
return "hello: " + s;
}
}
}
InterfaceTest.as
package actionScript
{
// 定义一个接口,该接口有一个方法
public interface InterfaceTest
{
function writeLog():String;
}
}
InterfaceTestA.as
package actionScript
{
// 实现InterfaceTest接口
// 在同一个包中,所以不需要import InterfaceTest
public class InterfaceTestA implements InterfaceTest
{
// 实现InterfaceTest接口的writeLog()方法
public function writeLog():String
{
return "记录日志到SQL Server数据库";
}
}
}
InterfaceTestB.as
package actionScript
{
// 实现InterfaceTest接口
// 在同一个包中,所以不需要import InterfaceTest
public class InterfaceTestB implements InterfaceTest
{
// 实现InterfaceTest接口的writeLog()方法
public function writeLog():String
{
return "记录日志到XML文件";
}
}
}
OO.as
package
{
import flash.display.Sprite;
// 导入actionScript目录下的所有包
import actionScript.*;
public class OO extends Sprite
{
public function OO()
{
// internal - 包内访问(默认)
// public - 完全公开
// private - 仅当前类可访问
// protected - 子类可访问
// 用函数表达式定义函数
var hello:Function = function(s:String):String
{
return "hello: " + s;
}
trace(hello("webabcd"));
// output: hello: webabcd
// 方法
showFunction();
// 属性、getter方法和setter方法
showProperty();
// 静态属性和静态方法
showStatic();
// 包外类
showPackageOut();
// 命名空间
showNamespace();
// 继承、基类和子类
showInherit();
// 接口
showInterface();
}
// 方法
function showFunction():void
{
var ft:FunctionTest = new FunctionTest();
trace(ft.Subtract(300, 100));
// output: 200
trace(ft.Add("webabcd", 1, 2, 3, 4, 5));
// output: webabcd: 15
}
// 属性、getter方法和setter方法
function showProperty():void
{
var pt:PropertyTest = new PropertyTest();
pt.nickname = "webabcd";
pt.age = 27;
pt.salary = 1000;
trace(pt.nickname);
// output: webabcd
trace(pt.age);
// output: 27
trace(pt.salary);
// output: 1000
}
// 静态属性和静态方法
function showStatic():void
{
trace(StaticTest.nickname);
// output: webabcd
StaticTest.age = 27;
trace(StaticTest.age);
// output: 27
trace(StaticTest.hello("webabcd"));
// output: hello: webabcd
}
// 包外类
function showPackageOut()
{
var po:PackageOut = new PackageOut();
trace(po.hello("webabcd"));
// output: hello: webabcd
}
// 命名空间
function showNamespace()
{
// 使用命名空间
// use namespace 不受上下文的影响,编译时会被自动地提到前端
use namespace china;
var n:NamespaceTest = new NamespaceTest();
trace(n.hello("webabcd"));
// output: 您好: webabcd
// 使用命名空间名称限定符
trace(n.usa::hello("webabcd"));
// output: hello: webabcd
}
// 继承、基类和子类
function showInherit()
{
var ct:ChildTest = new ChildTest();
trace(ct.hello("webabcd"));
// output: 基类的hello()方法 - hello: webabcd;子类重写后的hello()方法 - 您好: webabcd
trace(ct is ParentTest);
// output: true
}
// 接口
function showInterface()
{
var a:InterfaceTest = new InterfaceTestA();
trace(a.writeLog());
// output: 记录日志到SQL Server数据库
trace(a is InterfaceTest);
// output: true
var b:InterfaceTest = new InterfaceTestB();
trace(b.writeLog());
// output: 记录日志到XML文件
trace(b is InterfaceTest);
// output: true
}
}
}
// 包外类(只有当前类文件中的成员类可以访问)
class PackageOut
{
public function hello(s:String):String
{
return "hello: " + s;
}
}
OK
[源码下载]
[源码下载]
[align=center]积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承[/align]
作者:webabcd
介绍
Flash ActionScript 3.0 是一种面向对象的语言,包、类、包外类、命名空间、属性、方法、接口和继承
示例
FunctionTest.as
package actionScript
{
import flash.display.Sprite;
public class FunctionTest extends Sprite
{
public function FunctionTest()
{
}
// 减法
public function Subtract(a:int, b:int):int
{
// 参数总数
trace(arguments.length);
// output: 2
// 第一个参数
trace(arguments[0]);
// output: “参数 a 的值”
// 第二个参数
trace(arguments[1]);
// output: “参数 b 的值”
// 返回a - b
return a - b;
}
// 加法(args - 任意多参数)
public function Add(s:String, args):String
{
var i:int = 0;
// 枚举出 args 中的所有参数
for each(var v in args)
{
i += v;
}
return s + ": " + i;
}
}
}
PropertyTest.as
package actionScript
{
import flash.display.Sprite;
public class PropertyTest extends Sprite
{
// 属性
public var nickname:String;
public var age:int;
private var _salary:int;
public function PropertyTest()
{
}
// getter方法
public function get salary():int
{
return this._salary;
}
// setter方法
public function set salary(s:int):void
{
this._salary = s;
}
}
}
StaticTest.as
package actionScript
{
import flash.display.Sprite;
public class StaticTest extends Sprite
{
// 静态属性
public static const nickname:String = "webabcd";
public static var age:int;
public function StaticTest()
{
}
// 静态方法
public static function hello(s:String):String
{
return "hello: " + s;
}
}
}
ParentTest.as
package actionScript
{
import flash.display.Sprite;
public class ParentTest extends Sprite
{
public function ParentTest()
{
}
// ParentTest为基类,其内定义了一个名为hello()的方法
public function hello(s:String):String
{
return "hello: " + s;
}
}
}
ChildTest.as
package actionScript
{
import actionScript.ParentTest;
// ChildTest类继承自ParentTest类
// final代表禁止继承
public final class ChildTest extends ParentTest
{
public function ChildTest()
{
}
// 重写基类(ParentTest)中的hello()方法
public override function hello(s:String):String
{
// super为对基类的引用
return "基类的hello()方法 - " + super.hello(s) + ";子类重写后的hello()方法 - 您好: " + s;
}
}
}
china.as
package actionScript
{
// 定义一个名为china的命名空间
// 注:actionScript目录下必须要有名为china.as的文件
public namespace china;
}
usa.as
package actionScript
{
// 定义一个名为usa的命名空间
// 注:actionScript目录下必须要有名为usa.as的文件
public namespace usa;
}
NamespaceTest.as
package actionScript
{
import flash.display.Sprite;
// 使用命名控件
use namespace china;
use namespace usa;
public class NamespaceTest extends Sprite
{
public function NamespaceTest()
{
}
// china命名空间的hello()方法
china function hello(s:String):String
{
return "您好: " + s;
}
// usa命名空间的hello()方法
usa function hello(s:String):String
{
return "hello: " + s;
}
}
}
InterfaceTest.as
package actionScript
{
// 定义一个接口,该接口有一个方法
public interface InterfaceTest
{
function writeLog():String;
}
}
InterfaceTestA.as
package actionScript
{
// 实现InterfaceTest接口
// 在同一个包中,所以不需要import InterfaceTest
public class InterfaceTestA implements InterfaceTest
{
// 实现InterfaceTest接口的writeLog()方法
public function writeLog():String
{
return "记录日志到SQL Server数据库";
}
}
}
InterfaceTestB.as
package actionScript
{
// 实现InterfaceTest接口
// 在同一个包中,所以不需要import InterfaceTest
public class InterfaceTestB implements InterfaceTest
{
// 实现InterfaceTest接口的writeLog()方法
public function writeLog():String
{
return "记录日志到XML文件";
}
}
}
OO.as
package
{
import flash.display.Sprite;
// 导入actionScript目录下的所有包
import actionScript.*;
public class OO extends Sprite
{
public function OO()
{
// internal - 包内访问(默认)
// public - 完全公开
// private - 仅当前类可访问
// protected - 子类可访问
// 用函数表达式定义函数
var hello:Function = function(s:String):String
{
return "hello: " + s;
}
trace(hello("webabcd"));
// output: hello: webabcd
// 方法
showFunction();
// 属性、getter方法和setter方法
showProperty();
// 静态属性和静态方法
showStatic();
// 包外类
showPackageOut();
// 命名空间
showNamespace();
// 继承、基类和子类
showInherit();
// 接口
showInterface();
}
// 方法
function showFunction():void
{
var ft:FunctionTest = new FunctionTest();
trace(ft.Subtract(300, 100));
// output: 200
trace(ft.Add("webabcd", 1, 2, 3, 4, 5));
// output: webabcd: 15
}
// 属性、getter方法和setter方法
function showProperty():void
{
var pt:PropertyTest = new PropertyTest();
pt.nickname = "webabcd";
pt.age = 27;
pt.salary = 1000;
trace(pt.nickname);
// output: webabcd
trace(pt.age);
// output: 27
trace(pt.salary);
// output: 1000
}
// 静态属性和静态方法
function showStatic():void
{
trace(StaticTest.nickname);
// output: webabcd
StaticTest.age = 27;
trace(StaticTest.age);
// output: 27
trace(StaticTest.hello("webabcd"));
// output: hello: webabcd
}
// 包外类
function showPackageOut()
{
var po:PackageOut = new PackageOut();
trace(po.hello("webabcd"));
// output: hello: webabcd
}
// 命名空间
function showNamespace()
{
// 使用命名空间
// use namespace 不受上下文的影响,编译时会被自动地提到前端
use namespace china;
var n:NamespaceTest = new NamespaceTest();
trace(n.hello("webabcd"));
// output: 您好: webabcd
// 使用命名空间名称限定符
trace(n.usa::hello("webabcd"));
// output: hello: webabcd
}
// 继承、基类和子类
function showInherit()
{
var ct:ChildTest = new ChildTest();
trace(ct.hello("webabcd"));
// output: 基类的hello()方法 - hello: webabcd;子类重写后的hello()方法 - 您好: webabcd
trace(ct is ParentTest);
// output: true
}
// 接口
function showInterface()
{
var a:InterfaceTest = new InterfaceTestA();
trace(a.writeLog());
// output: 记录日志到SQL Server数据库
trace(a is InterfaceTest);
// output: true
var b:InterfaceTest = new InterfaceTestB();
trace(b.writeLog());
// output: 记录日志到XML文件
trace(b is InterfaceTest);
// output: true
}
}
}
// 包外类(只有当前类文件中的成员类可以访问)
class PackageOut
{
public function hello(s:String):String
{
return "hello: " + s;
}
}
OK
[源码下载]
相关文章推荐
- 积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承
- 积少成多Flash ActionScript 3.0(2) - 基础之包、类、包外类、命名空间、属性、方法、接口和继承
- 积少成多Flash ActionScript 3.0(2) - 基础之包、类、包外类、命名空间、属性、方法、接口和继承
- C#基础知识复习1代码规范-执行流程(c#)-面向对象-引用命名空间-封装-继承-访问修饰符-虚方法-静态成员-多态-抽象类等
- C#学习笔记(五)中级 方法的重载,参数,继承和多态,异常处理,命名空间,接口,泛型
- 2018/01/08JAVA 基础 / 接口与继承:调用父类/子类的类方法、对象方法,访问父类的类属性、对象属性的方式汇总
- 2018/01/08JAVA 基础 / 接口与继承/Java关键词:变量引用 super 调用父类对象方法/构造方法【或者访问父类对象属性】的方式归纳
- 积少成多Flash(1) - ActionScript 3.0 基础之数据类型、操作符和流程控制语句
- 新瓶旧酒ASP.NET AJAX(2) - 客户端脚本编程(命名空间、类、成员、接口、继承、枚举)
- 积少成多Flash(5) - ActionScript 3.0 实例之闹钟(自定义事件, 画图, 动画)
- c#学习摘录:类\继承\接口\字段、方法、属性\浅深复制(1)
- IDL中有继承关系的接口不应有重复的属性和方法
- Spring(六)属性依赖注入(构造方法,setter方法,p命名空间,spel表达式,集合注入)
- 积少成多Flash ActionScript 3.0(3) - 基础之以文本形式、XML形式和JSON形式与ASP.NET通信
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
- C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)
- 面向对象基础(继承类,抽象方法,接口,委托和事件)
- Java中类、接口、变量、方法、属性、常量的命名规则
- 复习java基础------->继承关系中的方法覆盖和属性隐藏
- 新瓶旧酒ASP.NET AJAX(2) - 客户端脚本编程(命名空间、类、成员、接口、继承、枚举)