Groovy Beans

class Car {
 int numberOfDoors
 String model
 String brand
 boolean automatic
 double price
  
 String toString() {
     "[Car details => brand: '${brand}', model: '${model}', #doors: '${numberOfDoors}', automatic: '${automatic}', price: '${price}']"
 }
}

Car ford = new Car(brand: 'Ford', model: 'Focus', numberOfDoors: 4, automatic: false, price: 24995)
Car toyota =  new Car(brand: 'Toyota', model: 'Verso')
toyota.automatic = true  // Yes, this invokes setAutomatic.
toyota.setPrice(28919)  // Normal set invocation.
toyota.setNumberOfDoors 5



class Car {
 int numberOfDoors
 String model
 final String brand  // Readonly, public getter is generated, no setter.
 
//Caught: groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: brand for class: Car

 boolean automatic
 double price
  
 // Protected setter, public getter is still generated.
 protected void setModel(model) {
     this.model = modelName
    }
  
 String toString() {
     "[Car details => brand: '${brand}', model: '${model}', #doors: '${numberOfDoors}', automatic: '${automatic}', price: '${price}']"
 }
}




Comments

Popular posts from this blog

Array and Object Creation - JavaScript

Groovy Tuples

அவன் ஏன் கூலியானான்?