Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Apache Groovy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Traits=== According to Groovy's documentation, "[[Trait (computer programming)|Traits]] are a structural construct of the language that allows: composition of behaviors, runtime implementation of interfaces, behavior overriding, and compatibility with static type checking/compilation." Traits can be seen as [[interface (object-oriented programming)|interface]]s carrying both default implementations and state. A trait is defined using the trait keyword: <syntaxhighlight lang="groovy"> trait FlyingAbility { /* declaration of a trait */ String fly() { "I'm flying!" } /* declaration of a method inside a trait */ } </syntaxhighlight> Then, it can be used like a normal interface using the keyword <code>implements</code>: <syntaxhighlight lang="groovy"> class Bird implements FlyingAbility {} /* Adds the trait FlyingAbility to the Bird class capabilities */ def bird = new Bird() /* instantiate a new Bird */ assert bird.fly() == "I'm flying!" /* the Bird class automatically gets the behavior of the FlyingAbility trait */ </syntaxhighlight> Traits allow a wide range of abilities, from simple composition to testing.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)