You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Groovy构造方法中初始化默认值?附代码示例

How to Initialize Default Values in Groovy Constructors (Using Your PSLConfig Example)

First, let's start with the PSLConfig code you provided as a reference:

@groovy.transform.InheritConstructors
class PSLConfig {
    // ConfigManager related member
    ConfigManager configManager
    // Model description member variable
    String modelDescription
    // Constructor accepting ConfigBundle parameter
    PSLConfig(ConfigBundle configBundle) {
        this.configManager = new ConfigManager(configBundle)
    }
}

Groovy offers several flexible ways to add default value initialization to constructors—here are the most common approaches tailored to your class:

1. Direct Default Assignment in Member Variable Definitions

This is the simplest method for static default values. Just assign the default directly when declaring the variable:

@groovy.transform.InheritConstructors
class PSLConfig {
    ConfigManager configManager
    // Set a default string for modelDescription
    String modelDescription = "Default PSL Model Description"
    
    PSLConfig(ConfigBundle configBundle) {
        this.configManager = new ConfigManager(configBundle)
    }
}

Now, any instance of PSLConfig (including those created via the inherited no-arg constructor from @InheritConstructors) will have modelDescription set to the default value unless you explicitly override it.

2. Default Parameters in Custom Constructors

If you need dynamic default logic or want to make constructor parameters optional, add default values directly to the constructor parameters:

@groovy.transform.InheritConstructors
class PSLConfig {
    ConfigManager configManager
    String modelDescription
    
    PSLConfig(ConfigBundle configBundle = null, String defaultModelDesc = "Default Model") {
        // Initialize configManager with a default instance if no bundle is provided
        this.configManager = configBundle ? new ConfigManager(configBundle) : new ConfigManager()
        this.modelDescription = defaultModelDesc
    }
}

This lets you call the constructor in multiple ways:

  • new PSLConfig() → Uses all default values
  • new PSLConfig(myCustomBundle) → Uses the provided bundle, keeps the default model description
  • new PSLConfig(myCustomBundle, "Custom Model") → Overrides both values

3. Use @TupleConstructor for Boilerplate-Free Constructors

If you want to avoid writing custom constructors entirely, replace or combine @InheritConstructors with @TupleConstructor (with defaults = true to enable default values):

@groovy.transform.TupleConstructor(defaults = true)
class PSLConfig {
    // Assign default instances/values directly to members
    ConfigManager configManager = new ConfigManager()
    String modelDescription = "Default PSL Model"
}

Groovy automatically generates a constructor that supports optional parameters, so you can:

  • Create an instance with all defaults: new PSLConfig()
  • Override only configManager: new PSLConfig(myCustomManager)
  • Override both members: new PSLConfig(myCustomManager, "Custom Description")

4. Initialization Blocks for Post-Constructor Defaults

If you're using @InheritConstructors and want to ensure defaults are set across all inherited constructors, use a Groovy initialization block (runs after any constructor executes):

@groovy.transform.InheritConstructors
class PSLConfig {
    ConfigManager configManager
    String modelDescription
    
    // This block runs after every constructor
    {
        // Use Elvis operator to set default if value is null
        modelDescription = modelDescription ?: "Fallback Default Model"
        configManager = configManager ?: new ConfigManager()
    }
    
    PSLConfig(ConfigBundle configBundle) {
        this.configManager = new ConfigManager(configBundle)
    }
}

This guarantees that even if someone uses the inherited no-arg constructor, configManager and modelDescription will never be null—they'll fall back to your defaults.


内容的提问来源于stack exchange,提问作者firefly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:04:21