本帖最后由 雪原xy 于 2018-12-22 18:37 编辑
自己做了一个练习,有大神给我点评一下吗?[Actionscript3] 纯文本查看 复制代码 //定义一个复合类,Box:从中可以取出不同的笔;
package son{
public class Box {
var nam:String;
var _pencil:Pencil;
var _rubber:Rubber;
public var _pm:Memory;
public var _rm:Memory;
public function Box(na:String)
{
nam = na;
initparts();
}
//一个可以重载的方法;
public function magicpen(...mag)
{
if (mag.length == 1) {
if (mag[0] is String) {
write(mag[0]); }
if (mag[0] == 1) {
rubber(); } }
else {
trace("请输入正确的参数!谢谢!");
} }
private function initparts() {
_pencil=new Pencil();
_rubber=new Rubber();
_pm=new Memory();
_rm=new Memory(); }
private function write(e:String) {
_pencil.write(e);
_pm.memory.push(_pencil.writein);
_pm.t++; }
private function rubber()
{
if (_pm.memory.length == 1) {
trace(_pm.memory[0]);
_rm.t++; }
else {
_rubber.write(_pm.memory[_pm.memory.length-1]);
_rm.memory.push(_rubber.writein);
_pm.memory.pop();
_pm.t--;
_rm.t++; } } }}
//定义一个父类
Pen;package son{ class Pen {
var writein:String = "什么都没有!";
function write(e:String) {
writein = e; } }}
//定义一个存储类
Memorypackage son{ class Memory {
public var memory:Array = ["什么都没有"];
//用来存储写入的内容;
public var t:int = 0;//用来存储写入的次数;
}}
//定义一个子类
Rubber;package son{ class Rubber extends Pen {
override function write(e:String) {
writein = e;
trace("这次擦除的是:"+e); } }}
//文档类
package {
import flash.display.Sprite;
import son.Box;
public class doc extends Sprite {
var _box:Box = new Box("魔盒");
public function doc() {
init(); }
function init() {
_box.magicpen(12);
_box.magicpen("我");
_box.magicpen("是");
_box.magicpen("中");
_box.magicpen("国");
_box.magicpen("人");
_box.magicpen("?");
_box.magicpen(1);
trace("您共写入:"+_box._pm.t+"次,内容是:"+_box._pm.memory);
trace("您共擦除了:"+_box._rm.t+"次,内容是:"+_box._rm.memory);
} }}
|