-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
69 lines (57 loc) · 2 KB
/
Main.java
File metadata and controls
69 lines (57 loc) · 2 KB
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
63
64
65
66
67
68
69
package FieldReflection;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
printDeclaredFields(Movie.MovieStats.class);
printDeclaredFields(Category.class);
// System.out.println(Arrays.stream(Category.values()).collect(Collectors.toList()));
Movie movie = new Movie("A", 2, true, Category.ADVENTURE, 200);
getFieldValues(movie.getClass(), movie);
}
private static <T> void getFieldValues(Class<? extends T> clazz, T instance) throws IllegalAccessException {
for (Field field : clazz.getDeclaredFields()) {
System.out.println(String.format("Field name: %s, type: %s, isSynthetic: %s", field.getName(), field.getType(), field.isSynthetic()));
// Returns the value of the field represented by this Field, on the specified object.
System.out.println(String.format("Field value is: %s", field.get(instance)));
System.out.println();
}
}
private static void printDeclaredFields(Class<?> clazz) {
for (Field field : clazz.getDeclaredFields()) {
System.out.println(String.format("Field name: %s, type: %s, isSynthetic: %s", field.getName(), field.getType(), field.isSynthetic()));
System.out.println();
}
}
static class Movie extends Product {
static final double INIT_PRICE = 100;
private boolean isReleased;
private Category category;
private double actualPrice;
public Movie(String name, int year, boolean isReleased, Category category, double actualPrice) {
super(name, year);
this.isReleased = isReleased;
this.category = category;
this.actualPrice = actualPrice;
}
public class MovieStats {
private double timesWatched;
public MovieStats(double timesWatched) {
this.timesWatched = timesWatched;
}
}
}
static class Product {
protected String name;
protected int year;
protected double actualPrice;
public Product(String name, int year) {
this.name = name;
this.year = year;
}
}
public enum Category {
ADVENTURE,
ACTION,
COMEDY
}
}