A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Ethan丶 中级黑马   /  2015-9-18 19:33  /  725 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Student {
  2.         public void study() {
  3.                 System.out.println(" 学习使人进步");
  4.         }
  5. }

  6. class StudentDemo {
  7.         public Student getStudent() {
  8.                 return new Student();
  9.         }
  10. }

  11. class StudentTest3 {
  12.         public static void main(String[] args) {
  13.                
  14.                 StudentDemo sd = new StudentDemo();
  15.        
  16.                 sd.getStudent().study();
  17.         }
  18. }
复制代码

4 个回复

倒序浏览
每次调用完毕方法后,返回的是一个对象。   sd.getStudent().study();
回复 使用道具 举报
今日复习小结2:匿名内部类
内部类概述:
                把类定义在其他类的内部,这个类就被称为内部类。
       
        1.内部的访问特点:
                A:内部类可以直接访问外部类的成员,包括私有。
                B:外部类要访问内部类的成员,必须创建对象。
        2.内部类位置
                成员位置:在成员位置定义的类,被称为成员内部类。       
                局部位置:在局部位置定义的类,被称为局部内部类。
                               
               成员位置:在成员位置定义的类,被称为成员内部类。
        3.成员内部类:
                如何直接访问内部类的成员。
                外部类名.内部类名 对象名 = 外部类对象.内部类对象;

注意:成员内部类被静态修饰后的访问方式是:
          格式:外部类名.内部类名 对象名 = new 外部类名.内部类名();

匿名内部类就是内部类的简化写法。格式:
                new 类名或者接口名(){
                        重写方法;
                }
回复 使用道具 举报
嗯,楼主也可以用匿名内部类
回复 使用道具 举报
class StudentDemo {
        public Student getStudent() {
                return new Student(){
                             public void study() {
                                        System.out.println(" 学习使人进步");
                               }
                           };
        }
}

class StudentTest3 {
        public static void main(String[] args) {
               
                StudentDemo sd = new StudentDemo();
        
                sd.getStudent().study();
        }
}
匿名内部类。是这样不。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马