继承嵌套子类实例化声明Java
来自JavaScript背景,我发现以下代码有点过于健壮,包含多条语句;我想知道我如何简单地编写代码,并在一条语句中完成所有工作
Student是超类,Friend和Schedule是聚合到超类ArrayList公共成员中的子类(这些不是嵌套类)。以下是我目前的代码:
public static void main(String[] args) {
// Subclass1 instantiation and declaration
Friend friend_of_andy_1 = new Friend(1002, "Bob");
ArrayList<Friend> andys_friends = new ArrayList<>();
andys_friends.add(friend_of_andy_1); // superclass member
// Subclass1 instantiation and declaration
Schedule schedule_of_andy_1 = new Schedule(1, "Yoga practice");
ArrayList<Schedule> andys_schedule = new ArrayList<>();
andys_schedule.add(schedule_of_andy_1); // superclass member
// Superclass instantiation declaration with subclass objects in constructor argument
Student student_andy = new Student(1001, "Andy", andys_friends, andys_schedule);
}
我想知道我是否可以这样做,在一条语句中声明/实例化超类和子类;这可能吗
public static void main(String[] args) {
Student student_andy = new Student(
1001,
"Andy",
// instantiation of subclass
ArrayList<Friend>[
Friend(
{
1002,
"Bob"
}
)
],
ArrayList<Schedule>[
Schedule(
{
1,
"Yoga practice"
}
)
]
)
};
共 (0) 个答案