Java学习笔记 继承与抽象类:内部类与匿名内部类

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();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值