美文网首页
面向对象.动态添加属性,方法

面向对象.动态添加属性,方法

作者: 龙剑灵 | 来源:发表于2020-03-19 00:06 被阅读0次
image.png

groovy中的接口

interface Action {
    void eat()
    void play()
}
trait DefaultAction {
    abstract void eat()
    void play() {
        println "i can play"
    }
}

接下来有类实现接口

class Student implements DefaultAction{
    String name
    Integer age

    @Override
    void eat() {

    }
}

//groovy默认都是public
//用.属性也是相当于用get

Student.metaClass.sex = 'male' //动态添加属性
def stu = new Student(name: "jimmy", age: 31)
println "name: ${stu.getName()}, age: ${stu.age}"
stu.play()
println stu.sex
println stu.cry() //不存在的方法 在运行时才会报错

运行结果如下

name: jimmy, age: 31
i can play
male
Caught: groovy.lang.MissingMethodException: No signature of method: com.cjt.groovy.objectorention.Student.cry() is applicable for argument types: () values: []
Possible solutions: any(), any(groovy.lang.Closure), eat(), play(), grep(), every()
groovy.lang.MissingMethodException: No signature of method: com.cjt.groovy.objectorention.Student.cry() is applicable for argument types: () values: []
Possible solutions: any(), any(groovy.lang.Closure), eat(), play(), grep(), every()
    at com.cjt.groovy.objectorention.studenttest.run(studenttest.groovy:10)

接下来,换个类看个例子(暂不实现接口)
重写 invokeMethod , methodMissing
在类实例中调用不存在的方法时会调用此方法,存在优先级别

class People {
    String name
    Integer age

    def increaseAge(Integer years) {
        this.name += years
    }
    
    //重写此方法, 若调用此类没有的方法,编译时不报错, 执行时会执行此方法
    def invokeMethod(String name, Object args)
    {
        return  "this method is ${name}, agrs: ${args}"
    }

    def methodMissing(String name, Object args)
    {
        return  "this method  ${name} is missing"
    }

}
//groovy默认都是public
//用.属性也是相当于用get
People.metaClass.sex = 'male' //动态添加属性
def people = new People(name: "jimmy", age: 31)
println "name: ${people.getName()}, age: ${people.age}"
people.play()
println people.cry() //不存在的方法 在运行时才会报错
println people.sex

println "------为类动态添加方法-----------"
People.metaClass.setUpperCase = { -> sex.toUpperCase() }
def p = new People(name: "吉米", age: 22)
println p.setUpperCase()

println "------为类动态添加静态方法-----------"
People.metaClass.static.createPeople = {
    String name, int age -> new People(name: name, age: age)
}
def p2 = People.createPeople("cjt", 25)
println  "name: " + p2.name +" , age: " + p2.age

结果如下

name: jimmy, age: 31
this method  cry is missing
male
-----------------
MALE
------为类动态添加静态方法-----------
name: cjt , age: 25

外部注入类方法,在整个应用中有效

image.png
/**
 * 模拟一个应用的管理类
 */
class ApplicationManager {

    static void init() {
        ExpandoMetaClass.enableGlobally()
        //为第三方类添加方法
        People.metaClass.static.createPeople = {
            String name, int age -> new People(name: name, age: age)
        }
    }
}
class PeopleManager {

    static People createPeople(String name, int age) {
        return People.createPeople(name, age)
    }
}
//外部注入类方法,在整个应用中有效
class Entry {
    static void main(def args) {
        println '应用程序正在启动...'
        //初始化
        ApplicationManager.init()
        println '应用程序初始化完成...'

        def p = PeopleManager.createPeople("吉米", 28)
        println "create people name: ${p.name}, age: ${p.age}"
    }
}

输出结果 :

应用程序正在启动...
应用程序初始化完成...
create people name: 吉米, age: 28

相关文章

  • 二十一:Python之动态添加属性方法

    一:动态添加属性方法 动态添加属性:类属性,成员属性 动态添加方法:成员方法,类方法,静态方法 动态添加对象方法需...

  • 面向对象.动态添加属性,方法

    groovy中的接口 接下来有类实现接口 //groovy默认都是public//用.属性也是相当于用get 运行...

  • react 对象动态添加属性 setState

    // js 对象动态添加 js对象动态添加 //总结,给对象动态添加变量属性的方法如下: //obj[变量]=变...

  • pythonx1_面向对象2

    面向对象 高级 === 动态添加属性python可以动态为对象绑定属性,但是不会影响到同类的其他对象。 cl...

  • iOS RunTime 理解

    可以遍历对象的属性 可以动态的添加、修改属性,动态添加、修改、替换方法,动态添加、修改、替换协议 可以动态创建类、...

  • 匿名函数,面向对象

    匿名函数和使用 类 &面向对象 给对象添加属性 ——init——()方法 魔法方法

  • 面向对象

    面向对象基础 目标 理解面向对象 类和对象 添加和获取对象属性 魔法方法 一. 理解面向对象 面向对象是一种抽象化...

  • runTime之--动态添加属性(给系统类对象)

    系统类对象动态添加属性 实现 就是添加关联 微云网盘动态添加属性demo

  • 8、面向对象

    分析面向对象 先分析类,分析静态的属性和动态的方法 创建对象:类名 对象名=new 类名(); 为对象的属性赋值:...

  • Python动态语言的体现

    Python作为一门动态语言 ,他的动态性体现在可以动态给对象添加属性 和方法。 1,通过TypeMethod方法...

网友评论

      本文标题:面向对象.动态添加属性,方法

      本文链接:https://www.haomeiwen.com/subject/ogxbdhtx.html