Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- static class Report {
- String id;
- ReportData data;
- }
- static abstract class ReportData {
- String id;
- }
- static class ReportDataTypeA extends ReportData {
- String section1;
- String section2;
- }
- static class ReportDataTypeB extends ReportData {
- String header;
- String section1a;
- String section1b;
- }
- static ReportData createDataByYear(int year) {
- if (year == 2021) {
- ReportDataTypeA reportData = new ReportDataTypeA();
- reportData.id = "1";
- reportData.section1 = "test1";
- return reportData;
- } else if (year == 2022) {
- ReportDataTypeB reportData = new ReportDataTypeB();
- reportData.id = "2";
- reportData.header = "header";
- return reportData;
- }
- return null;
- }
- public static void main(String... args) {
- Report report = new Report();
- report.data = createDataByYear(2021);
- System.out.println(report.data.id);
- report.data = createDataByYear(2022);
- System.out.println(report.data.id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement