1.内部类
(1)内部类的概念(inner class)
定义在其他类中的类,也称为嵌套类
可以看成是外部类的成员,所以也成为成员类
作用:将逻辑上相关的类放到一起
不能与外部类同名
内部类可以有自己的成员变量,方法等,与一般类相同
(2)内部类的特征
可用修饰符比外部类多
可以声明为private和protected
可以用final,abstract,static修饰
内部类也可以是一个接口,该接口必须有另一个内部类实现
(3)内部类与外部类成员访问
外部类通过内部类的对象访问内部类中定义的成员
外部类的成员变量在内部类中有效
··内部类可以直接访问外部类的其他域及方法,即使private也行
(4)编译结果
public class Group {
private int age;
public class Student {
String name;
public Student(String n, int a) {
name = n;
age = a;
}
public void output() {
System.out.println("姓名" + this.name + ";年龄:" + age);
}
}//内部类
public void output(){ //外部类成员方法
Student stu = new Student("牛羊",24);//内部类成员stu
stu.output();
}
public static void main(String[] args) {
Group g = new Group();
g.output();//外部类实例调用外部类成员方法
}
}
上面是通过外部对象调用方法在方法中间接创建内部类对象
疑问,能否直接创建内部类对象吗?
可以只要改成
public class Group {
private int age;
public class Student {//内部类
String name;
public Student(String n, int a) {
name = n;
age = a;
}
public void output() {
System.out.println("姓名" + this.name + ";年龄:" + age);
}
}
public static void main(String[] args) {
Group g = new Group();
Group.Student stu = g.new Student("刘洋",24);
stu.output();
}
}
(6)方法中的内部类
在方法在定义的类
同局部变量一样,不能用public,private,protected,static修饰
内部类定义非常灵活,甚至可以定义在循环体内
2.匿名内部类
(1)匿名内部类(anonymous inner class)
一种特殊的内部类,没有类名,直接用其父类的名字或所实现的接口的名字
在定义类的同时,创建该类的一个对象
可以访问外嵌类中的成员变量和方法,不可以声明类变量和类方法
(2)匿名内部类特点
不使用关键词class
类名前面不能有修饰符
类中不能定义构造方法,因为它没有类名
类在定义后马上用到,只用一次
public class anonymous {
static class inner{String name;}//定义内部类
public static void main(String[] args) {
(//括号里相当于一个匿名内部类对象
new inner(){//用父类的构造方法创建匿名内部类对象
void setName(String n){//弥补内部类inner里没有定义到的方法
name = n;
System.out.println("姓名:"+name);
}
}
).setName("张华");//执行匿名内部类里所定义的方法
}
}
类要非常小(推荐10行代码以下)
编程训练
class School
{ String name;
public class Student
{ String name;
int age;
public Student(String schoolName, String studentName, int newAge)
{ 【代码1】//将schoolName赋值给School类的name属性
【代码2】//将studentName赋值给Student类的name属性
【代码3】 //将newAge赋值给Student类的age属性 }
public void output ( )
{ System.out.println("学校:"+School.this.name);
System.out.println("姓名:"+this.name);
System.out.println("年龄:"+this.age); }
}
public void output( )
{ Student stu = new Student("金融学院","张三", 24);
stu.output( ); }
}
public class Inner
{ public static void main(String[ ] args)
{ System.out.println("--通过外部类成员访问内部类成员--");
School a = new School( );
a.output( );
System.out.println("--直接访问内部类成员--");
School.Student b = a.new Student("金融学院","李四",23);
b.output();
}
}
答案
class School {
String name;
public class Student {
String name;
int age;
public Student(String schoolName, String studentName, int newAge) {
School.this.name = schoolName;//将schoolName赋值给School类的name属性
this.name = studentName;//将studentName赋值给Student类的name属性
this.age = newAge; //将newAge赋值给Student类的age属性
}
public void output(){
System.out.println("学校:" + School.this.name);
System.out.println("姓名:" + this.name);
System.out.println("年龄:" + this.age);
}
}
public void output() {
Student stu = new Student("金融学院", "张三", 24);
stu.output();
}
}
public class text {
public static void main(String[] args) {
System.out.println("--通过外部类成员访问内部类成员--");
School a = new School();
a.output();
System.out.println("--直接访问内部类成员--");
School.Student b = a.new Student("金融学院", "李四", 23);
b.output();
}
}