zoukankan      html  css  js  c++  java
  • java 内部类复习

      1 /**
      2  * 内部类详解
      3  * 
      4  * @author shao
      5  * 
      6  */
      7 public class InnerClass {
      8 
      9     public static void main(String[] args) {
     10         Man p = new Man("丽丽");
     11         Man.Son son = p.new Son("小丽丽");
     12         son.play();
     13         Man.Girl girl = new Man.Girl();
     14         girl.play();
     15         p.work();
     16         p.getMoney().fun();
     17     }
     18 
     19 }
     20 
     21 class Man {
     22 
     23     private String wife;
     24     private String house = "房子";
     25     private static String girlOwn = "嫁妆是必须的";
     26 
     27     public Man(String wife) {
     28         this.wife = wife;
     29     }
     30 
     31     public void work() {
     32         // 从不向儿子索取
     33         // System.out.println("工作"+toy);
     34 
     35         // 局部内部类,与局部变量类似 ,用法与成员内部类一样
     36         class Sleep {
     37             public String sleep() {
     38                 return "该睡觉了~~~~";
     39             }
     40         }
     41 
     42         System.out.println(new Sleep().sleep());
     43 
     44     }
     45 
     46     public static String fun() {
     47         return "天天上班挣钱";
     48     }
     49 
     50     public Money getMoney() {
     51 
     52         /*
     53          * 匿名内部类是一种特殊的局部内部类,没有类名,只适合一次并且不需要多次创建的类。 匿名内部类是唯一一种没有构造方法的类。
     54          * 匿名内部类用于继承其他类或是实现接口,并不需要增加额外的方法,只是对继承方法的实现或是重写,在android开发中经常用到。事件监听等 。
     55          */
     56         return new Money() {
     57 
     58             @Override
     59             public void fun() {
     60                 System.out.println("就涨过一次工资。。。。哎");
     61             }
     62 
     63         };
     64     }
     65 
     66     /*
     67      * 非静态内部类,可以调用引用类的成员变量,但引用类,不可以调用 内部 类的成员变量。
     68      */
     69     class Son {
     70 
     71         private String wife;
     72         private String toy;
     73 
     74         public Son(String wife) {
     75             this.wife = wife;
     76         }
     77 
     78         public void play() {
     79             System.out.println(wife + ",是我妈咪的儿媳妇~");
     80             System.out.println(Man.this.wife + ",是我妈咪");
     81             System.out.println("我是儿子,可以继承资产..." + house + "--哈哈——");
     82             System.out.println(fun() + "也是我的钱。");
     83         }
     84     }
     85 
     86     /*
     87      * 当一个静态内部类存在,并不一定存在对应 的外部类对象 可以看成外部类(引有类)的一个静态成员
     88      */
     89     static class Girl {
     90 
     91         private String dress;
     92 
     93         public void play() {
     94 
     95             // 静态内部类不可以访问外部类的成员变量
     96             // System.out.println("我也不需要房子"+house);
     97             System.out.println(girlOwn + ",这个再不能给哥哥了~");
     98 
     99         }
    100     }
    101 
    102     public interface Money {
    103         void fun();
    104     }
    105 }
  • 相关阅读:
    ipv4 ipv6 求字符串和整数一一映射的算法 AmazonOrderId
    Example Bookstore schema showing how data is sharded DATABASE SHARDING
    When Database Sharding is Appropriate DATABASE SHARDING
    Database Sharding Challenges DATABASE SHARDING
    Database Sharding, The “Shared-Nothing” Approach DATABASE SHARDING
    Database Partitioning Options DATABASE SHARDING
    What Drives the Need for Database Sharding? DATABASE SHARDING
    The Rise of Database Sharding DATABASE SHARDING
    为了数学美,拆卸了脚手架
    Scanline Fill Algorithm
  • 原文地址:https://www.cnblogs.com/shaoyu19900421/p/3819394.html
Copyright © 2011-2022 走看看