-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car.java
62 lines (50 loc) · 1.31 KB
/
Car.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.rajpro;
import java.util.ArrayList;
import java.util.function.Supplier;
public class Car {
public static Car create(final Supplier<Car> supplier) {
return supplier.get();
}
public static void collide(final Car car) {
System.out.println("Collided " + car.toString());
}
public void follow(final Car another) {
System.out.println("Following the " + another.toString());
}
public void repair() {
System.out.println("Repaired " + this.toString());
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Car [name=" + name + "]";
}
public ArrayList<Car> getCars(){
ArrayList<Car> al=new ArrayList<Car>();
Car c=new Car();
c.setName("Maruti");
al.add(c);
Car c1=new Car();
c1.setName("hundai");
al.add(c1);
return al;
}
public static void main(String[] args) {
Car c=Car.create(Car::new);
System.out.println("called repair");
c.repair();
System.out.println("--------------------");
System.out.println("static method call print the cars");
ArrayList<Car> cars = c.getCars();
cars.forEach(Car::collide);
System.out.println("Called non-static method print the cars");
final Car police = Car.create( Car::new );
cars.forEach( police::follow );
}
}