In the first article, we had just the Employee class with getter/setter methods. We implemented it in idiomatic Java and then again in idiomatic Go. Now let's continue our employee example in Java and Go. First Java.
Java Employee
/java-examples/src/com/example/inheritance/Employee.java
package com.example.inheritance;
public class Employee {
protected String firstName;
protected String lastName;
protected int age;
protected boolean current;
public Employee(String firstName, String lastName, int age, boolean current) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.current = current;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public boolean isCurrent() {
return current;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setAge(int age) {
this.age = age;
}
public void setCurrent(boolean current) {
this.current = current;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + ", current=" + current + "]";
}
}
Go Employee
/examples/src/example/inheritance/employee.go
package inheritance
import "strconv"
type Employee struct {
firstName string
lastName string
age int
current bool
}
func NewEmployee(firstName string, lastName string, age int, current bool) *Employee {
return &Employee{firstName, lastName, age, current}
}
func (this *Employee) FirstName() string {
return this.firstName
}
func (this *Employee) LastName() string {
return this.lastName
}
func (this *Employee) Age() int {
return this.age
}
func (this *Employee) Current() bool {
return this.current
}
func (this *Employee) SetFirstName(name string) {
this.firstName = name
}
func (this *Employee) SetLastName(name string) {
this.lastName = name
}
func (this *Employee) SetAge(newAge int) {
this.age = newAge
}
func (this *Employee) SetCurrent(current bool) {
this.current = current
}
func (this *Employee) String() string {
return "Employee [firstName=" + this.firstName + ", lastName=" + this.lastName +
", age=" + strconv.Itoa(this.age) + ", current=" + strconv.FormatBool(this.current) + "]"
}
Java HourlyEmployee
/java-examples/src/com/example/inheritance/HourlyEmployee.java
package com.example.inheritance;
public class HourlyEmployee extends Employee {
private double hourlyRate;
public HourlyEmployee(String firstName, String lastName, int age,
boolean current, double commisionRate) {
super(firstName, lastName, age, current);
this.hourlyRate = commisionRate;
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
@Override
public String toString() {
return "HourlyEmployee [firstName=" + firstName + ", lastName="
+ lastName + ", age=" + age + ", current=" + current
+ ", hourlyRate=" + hourlyRate + "]";
}
}
Go HourlyEmployee
/examples/src/example/inheritance/hourly.go
package inheritance
import "strconv"
type HourlyEmployee struct {
Employee
hourlyRate float64
}
func NewHourlyEmployee(firstName string, lastName string, age int, current bool, hourlyRate float64) *HourlyEmployee {
newEmp := new(HourlyEmployee)
newEmp.hourlyRate = hourlyRate
newEmp.firstName = firstName
newEmp.lastName = lastName
newEmp.age = age
newEmp.current = current
return newEmp
}
func (this *HourlyEmployee) HourlyRate() float64 {
return this.hourlyRate
}
func (this *HourlyEmployee) SetHourlyRate(rate float64) {
this.hourlyRate = rate
}
func (this *HourlyEmployee) String() string {
return "HourlyEmployee [firstName=" + this.firstName + ", lastName=" + this.lastName +
", age=" + strconv.Itoa(this.age) + ", current=" + strconv.FormatBool(this.current) +
", hourlyRate=" + strconv.FormatFloat(float64(this.hourlyRate), 'f', 2, 32) + "]"
}
Java SalaryEmployee
/java-examples/src/com/example/inheritance/SalaryEmployee.java
package com.example.inheritance;
public class SalaryEmployee extends Employee {
private double commisionRate;
public SalaryEmployee(String firstName, String lastName, int age,
boolean current, double commisionRate) {
super(firstName, lastName, age, current);
this.commisionRate = commisionRate;
}
public double getCommisionRate() {
return commisionRate;
}
public void setCommisionRate(double commisionRate) {
this.commisionRate = commisionRate;
}
@Override
public String toString() {
return "SalaryEmployee [firstName=" + firstName + ", lastName="
+ lastName + ", age=" + age + ", current=" + current
+ ", commisionRate=" + commisionRate + "]";
}
}
Go SalaryEmployee
/examples/src/example/inheritance/salary.go
package inheritance
import "strconv"
type SalaryEmployee struct {
Employee
commisionRate float64
}
func NewSalaryEmployee(firstName string, lastName string, age int, current bool, commisionRate float64) *SalaryEmployee {
newEmp := new(SalaryEmployee)
newEmp.commisionRate = commisionRate
newEmp.firstName = firstName
newEmp.lastName = lastName
newEmp.age = age
newEmp.current = current
return newEmp
}
func (this *SalaryEmployee) CommisionRate() float64 {
return this.commisionRate
}
func (this *SalaryEmployee) SetCommisionRate(rate float64) {
this.commisionRate = rate
}
func (this *SalaryEmployee) String() string {
return "SalaryEmployee [firstName=" + this.firstName + ", lastName=" + this.lastName +
", age=" + strconv.Itoa(this.age) + ", current=" + strconv.FormatBool(this.current) +
", commisionRate=" + strconv.FormatFloat(float64(this.commisionRate), 'f', 2, 32) + "]"
}
Using Classes Java
/java-examples/src/com/example/UseInheritance.java
package com.example;
import com.example.inheritance.Employee;
import com.example.inheritance.HourlyEmployee;
import com.example.inheritance.SalaryEmployee;
public class UseInheritance {
public static void main(String[] args) {
Employee employee1 = new Employee("Rick", "Hightower", 32, true);
SalaryEmployee employee2 = new SalaryEmployee("Whitney", "Hightower",
17, true, 0.05);
HourlyEmployee employee3 = new HourlyEmployee("Bob", "Hightower", 27,
true, 25.0);
System.out.println(employee1);
System.out.println(employee2);
System.out.println(employee3);
employee1.setLastName("Smith");
employee1.setAge(45);
System.out.printf("Name = %s, %s | Age = %d \n",
employee1.getFirstName(), employee1.getLastName(),
employee1.getAge());
System.out.printf("Name = %s, %s | Age = %d \n",
employee2.getFirstName(), employee2.getLastName(),
employee2.getAge());
System.out.printf("Name = %s, %s | Age = %d \n",
employee3.getFirstName(), employee3.getLastName(),
employee3.getAge());
Object[] employees = new Object[] { employee1, employee2, employee3 };
for (Object employee : employees) {
System.out.println(employee);
}
}
}
Using Classes Go
/examples/src/example/inheritance/useInheritance.go
package main
import (
"example/inheritance"
"fmt"
)
func main() {
employee1 := inheritance.NewEmployee("Rick", "Hightower", 32, true)
employee2 := inheritance.NewSalaryEmployee("Whitney", "Hightower", 17, true, 0.05)
employee3 := inheritance.NewHourlyEmployee("Bob", "Hightower", 27, true, 25.0)
fmt.Println(employee1)
fmt.Println(employee2)
fmt.Println(employee3)
employee1.SetLastName("Smith")
employee1.SetAge(45)
fmt.Printf("Name = %s, %s | Age = %d \n", employee1.FirstName(), employee1.LastName(), employee1.Age())
fmt.Printf("Name = %s, %s | Age = %d \n", employee2.FirstName(), employee2.LastName(), employee2.Age())
fmt.Printf("Name = %s, %s | Age = %d \n", employee3.FirstName(), employee3.LastName(), employee3.Age())
employees := []interface{}{employee1, employee2, employee3}
for _, employee := range employees {
fmt.Println(employee)
}
}
The output is the same.
Employee [firstName=Rick, lastName=Hightower, age=32, current=true] SalaryEmployee [firstName=Whitney, lastName=Hightower, age=17, current=true, commisionRate=0.05] HourlyEmployee [firstName=Bob, lastName=Hightower, age=27, current=true, hourlyRate=25.00] Name = Rick, Smith | Age = 45 Name = Whitney, Hightower | Age = 17 Name = Bob, Hightower | Age = 27 Employee [firstName=Rick, lastName=Smith, age=45, current=true] SalaryEmployee [firstName=Whitney, lastName=Hightower, age=17, current=true, commisionRate=0.05] HourlyEmployee [firstName=Bob, lastName=Hightower, age=27, current=true, hourlyRate=25.00]
No comments:
Post a Comment