Created
October 1, 2019 13:50
-
-
Save SakaDream/bca1805a306a566e342b9cdf8be2d6ed to your computer and use it in GitHub Desktop.
Anonymous Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Age { | |
public void checkAge(); | |
public Student[] getStudents(); | |
} | |
class Student { | |
private int id; | |
private String name; | |
private int age; | |
public int getId() { return this.id; } | |
public String getName() { return this.name; } | |
public int getAge() { return this.age; } | |
public void setId(int id) { this.id = id; } | |
public void setName(String name) { this.name = name; } | |
public void setAge(int age) { this.age = age; } | |
@Override public String toString() { return name + " " + age + " " + id; } | |
} | |
abstract class Students implements Age { | |
private Student[] students; | |
public Students(Student[] students) { this.students = students; } | |
@Override public Student[] getStudents() { return this.students; } | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Student stu = new Student(); | |
stu.setId(1); | |
stu.setName("John"); | |
stu.setAge(17); | |
Student[] stus = new Student[30]; | |
for(int i = 0; i < stus.length; i++) { | |
stus[i] = stu; | |
} | |
Age students = new Students(stus) { | |
@Override | |
public void checkAge() { | |
for(Student student : this.getStudents()) { | |
if (student.getAge() < 18) { | |
System.out.println(student.toString()); | |
} | |
} | |
} | |
}; | |
students.checkAge(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment