Java缂栫▼鎬濇兂---绗崄绔? 鍐呴儴绫伙紙涓婏級

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java缂栫▼鎬濇兂---绗崄绔? 鍐呴儴绫伙紙涓婏級相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%b8%80%e8%b5%b7' title='涓€璧?>涓€璧?/a>   sele   center   蹇呴』   string   缁勭粐   man   interface   

绗崄绔?nbsp; 鍐呴儴绫伙紙涓婏級

 

銆€銆€鍙互灏嗕竴涓被鐨勫畾涔夋斁鍦ㄥ彟涓€涓被鐨勫畾涔夊唴閮紝杩欏氨鏄唴閮ㄧ被銆?/span>

銆€銆€鍐呴儴绫绘槸涓€绉嶉潪甯告湁鐢ㄧ殑鐗规€э紝瀹冨厑璁镐綘鎶婁竴浜涢€昏緫鐩稿叧鐨勭被缁勭粐鍦ㄤ竴璧凤紝骞舵帶鍒朵綅浜庡唴閮ㄧ殑绫荤殑鍙鎬э紝浣嗗唴閮ㄧ被涓庣粍鍚堟槸瀹屽叏涓嶅悓鐨勬蹇点€?/span>

 

10.1 鍒涘缓鍐呴儴绫?/span>

 

銆€銆€鍒涘缓鍐呴儴绫荤殑鏂规硶灏辨槸锛屾妸绫荤殑瀹氫箟缃簬澶栧洿绫荤殑閲岄潰锛?/span>

public class Parcel1 
    class Contents 
        private int i = 11;
        public int value()  return i; 
    
    class Destination 
        private String label;
        Destination(String whereTo) 
            label = whereTo;
        
        String readLabel()  return label; 
    
    public void ship(String dest) 
        Contents c = new Contents();
        Destination d = new Destination(dest);
        System.out.println(d.readLabel());
    
    public static void main(String[] args) 
        Parcel1 p = new Parcel1();
        p.ship("Tasmania");
    

 

銆€銆€褰撴垜浠湪ship()鏂规硶閲屼娇鐢ㄥ唴閮ㄧ被鐨勬椂鍊欎笌浣跨敤鏅€氱被娌′粈涔堜笉鍚岋紝瀹為檯鐨勫尯鍒氨鍙槸鍐呴儴绫荤殑鍚嶅瓧宓屽鍦?/span>Parcel1涓€?/span>

public class Parcel2 
    class Contents 
        private int i = 11;
        public int value()  return i; 
    
    class Destination 
        private String label;
        Destination(String whereTo) 
            label = whereTo;
        
        String readLabel()  return label; 
    
    public Destination to(String s) 
        return new Destination(s);
    
    public Contents contents() 
        return new Contents();
    
    public void ship(String dest) 
        Contents c = new Contents();
        Destination d = new Destination(dest);
        System.out.println(d.readLabel());
    
    public static void main(String[] args) 
        Parcel2 p = new Parcel2();
        p.ship("Tasmania");
        Parcel2 q = new Parcel2();
        Parcel2.Contents c = q.contents();
        Parcel2.Destination d = q.to("Borneo");
    

 

銆€銆€濡傛灉鎯充粠澶栭儴绫荤殑闈為潤鎬佹柟娉曚箣澶栫殑浠绘剰浣嶇疆鍒涘缓鏌愪釜鍐呴儴绫荤殑瀵硅薄锛岄偅涔堝繀椤诲儚鍦?/span>main()鏂规硶涓偅鏍凤紝鍏蜂綋鎸囨槑杩欎釜瀵硅薄鐨勭被鍨嬶細OuterClassName.InnerClassName銆?/span>

 

10.2 閾炬帴鍒板閮ㄧ被

 

銆€銆€褰撶敓鎴愪竴涓唴閮ㄧ被鐨勫璞℃椂锛屾瀵硅薄涓庡埗閫犲畠鐨勫鍥村璞′箣闂村氨鏈変簡涓€绉嶈仈绯伙紝瀹冭兘璁块棶鍏跺鍥村璞$殑鎵€鏈夋垚鍛橈紝鑰屼笉闇€瑕佷换浣曠壒娈婃潯浠讹紝姝ゅ鍐呴儴绫昏繕鎷ユ湁鍏跺鍥寸被鐨勬墍鏈夊厓绱犵殑璁块棶鏉冿細

interface Selector 
    boolean end();
    Object current();
    void next();


public class Sequence 
    private Object[] items;
    private int next = 0;
    public Sequence(int size) 
        items = new Object[size];
    
    public void add(Object x) 
        if(next < items.length)
            items[next++] = x;
    
    private class SequenceSelector implements Selector 
        private int i = 0;
        public boolean end() 
            return i == items.length;
        
        public Object current() 
            return items[i];
        
        public void next() 
            if(i < items.length)
                i++;
        
    
    public Selector selector() 
        return new SequenceSelector();
    
    public static void main(String[] args) 
        Sequence sequence = new Sequence(10);
        for(int i = 0; i < 10; i++)
            sequence.add(Integer.toString(i));
        Selector selector = sequence.selector();
        while (!selector.end()) 
            System.out.print(selector.current() + " ");
            selector.next();
        
    

 

銆€銆€Sequence鍙槸涓€涓浐瀹氬ぇ灏忕殑Object鏁扮粍锛屼互绫荤殑褰㈠紡鍖呰浜嗚捣鏉ワ紝鍙互璋冪敤add()鍦ㄥ簭鍒楁湯澧炲姞鏂扮殑Object锛岃鑾峰彇Sequence涓殑姣忎竴涓璞★紝鍙互浣跨敤Selector鎺ュ彛銆傛敞鎰忔柟娉?/span>end()銆?/span>current()銆?/span>next()閮界敤鍒颁簡objects锛岃繖鏄竴涓紩鐢紝瀹冨苟涓嶆槸SequenceSelector鐨勪竴閮ㄥ垎锛岃€屾槸澶栧洿绫讳腑鐨勪竴涓?/span>private瀛楁锛岀劧鑰屽唴閮ㄧ被鍙互璁块棶鍏跺鍥寸被鐨勬柟娉曞拰瀛楁锛岃繖甯︽潵浜嗘瀬澶х殑鏂逛究銆?/span>

 

10.3 浣跨敤.this鍜?/span>.new

 

銆€銆€濡傛灉浣犻渶瑕佺敓鎴愬澶栭儴绫诲璞$殑寮曠敤锛屽彲浠ヤ娇鐢ㄥ閮ㄧ被鐨勫悕瀛楀悗闈㈢揣璺?/span>.this锛岃繖鏍蜂骇鐢熺殑寮曠敤鑷姩鍦板叿鏈夋纭殑绫诲瀷锛岃繖涓€鐐瑰湪缂栬瘧鏈熷氨琚煡鏅撳苟鍙楀埌妫€鏌ワ紝鍥犳娌℃湁浠讳綍杩愯鏃剁殑寮€閿€锛?/span>

public class DotThis 
    void f() 
        System.out.println("DotThis.f()");
    
    public class Inner 
        public DotThis outer() 
            return DotThis.this;
        
    
    public Inner inner() 
        return new Inner();
    
    public static void main(String[] args) 
        DotThis dt = new DotThis();
        DotThis.Inner dti = dt.inner();
        dti.outer().f();
    

 

銆€銆€鏈夋椂鍊欎綘鍙兘鎯宠鍛婄煡鏌愪簺鍏朵粬瀵硅薄鍘诲垱寤哄叾鏌愪釜鍐呴儴绫荤殑瀵硅薄锛屼綘蹇呴』鍦?/span>new琛ㄨ揪寮忎腑鎻愪緵瀵瑰叾浠栧閮ㄧ被瀵硅薄鐨勫紩鐢紝杩欐椂闇€瑕佷娇鐢?/span>.new璇硶锛?/span>

public class DotNew 
    public class Inner 
    public static void main(String[] args) 
        DotNew dn = new DotNew();
        DotNew.Inner dni = dn.new Inner();
    

 

銆€銆€瑕佹兂鐩存帴鍒涘缓鍐呴儴绫荤殑瀵硅薄锛屼綘涓嶈兘鎸夌収浣犳兂璞$殑鏂瑰紡鍘诲紩鐢ㄥ閮ㄧ被鐨勫悕瀛?/span>DotNew锛岃€屾槸蹇呴』浣跨敤澶栭儴绫荤殑瀵硅薄鏉ュ垱寤鸿鍐呴儴绫诲璞★紝灏卞儚鍦ㄤ笂闈㈢湅鍒扮殑閭f牱锛岃繖灏辫В鍐充簡鍐呴儴绫诲悕瀛椾綔鐢ㄥ煙鐨勯棶棰樸€?/span>

銆€銆€鍦ㄦ嫢鏈夊閮ㄧ被瀵硅薄涔嬪墠鏄笉鍙兘鍒涢€犲唴閮ㄧ被瀵硅薄鐨勶紝杩欐槸鍥犱负鍐呴儴绫诲璞′細鏆楁殫杩炴帴鍒板垱寤哄畠鐨勫閮ㄧ被瀵硅薄涓婏紝浣嗗鏋滀綘鍒涘缓鐨勬槸宓屽绫伙紙闈欐€佸唴閮ㄧ被锛夛紝閭d箞瀹冨氨涓嶉渶瑕佸澶栭儴绫诲璞$殑寮曠敤銆?/span>

銆€銆€涓嬮潰鎴戜滑鎶?/span>.new搴旂敤鍒?/span>Parcel锛?/span>

public class Parcel3 
    class Contents 
        private int i = 11;
        public int value() 
            return i;
        
    
    class Destination 
        private String label;
        Destination(String whereTo) 
            label = whereTo;
        
        String readLabel() 
            return label;
        
    
    public static void main(String[] args) 
        Parcel3 p = new Parcel3();
        Parcel3.Contents c = p.new Contents();
        Parcel3.Destination d = p.new Destination("Tasmania");
    

 

10.4 鍐呴儴绫讳笌鍚戜笂杞瀷

 

銆€銆€褰撳皢鍐呴儴绫诲悜涓婅浆鍨嬩负鍩虹被锛屽挨鍏舵槸杞瀷涓轰竴涓帴鍙g殑鏃跺€欙紝鍐呴儴绫诲氨鏈変簡鐢ㄦ涔嬪湴锛岃繖鏄洜涓哄唴閮ㄧ被——鏌愪釜鎺ュ彛鐨勫疄鐜?mdash;—鑳藉瀹屽叏涓嶅彲瑙佸苟涓斾笉鍙敤锛屾墍寰楀埌鐨勭煡璇嗘寚鍚戝熀绫绘垨鎺ュ彛鐨勫紩鐢紝鎵€浠ヨ兘澶熷緢鏂逛究鍦伴殣钘忓疄鐜扮粏鑺傘€?/span>

銆€銆€褰撳彇寰椾簡涓€涓寚鍚戝熀绫绘垨鎺ュ彛鐨勫紩鐢ㄦ椂锛岀敋鑷冲彲鑳芥棤娉曟壘鍑哄畠纭垏鐨勭被鍨嬶細

public class Parcel4 
    private class PContents implements Contents 
        private int i = 11;
        public int value() 
            return i;
        
    
    protected class PDestination implements Destination 
        private String label;
        private PDestination(String whereTo) 
            label = whereTo;
        
        public String readlabel() 
            return label;
        
    
    public Destination destination(String s) 
        return new PDestination(s);
    
    public Contents contents() 
        return new PContents();
    


public class TestParcel 
    public static void main(String[] args) 
        Parcel4 p = new Parcel4();
        Contents c = p.contents();
        Destination d = p.destination("Tasmania");
    

 

10.5 鍦ㄦ柟娉曞拰浣滅敤鍩熷唴鐨勫唴閮ㄧ被

 

銆€銆€閫氬父濡傛灉璇诲啓鐨勪唬鐮佸寘鍚簡鍐呴儴绫伙紝閭d箞浠栦滑閮芥槸骞冲嚒鐨勫唴閮ㄧ被锛岀畝鍗曡€屼笖瀹规槗鐞嗚В锛岀劧鑰屽唴閮ㄧ被鐨勮娉曡鐩栦簡澶ч噺鍏朵粬鐨勯毦浠ョ悊瑙g殑鎶€鏈紝渚嬪锛屽彲浠ュ湪涓€涓柟娉曢噷闈㈡垨鑰呭湪浠绘剰浣滅敤鍩熷唴瀹氫箟鍐呴儴绫伙紝杩欎箞鍋氭湁涓や釜鐞嗙敱锛?/span>

銆€銆€1銆佸疄鐜颁簡鏌愮被鍨嬬殑鎺ュ彛锛屼簬鏄彲浠ュ垱寤哄苟杩斿洖瀵瑰叾鐨勫紩鐢?/span>

銆€銆€2銆佽瑙e喅涓€涓鏉傜殑闂锛屾兂鍒涘缓涓€涓被鏉ヨ緟鍔╀綘鐨勮В鍐虫柟妗堬紝浣嗘槸鍙堜笉甯屾湜杩欎釜绫绘槸鍏叡鍙敤鐨?/span>

銆€銆€绗竴涓緥瀛愬睍绀哄湪鏂规硶鐨勪綔鐢ㄥ煙鍐呭垱寤轰竴涓畬鏁寸殑绫伙紝杩欒绉颁綔灞€閮ㄥ唴閮ㄧ被锛?/span>

public class Parcel5 
    public Destination destination(String s) 
        class PDestination implements Destination 
            private String label;
            private PDestination(String whereTo) 
                label = whereTo;
            
            public String readLabel() 
                return label;
            
        
        return new PDestination(s);
    

    public static void main(String[] args) 
        Parcel5 p = new Parcel5();
        Destination d = p.destination("Tasmania");
    

 

銆€銆€PDestination绫绘槸destination()鏂规硶鐨勪竴閮ㄥ垎锛岃€屼笉鏄?/span>Parcel5鐨勪竴閮ㄥ垎锛屾墍浠ュ湪destination涔嬪涓嶈兘璁块棶PDestination銆備綘鍙互鍦ㄥ悓涓€瀛愮洰褰曚笅鐨勪换鎰忕被瀵规煇涓唴閮ㄧ被浣跨敤绫绘爣璇嗙PDestination锛岃繖骞朵笉浼氭湁鍛藉悕鍐茬獊銆?/span>

銆€銆€涓嬮潰鐨勪緥瀛愬睍绀哄浣曞湪浠绘剰鐨勪綔鐢ㄥ煙涓祵鍏ヤ竴涓唴閮ㄧ被锛?/span>

public class Parcel6 
    private void internalTracking(boolean b) 
        if(b) 
            class TrackingSlip 
                private String id;
                TrackingSlip(String s) 
                    id = s;
                
                String getSlip() 
                    return id;
                
            
            TrackingSlip ts = new TrackingSlip("slip");
            String s = ts.getSlip();
        
    
    public void track() 
        internalTracking(true);
    
    public static void main(String[] args) 
        Parcel6 p = new Parcel6();
        p.track();
    

 

銆€銆€TrackingSlip绫昏宓屽叆鍦?/span>if璇彞鐨勪綔鐢ㄥ煙鍐咃紝杩欏苟涓嶆槸璇磋绫荤殑鍒涘缓鏃舵湁鏉′欢鐨勶紝瀹冨叾瀹炰笌鍒殑绫讳竴璧风紪璇戣繃浜嗭紝鐒惰€屽湪瀹氫箟TrackingSlip鐨勪綔鐢ㄥ煙涔嬪瀹冩槸涓嶅彲浠ョ孩鐨勶紝闄ゆ涔嬪锛屽畠涓庢櫘閫氱殑鍏朵粬绫讳竴鏍枫€?/span>

 

10.6 鍖垮悕鍐呴儴绫?/span>

 

銆€銆€涓嬮潰鐨勪緥瀛愮湅璧锋潵鏈夌偣濂囨€細

public class Parcel7 
    public Contents contents() 
        return new Contents() 
            private int i = 11;
            public int value() 
                return i;
            
        
    
    public static void main(String[] args) 
        Parcel7 p = new Parcel7();
        Contents c = p.contents();
    

 

銆€銆€contents鏂规硶灏嗚繑鍥炲€肩殑鐢熸垚涓庤〃绀鸿繖涓繑鍥炲€肩殑绫荤殑瀹氫箟缁撳悎鍦ㄤ竴璧凤紝闄ゆ涔嬪杩欎釜绫绘槸鍖垮悕鐨勶紝娌℃湁鍚嶅瓧銆傝繖绉嶈娉曟寚鐨勬槸鍒涘缓涓€涓户鎵胯嚜Contents鐨勫尶鍚嶇被鐨勫璞★紝閫氳繃new琛ㄨ揪寮忚繑鍥炵殑寮曠敤琚嚜鍔ㄥ悜涓婅浆鍨嬩负瀵?/span>Contents鐨勫紩鐢ㄣ€傚尶鍚嶅唴閮ㄧ被涓庢瑙勭殑缁ф壙鐩告瘮鏈変簺鍙楅檺锛屽洜涓哄尶鍚嶅唴閮ㄧ被鏃㈠彲浠ユ墿灞曠被锛屼篃鍙互瀹炵幇鎺ュ彛锛屼絾鏄笉鑳戒簩鑰呭吋澶囷紝鑰屽鏋滄槸瀹炵幇鎺ュ彛涔熷彧鑳藉疄鐜颁竴涓帴鍙c€?/span>

 

10.6.1 鍐嶈宸ュ巶鏂规硶

 

銆€銆€浼樺寲宸ュ巶鏂规硶锛屾澶勭暐銆?/span>

銆€銆€浼樺厛浣跨敤绫讳笉鏄帴鍙o紝濡傛灉浣犺璁′腑闇€瑕佹煇涓帴鍙o紝浣犲繀椤讳簡瑙d粬锛屽惁鍒欎笉鍒颁竾涓嶅緱宸蹭笉瑕佸皢鍏舵斁鍦ㄤ綘鐨勮璁′腑銆?/span>

以上是关于Java缂栫▼鎬濇兂---绗崄绔? 鍐呴儴绫伙紙涓婏級的主要内容,如果未能解决你的问题,请参考以下文章

cnpm涓嶆槸鍐呴儴鎴栧閮ㄥ懡浠?cnpm: command not found

绗叓璇?绗簩璁?08_02_bash鑴氭湰缂栫▼涔嬩竷 case璇彞鍙婅剼鏈€夐」杩涢樁

鍑芥暟寮忕紪绋嬩腑鐨勫壇浣滅敤姒傚康