In Java, a bean is a class that follows below
conventions.
a.
All properties must be private, operations on
properties are exposed via getter and setter method.
b.
Class
must have a public no-argument constructor
c.
Class must implement Serializable interface.
In Java, we can define an Employee class like below.
public class Employee { private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
We can rewrite the Employee class like below in Groovy.
class Employee { int id String firstName String lastName }
Groovy provide setter and getter methods by default.
Employee emp1 =
new Employee();
emp1.setId(1);
emp1.setFirstName("Krishna");
emp1.setLastName("Gurram");
Find the below working application.
HelloWorld.groovy
class Employee { int id String firstName String lastName } Employee emp1 = new Employee(); emp1.setId(1); emp1.setFirstName("Krishna"); emp1.setLastName("Gurram"); println emp1.getId() + "," + emp1.getFirstName() + "," + emp1.getLastName()
Output
1,Krishna,Gurram
No comments:
Post a Comment