内部类定义在类中,作为外部类的成员变量,拥有类和成员变量的特征;而局部内部类定义在方法内、代码块内、构造器内(有必须使用final局部变量的问题)。
下面为成员内部类使用示例:
- 外部类和内部类属性方法的互相调用
@Data
public class School {
private String schoolName = "schoolName—School";
private String address = "济南—School";
public static String staticField = "staticField—School";
public static String staticField1= "staticField1—School";
public School(){
System.out.println("School Constructor");
}
/**
* 非静态内部类:
* 1、不能定义静态方法、静态变量,因为非静态内部类还没加载,内部的静态方法就加载了,这是不可能的
* 2、可以定义全局常量
* 3、类可以使用任意访问修饰符
* 4、非静态内部类对象创建需要使用外部类的对象.new 的方式
* 5、非静态内部类中有和外部类同名的属性或方法,内部类可以直接访问内部类的成员变量或方法,但如果内部类访问外部类的成员变量或者方法时,
* 需要使用外部类.this关键字
* 6、非静态内部类也是类,该继承继承,该重写重写,该重载重载,this和super随便用
*/
@Data
public class Student extends Teacher{
private String schoolName = "schoolName—Student";
private String studentName = "studentName—Student";
public static final String CLASS = "36班—Student";
//错误
//public static String staticFiled;
public Student(){
System.out.println("Student Constructor");
}
private String getStudentInfo(){
//School.this. 内部类中有同名的应该使用此方式调用外部类中同名属性
System.out.println("调用外部类中的同名属性:" + School.this.schoolName + ",调用内部类中属性:" + schoolName);
System.out.println("调用内部类中属性:" + studentName);
return "调用内部类的私有方法";
}
public void studentPublicMethod(){
System.out.println("非静态内部类做父类 -方法");
}
//错误
// public static void staticMethod(){
// System.out.println("静态内部类中的静态方法");
// }
}
/**
* 静态内部类:
* 1、内部可以包含任意的信息
* 2、静态内部类调用外部类中的非静态属性方法,需要使用外部类的对象调用
* 3、静态内部类对象可以直接创建(new 外部类.内部类())
*/
@Data
public static class Teacher{
private String schoolName = "schoolName-Teacher"; //与外部类同名属性
public static String staticField = "staticField-Teacher";
private String teacherName = "teacherName-Teacher";
Integer status = 1;
public Teacher(){
System.out.println("Teacher Constructor");
}
private String getTeacherInfo(){
//静态内部类调用外部类中的非静态属性方法,需要使用外部类的对象调用
School school = new School();
System.out.println("调用外部类中的非静态属性:" + school.address);
System.out.println("调用外部类中静态属性:" + staticField1);
System.out.println("调用外部类中的同名静态属性:" + School.staticField + "调用内部类中的同名静态属性:" + staticField);
System.out.println("调用外部类中的同名非静态属性:" + school.schoolName + "调用内部类中同名非静态属性:" + schoolName);
return "调用内部类的私有方法";
}
public void publicMethod(){
System.out.println("静态内部类做父类 -方法");
}
public static void staticMethod(){
School school = new School();
System.out.println("调用外部类中的非静态属性:" + school.address);
System.out.println("调用外部类中静态属性:" + staticField1);
System.out.println("调用外部类中的同名静态属性:" + School.staticField + "调用内部类中的同名静态属性:" + staticField);
System.out.println("调用外部类中的同名非静态属性:" + school.schoolName);
System.out.println("静态内部类中的静态方法");
}
}
public void callInner(){
/**
* 外部类可以调用内部类的私有属性,私有方法
*/
Teacher t = new Student();
System.out.println(((Student)t).getStudentInfo());
System.out.println(Student.CLASS);
Teacher.staticMethod();
Teacher teacher = new Teacher();
Integer status = teacher.status;
String staticField = Teacher.staticField;
System.out.println("status: " + status + "staticField: " + staticField);
String teacherName = teacher.teacherName;
System.out.println("teacherName: "+ teacherName);
Teacher teacher1 = new Teacher();
teacher1.getTeacherInfo();
}
}
- 内部类对象创建
public static void main(String[] args) {
//非静态内部类需要使用外部类的对象来创建对象
School school = new School();
School.Student student = school.new Student();
//静态内部类直接使用外部类.的方式
School.Teacher teacher = new School.Teacher();
/**
* 静态内部类、非静态内部类都可以被继承
*/
School.Teacher teacher1 = new ExtendInternalClass();
teacher1.publicMethod();
}
- 继承内部类
/**
* 继承静态内部类
*
*/
public class ExtendInternalClass1 extends School.Teacher {
@Override
public void publicMethod() {
System.out.println("继承内部类,并重写方法!!");
}
}
/**
* 继承非静态内部类,需要一个引用外部类School的构造方法
*
*/
public class ExtendInternalClass extends School.Student {
public ExtendInternalClass(School school){
school.super();
}
}