• Groovy 类名称赋值为变量使用(newInstance & new)


    类创建实例一般方式

    http://groovy-lang.org/objectorientation.html#_class

    class Person {                       
    
        String name                      
        Integer age
    
        def increaseAge(Integer years) { 
            this.age += years
        }
    }


    def p = new Person()

    类作为参数传递

    有些场景下,需要将类作为变量传递

    http://groovy-lang.org/objectorientation.html#_class

    class Runner {
        static <T> T run(Class<T> taskClass) {
            def tasks = taskClass.newInstance()                                         
            def params = [jdk:6, windows: false]                                        
            tasks.class.declaredMethods.each { m ->                                     
                if (Modifier.isPublic(m.modifiers) && m.parameterTypes.length == 0) {   
                    def onlyIf = m.getAnnotation(OnlyIf)                                
                    if (onlyIf) {
                        Closure cl = onlyIf.value().newInstance(tasks,tasks)            
                        cl.delegate = params                                            
                        if (cl()) {                                                     
                            m.invoke(tasks)                                             
                        }
                    } else {
                        m.invoke(tasks)                                                 
                    }
                }
            }
            tasks                                                                       
        }
    }



    // create a new instance of the class passed as an argument (the task class)

    DEMO2 抽象工厂

    http://groovy-lang.org/design-patterns.html#_example

    def guessFactory = [messages: GuessGameMessages, control: GuessGameControl, converter: GuessGameInputConverter]
    def twoupFactory = [messages: TwoupMessages, control: TwoupControl, converter: TwoupInputConverter]
    
    class GameFactory {
        def static factory
        def static getMessages() { return factory.messages.newInstance() }
        def static getControl() { return factory.control.newInstance() }
        def static getConverter() { return factory.converter.newInstance() }
    }
    GameFactory.factory = twoupFactory
    def messages = GameFactory.messages
    def control = GameFactory.control
    def converter = GameFactory.converter
    println messages.welcome
    def reader = new BufferedReader(new InputStreamReader(System.in))
    while (control.moreTurns()) {
        def input = reader.readLine().trim()
        control.play(converter.convert(input))
    }
    println messages.done

    DEMO3 -- 变量

    class Greet {
        def salute() { println "Hello !" }
    }
     
    g = new Greet()  // create object
    g.salute()


    def  class_var = Greet
    // h = new class_var()
    h = class_var.newInstance()
    h.salute()

  • 相关阅读:
    python之bytes和string(转)
    http post请求传文件报错 invalid character '' in numeric literal
    正则表达式匹配从指定字符开始到指定字符结束的字符串
    为什么提倡会议上要多提问?
    4K 显示器突然变模糊了怎么调回来
    快速将 gif 图转成 jpg 图片
    <一>window安装git
    <二>使用git将本地项目上传到gitee
    <一>window安装rabbitmq
    <一>redis安装
  • 原文地址:https://www.cnblogs.com/lightsong/p/8655019.html
Copyright © 2020-2023  润新知