forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParcel10.java
More file actions
36 lines (32 loc) · 1010 Bytes
/
Parcel10.java
File metadata and controls
36 lines (32 loc) · 1010 Bytes
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
// innerclasses/Parcel10.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Using "instance initialization" to perform
// construction on an anonymous inner class
public class Parcel10 {
public static void main(String[] args) {
Parcel10 p = new Parcel10();
Destination d = p.destination("Tasmania", 101.395F);
}
public Destination
destination(final String dest, final float price) {
return new Destination() {
private int cost;
private String label = dest;
// Instance initialization for each object:
{
cost = Math.round(price);
if (cost > 100)
System.out.println("Over budget!");
}
@Override
public String readLabel() {
return label;
}
};
}
}
/* Output:
Over budget!
*/