欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

内部类如何实例化

程序员文章站 2022-05-23 17:33:18
...
内部类不能通过直接new的方式生成实例,而是要通过其外部类的实例来生成实例


public class Student {
private String name;

public Student(){}

public Student(String name){
this.name = name;
}

public class Home {
private String address;

public Home(){}

public Home(String address){
this.address = address;
}
}

public static void main(String[] args) throws IOException {
Student student = new Student("张三");
Home home = student.new Home("南京路100号");
}
}


关键点:[color=red]Home home = student.new Home("南京路100号");[/color]
相关标签: 内部类 实例化