首页 > 代码库 > coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现

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

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