Skip to content

Instantly share code, notes, and snippets.

@SakaDream SakaDream/Main.java
Created Oct 1, 2019

Embed
What would you like to do?
Anonymous Classes
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
You can’t perform that action at this time.