Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. class Person {
  2.     private String firstName;
  3.     private String secondName;
  4.     private String lastName;
  5.     private int age;
  6.     private String address;
  7.  
  8.     private Person(String firstName, String lastName) {
  9.         this.firstName = firstName;
  10.         this.lastName = lastName;
  11.     }
  12.  
  13.     public static Person of(String firstName, String lastName) {
  14.         return new Person(firstName, lastName);
  15.     }
  16.  
  17.     public Person secondName(String secondName) {
  18.         this.secondName = secondName;
  19.         return this;
  20.     }
  21.  
  22.     public Person age(int age) {
  23.         this.age = age;
  24.         return this;
  25.     }
  26.  
  27.     public Person address(String address) {
  28.         this.address = address;
  29.         return this;
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         return "Person{" +
  35.                 "firstName='" + firstName + '\'' +
  36.                 ", secondName='" + secondName + '\'' +
  37.                 ", lastName='" + lastName + '\'' +
  38.                 ", age=" + age +
  39.                 ", address='" + address + '\'' +
  40.                 '}';
  41.     }
  42. }
  43.  
  44. public class Main {
  45.     public static void main(String[] args) {
  46.         Person john = Person.of("John", "Smith").secondName("William").age(30);
  47.  
  48.         System.out.println(john);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement