目录
内部类
java中,允许在一个类的内部定义类,这样的类称之为内部类,内部类所在的类称之为外部类。
内部类可以分为4种:成员(实例)内部类,局部(方法)内部类,静态内部类,匿名内部类
成员内部类
一个类中可以定义成员变量,成员方法,还可以定义类,这样的类称为成员内部类
class Person{//外部类
int age=19;
String name ="李四";
class Student{
String classroom="高三一班";
double score=98.7;
//static int s; 内部类属于外部类,需要外部类的实例化对象调用,而static定义的变量属于类,不属于对象
public Student() {
System.out.println("内部类的构造方法");
}
}
public Person() {
System.out.println("外部类的构造方法");
}
}
public class Text {
public static void main(String[] args) {
Person person=new Person();
Person.Student student= person.new Student();
System.out.println(person.age);
System.out.println(person.name);
System.out.println(student.classroom);
System.out.println(student.score);
}
}
1、内部类需要借助外部类的实例化对象才可以实例化,属于对象,而static属于类(如果非要定义,必须是静态的常量(final)
2、如何实例化内部类
格式:外部类名.内部类名 变量名=new 外部类名().new 内部类名();
3、外部类和内部类的相互访问
内部类访问外部类的成员
内部类有两个this,一个是内部类的,一个是外部类的
public Student() {
System.out.println("内部类的构造方法");
}
public void fun(){
this.name="hi";//在内部类中指代内部类的变量
Person.this.name="world";//在内部类中指代外部类的变量
}
内部类里面可以实现对内外类变量值的更改
class Student{
String name="高三一班";
double score=98.7;
//static int s; 内部类属于外部类,需要外部类的实例化对象调用,而static定义的变量属于类,不属于对象
public Student() {
System.out.println("内部类的构造方法");
}
public void fun(){
this.name="hi";//在内部类中指代内部类的变量
Person.this.name="world";//在内部类中指代外部类的变量
}
外部类访问内部类
- 如果内部类被static 修饰:直接实例化内部类对象:new Student()
- 如果内部类没有被static 修饰:通过实例化外部类来实例化内部类对象:new Person().new Student()
外部类不能直接实现对内部类的更改,外部类只有一个this指代外部类
(除非构建内部类对象,调用内部类方法来更改变量值)
class Person{//外部类
int age=19;
String name ="李四";
class Student{
String name="高三一班";
double score=98.7;
//static int s; 内部类属于外部类,需要外部类的实例化对象调用,而static定义的变量属于类,不属于对象
public Student() {
System.out.println("内部类的构造方法");
}
public void fun(){
this.name="hi";//在内部类中指代内部类的变量
Person. this.name="world";//在内部类中指代外部类的变量
}
}
public Person() {
System.out.println("外部类的构造方法");
}
public void change(){
this.name="hello";//外部类中指代当前类的变量
//外部类访问内部类
// new Person().new Student().name="bite";
//对实例化对象的更改,没有改变类的值
}
public void newchange(){
new Person().new Student().fun();
// new Student().fun();//调用内部类的方法来改变
}
}
public class Text {
public static void main(String[] args) {
Person person=new Person();
Person.Student student= person.new Student();
System.out.println(student.name);
System.out.println(person.name);
System.out.println("内部类更改_______");
student.fun();
System.out.println(student.name);
System.out.println(person.name);
System.out.println("外部类更改_______");
person.change();
System.out.println(student.name);
System.out.println(person.name);
person.newchange();
System.out.println(student.name);
System.out.println(person.name);
}
方法内部类
定义在某一个方法里的内部类,有效范围只有在方法的内部
class Animal{
private String name="animal";
public void printbird(){
class Bird{
int age=9;
String name="bird";
}
Bird bird=new Bird();
System.out.println(bird.age);
System.out.println(bird.name);
System.out.println(Animal.this.name);
//方法内部类可以访问外部类的所有成员
}
}
public class Text {
public static void main(String[] args) {
Animal animal=new Animal();
animal.printbird();
}
}
1、 方法内部类可以访问外部类的所有成员
2、外部类只可以在调用方法内部类所在的方法时才可以访问到内部类的信息
静态内部类
static修饰的成员内部类
1、创建静态内部类对象的语法格式
2、相互访问
静态内部类访问外部类的静态成员
- 直接使用外部类类名调用外部类的静态成员:静态内部类属于类,而普通的外部类成员属于对象
- 实例化外部类来调用非静态成员
更改:内部类中可以直接使用外部类类名对外部类的静态成员更改,无法更改外部类的非静态成员
在外部类访问静态内部类
- 通过实例化内部类对象访问内部类
更改:无法对内部类进行更改
class Out{
static int age=90;
String name="out";
static class In{
int age=8;
String name="world";
public void fun(){
Out.age=80;
System.out.println(Out.age);
System.out.println(new Out().name);
}
}
public void print(){
//在外部类访问静态内部类->实例化
In in= new In();
System.out.println(in.age);
}
}
public class Text {
public static void main(String[] args) {
Out.In in=new Out.In();
System.out.println(in.age);
Out out=new Out();
System.out.println(out.name);
in.fun();
}
}
匿名内部类
匿名内部类的格式如下
class TextDemo{
public void print(){
System.out.println("TextDemo的print方法");
}
}
public class Text {
public static void main(String[] args) {
new TextDemo(){
};
}
public static void main(String[] args) {
new TextDemo(){
}.print();//调用了匿名内部类的print方法-》也就是TextDemo类的print方法
}
public static void main(String[] args) {
new TextDemo(){
@Override
public void print() {//对匿名内部类的方法进行重写,调用重写的方法
System.out.println("这是重写的方法");
}
}.print();//调用了匿名内部类的print方法-》也就是TextDemo类的print方法
}