标题: [原创]对于类不能存取一种替代方案...大师来看看能不能把他简化一下
sjmind
会员
Rank: 1



UID 1025
精华 0
积分 106
帖子 30
阅读权限 5
注册 2008-6-4
 
发表于 2008-7-19 20:43  资料  短消息  加为好友  添加 sjmind 为MSN好友 通过MSN和 sjmind 交谈
[原创]对于类不能存取一种替代方案...大师来看看能不能把他简化一下

写了2小时,基本没有大问题
把这里的类称为D类以和通常的类示以区别
D类实际上是一个特殊结构的Dictionary
必要的结构为


用我写的例子(在first.ks调用前调用):

var DClass=
%[
        Name:"DClass",
        Value:0        ,
        Array:[],
        methods:%[]
];
DClass.methods=%[
Init:function (dc,elm=%[])
{
        dc.Value=0;
        dc.Array=[];
},
Add:function (dc,elm=%[value:0])
{
        dc.Value++;
        dc.Array.add(elm.value);
}
];

然后是驱动AfterInit.tjs:


我给的样例的first.ks:


使用没有问题,因为实例化的D类把数据完整的保留了,而成员函数实际上是通过注册D类时的函数实现的。
因而不会出现null的问题。
只不过个人觉得调用成员函数的写法有点丑,大师如果能改进的话,还请赐教

[ 本帖最后由 sjmind 于 2008-7-19 20:50 编辑 ]

顶部
sjmind
会员
Rank: 1



UID 1025
精华 0
积分 106
帖子 30
阅读权限 5
注册 2008-6-4
 
发表于 2008-7-20 14:11  资料  短消息  加为好友  添加 sjmind 为MSN好友 通过MSN和 sjmind 交谈
稍微简化了一下:
first.ks:


RegClass.ks:

@iscript
KAGLoadScript("dc_ItemList.tjs");
RegClass.ItemList=ItemList;
@endscript
@return



@macro name=setupDClass
@eval exp=KAGLoadScript("DClassSys.tjs");
@endmacro

dc_ItemList.tjs:

var ItemList=
%[
        Name:"ItemList",
        item:[],
        count:0,
        needsort:true,
        methods:
        %[
                Init:function(dc)
                {
                        dc.item=[];
                        dc.count=0;
                },
                Add:function(dc,value)
                {
                        dc.item.add(value);
                        if (needsort)
                                dc.item.sort();
                        dc.count=dc.item.count;
                }
        ]
];

内核DClassSys.tjs:


[ 本帖最后由 sjmind 于 2008-7-20 14:43 编辑 ]

顶部
sjmind
会员
Rank: 1



UID 1025
精华 0
积分 106
帖子 30
阅读权限 5
注册 2008-6-4
 
发表于 2008-7-20 14:43  资料  短消息  加为好友  添加 sjmind 为MSN好友 通过MSN和 sjmind 交谈
这个做法可以携带类的继承特性:
以下是继承的写法:



顶部
 



清除 Cookies - 联系我们 - [Key Fans Club] the Council of Doujin-game Development and Promotion - Archiver - WAP
Powered by Discuz! 5.5.0 © 2001-2006 Comsenz Inc.
Processed in 0.018984 second(s), 7 queries , Gzip enabled
粤ICP备05060595号
[ 当前模板 - 天高云淡 v 设计制作 - 七星论坛 v 请使用1024*768或以上分辨率浏览本模板 ]
请勿删除或修改此处版权图示