NET-JAVA 六--------- 匿名类、内部类、

来源:互联网 发布:大数据在电商中的应用 编辑:程序博客网 时间:2024/04/30 21:16
     


package main;

public class InnerCls2 {
     private void into(boolean b){
         if(b){
             class GDestination {
                 private String label;
                 public GDestination(String whereTo){
                     label = whereTo;
                 }
                 public String readLabel(){
                     return label;
                 }   
             }
             GDestination destination = new GDestination("beijing");
             System.out.println(destination.readLabel());
         }
     }
    
     public void dest(boolean b){
         into(b);
     }
    
     public static void main(String args[]){
         InnerCls2 inner = new InnerCls2();
         inner.dest(true);
     }
 }


package main;

public class TestInnerClass {
 public static void main(String args[]){
        Goods goodsObj = new Goods();
        
        Contents content = goodsObj.dest();
        System.out.println(content.value());
       
        Destination destination = goodsObj.cont("描述信息:北京");
        System.out.println(destination.readLabel());
    }
}

interface Contents{
    int value();
}
interface Destination{
    String readLabel();
}

class Goods{
 private int valueRate = 2;
 
    private class Content implements Contents{
     private int i = 11 * valueRate;
        public int value(){
            return i;
        }
    }
    protected class GDestination implements Destination{
        private String label;
        private GDestination(String whereTo){
            label = whereTo;
        }
        public String readLabel(){
            return label;
        }
    }
    public Content dest(){
        return new Content();
    }
    public GDestination cont(String s){
        return new GDestination(s);
    }
}


0 0