码迷,mamicode.com
首页 > 其他好文 > 详细

coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现

时间:2014-05-10 06:33:57      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   ext   

bubuko.com,布布扣
class Parent
    constructor: ->

    _privateAttr = privateAttr
    _privateFun = ()->
        console.log "Parent::privateFun log #{_privateAttr}"

    publicAttr: publicAttr
    publicFun: ()->
        _privateFun()
        console.log "Parent::publicFun log #{@publicAttr} #{_privateAttr}"

    @staticAttr: staticAttr
    @staticFun: ()->
        console.log "Parent::staticFun log #{@staticAttr}"


class Child extends Parent
    childPublicFun: ()->
        try
            _privateFun()#报错 TypeError:Object #<Parent> has no method _privateFun
        catch e
            console.log "_privateFun() is error"
        console.log "Child::childPublicFun"

p = new Parent

c = new Child

Parent.staticFun()
#Parent::staticFun log staticAttr
try
    p.staticFun()#报错 TypeError:Object #<Parent> has no method staticFun
catch e
    console.log "p.staticFun() is error"

p.publicFun()
#Parent::privateFun log privateAttr
#Parent::publicFun log publicAttr privateAttr

try
    p._privateFun()#报错 TypeError:Object #<Parent> has no method _privateFun
catch e
    console.log "p._privateFun() is error"


Child.staticFun()
#Parent::staticFun log staticAttr

c.publicFun()
#Parent::privateFun log privateAttr
#Parent::publicFun log publicAttr privateAttr

c.childPublicFun()
#_privateFun() is error
#Child::childPublicFun
bubuko.com,布布扣

 

coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现,布布扣,bubuko.com

coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现

标签:style   blog   class   code   java   ext   

原文地址:http://www.cnblogs.com/hbxeagle/p/3719300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!