锘??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美日韩视频在线第一区,在线播放日韩专区,久久一本综合http://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386807.htmlabinabinSun, 02 Sep 2012 10:00:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386807.htmlhttp://www.aygfsteel.com/stevenjohn/comments/386807.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386807.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/386807.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/386807.htmlpackage com.abin.lee.collection.inner;
public class XiXiang {
public static enum IdType{
ONE("one"),TWO("two"),THREE("three");
private String textVal;
private IdType(String textVal){
this.textVal=textVal;
}
private String getString(){
return textVal;
}
}
public static String get(IdType type,String id){
String status=null;
final class get{
String result=null;
public String getName(String id){
if(id.equals("1")){
result="lee";
}
return result;
}
}
if(type==IdType.valueOf("ONE")){
status=new get().getName(id);
}
return status;
}
public static void main(String[] args) {
String id="1";
IdType type=IdType.valueOf("ONE");
String result=new XiXiang().get(type,id);
System.out.println("result="+result);
}
}


abin 2012-09-02 18:00 鍙戣〃璇勮
]]>
Java 鍐呴儴綾伙紙涓錛?/title><link>http://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386801.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Sun, 02 Sep 2012 08:43:00 GMT</pubDate><guid>http://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386801.html</guid><wfw:comment>http://www.aygfsteel.com/stevenjohn/comments/386801.html</wfw:comment><comments>http://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386801.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/stevenjohn/comments/commentRss/386801.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/stevenjohn/services/trackbacks/386801.html</trackback:ping><description><![CDATA[<div>package com.abin.lee.collection.inner;</div><div></div><div></div><div>public class LiangCheng {</div><div></div><div><span style="white-space:pre"> </span>public static String LocalProcess(String wu, String id) {</div><div><span style="white-space:pre"> </span>String well = null;</div><div><span style="white-space:pre"> </span>if (wu.equalsIgnoreCase("abin")) {</div><div><span style="white-space:pre"> </span>LiangCheng liangCheng=new LiangCheng();</div><div><span style="white-space:pre"> </span>LiangCheng.Process process=liangCheng.new Process();</div><div><span style="white-space:pre"> </span>well=process.getNameById(id);</div><div><span style="white-space:pre"> </span>}</div><div><span style="white-space:pre"> </span>return well;</div><div><span style="white-space:pre"> </span>}</div><div></div><div><span style="white-space:pre"> </span>public class Process {</div><div><span style="white-space:pre"> </span>private String result = null;</div><div><span style="white-space:pre"> </span>public String getNameById(String id) {</div><div><span style="white-space:pre"> </span>if (id.equalsIgnoreCase("15")) {</div><div><span style="white-space:pre"> </span>result = "lee";</div><div><span style="white-space:pre"> </span>}</div><div><span style="white-space:pre"> </span>return result;</div><div><span style="white-space:pre"> </span>}</div><div></div><div><span style="white-space:pre"> </span>}</div><div><span style="white-space:pre"> </span>public static void main(String[] args) {</div><div><span style="white-space:pre"> </span>LiangCheng liangCheng=new LiangCheng();</div><div><span style="white-space:pre"> </span>String wu="abin";</div><div><span style="white-space:pre"> </span>String id="15";</div><div><span style="white-space:pre"> </span>String result=liangCheng.LocalProcess(wu, id);</div><div><span style="white-space:pre"> </span>System.out.println("result="+result);</div><div><span style="white-space:pre"> </span>}</div><div></div><div>}</div><div></div><img src ="http://www.aygfsteel.com/stevenjohn/aggbug/386801.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/stevenjohn/" target="_blank">abin</a> 2012-09-02 16:43 <a href="http://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386801.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Java 闈欐佸唴閮ㄧ被鍜屾灇涓?閾惰仈鏀粯)http://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386776.htmlabinabinSat, 01 Sep 2012 16:03:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386776.htmlhttp://www.aygfsteel.com/stevenjohn/comments/386776.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2012/09/02/386776.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/386776.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/386776.html//榪欓噷闈富瑕佹槸鐢變簬閾惰仈鐨勬墜鏈烘敮浠樼敤鍒頒簡涓涓粯嬈劇殑杞崲鏂瑰紡闂錛屽綋璋冪敤鏀粯瀹濅粯嬈劇殑鏃跺欙紝姣斿涓鍒嗛挶鏍煎紡灝辨槸鎴戜滑鏅氱敤鐨勬牸寮忥細0.01錛岃岄摱鑱旂‘瑕佹妸鏍煎紡杞寲涓?00000000001錛堥摱鑱旇姹傛槸12浣嶏級榪欑鏍煎紡鐨勶紝鏈鍚庝竴浣嶆槸鍒嗭紝鐒跺悗鍚戝墠渚濇鏄錛屽厓錛屽崄鍏冿紝鐧懼厓錛岀浉淇′笉鐢ㄦ垜璇達紝浣犲凡緇忔噦浜嗗惂銆傛墍浠ヨ繖閲屽氨闇瑕佷竴涓漿鎹紝涓轟簡搴斿鍍?000錛?000.00錛?000.0榪欑浼犺繃鏉ョ殑鏍煎紡鐨勬暟鎹紝鐗瑰仛涓涓嬭漿鎹紝鐒跺悗浼犵粰閾惰仈榪涜鏀粯銆?br />

package com.abin.lee.collection.inner;
public class ConsumeFinance {
public static enum Currency{
CHINARMB("chinarmb"),DOLLAR("dollar"),HK("hk");
private Currency(String textVal){
this.textVal=textVal;
}
private String textVal;
public String toString(){
return textVal;
}
}
public static String ConsumeProcess(String money,Currency currency){
System.out.println("currentcy="+currency);
String flag=null;
if(currency.equals(Currency.CHINARMB)){
ConsumeFinance.ChinaProcess chinaProcess=new ConsumeFinance.ChinaProcess();
flag=chinaProcess.chinaChange(money, currency);
}
return flag;
public static String ConsumeProcessReturn(String money,Currency currency){
System.out.println("currentcy="+currency);
String flag=null;
if(currency.equals(Currency.CHINARMB)){
ConsumeFinance.ChinaReturn chinaReturn=new ConsumeFinance.ChinaReturn();
flag=chinaReturn.chinaReChange(money, currency);
}
return flag;
public static class ChinaProcess{
private static String status=null;
public static String chinaChange(String money,Currency currency){
int point=money.lastIndexOf(".");
if(point==-1){
StringBuffer stb=new StringBuffer();
int len=money.length();
int lon=10-len;
for(int i=0;i<lon;i++){
stb.append("0");
}
stb.append(money);
stb.append("00");
status=stb.toString().intern();
}
if(point>0){
int len=money.indexOf(".");
int chn=money.length()-len-1;
switch(chn){
//鍏堝垽鏂璵oney鏍煎紡涓?00.0榪欑鏍煎紡鐨勶紝灝忔暟鐐瑰悗闈㈡湁涓浣嶅皬鏁扮偣鐨勬儏鍐?/div>
case  1:
StringBuffer stb=new StringBuffer(money);
stb.deleteCharAt(len);
int lend=stb.length();
StringBuffer sb=new StringBuffer();
for(int i=0;i<12-lend;i++){
sb.append("0");
}
sb.append(stb.toString());
status=sb.toString().intern();
//鍏堝垽鏂璵oney鏍煎紡涓?00.01榪欑鏍煎紡鐨勶紝灝忔暟鐐瑰悗闈㈡湁涓や綅灝忔暟鐐圭殑鎯呭喌
case 2:
money=money.replace(".", "");
int lende=money.length();
String connect="";
for(int i=0;i<12-lende;i++){
connect+="0";
}
status=connect+money;
}
}
return status;
}
}
//鎶?00000001001杞寲涓?0.01榪欑鏍煎紡
public static class ChinaReturn{
private static String status=null;
public static String chinaReChange(String money,Currency currency){
int change=money.length();
int num=0;
for(int i=0;i<change;i++){
int value=Integer.valueOf(String.valueOf(money.charAt(i)));
if(value>0){
num=i;
break;
}
}
money=money.substring(num);
if(num==10){
status="0."+money;
}
if(num==11){
status="0.0"+money;
}
if(num<10){
System.out.println(money.length());
StringBuffer stb=new StringBuffer(money);
stb.insert(money.length()-2, ".");
status=stb.toString().intern();
}
return status;
}
}
public static void main(String[] args) {
Currency currency=Currency.CHINARMB;
String money="000000001001";
String result=ConsumeProcessReturn(money, currency);
// String result=ConsumeProcess(money, currency);
System.out.println("result="+result);
}
}


abin 2012-09-02 00:03 鍙戣〃璇勮
]]>
Java鍐呴儴綾繪葷粨 http://www.aygfsteel.com/stevenjohn/archive/2012/04/14/374124.htmlabinabinFri, 13 Apr 2012 18:11:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2012/04/14/374124.htmlhttp://www.aygfsteel.com/stevenjohn/comments/374124.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2012/04/14/374124.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/374124.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/374124.html鍐呴儴綾繪槸鎸囧湪涓涓閮ㄧ被鐨勫唴閮ㄥ啀瀹氫箟涓涓被銆傚唴閮ㄧ被浣滀負澶栭儴綾葷殑涓涓垚鍛橈紝騫朵笖渚濋檮浜庡閮ㄧ被鑰屽瓨鍦ㄧ殑銆傚唴閮ㄧ被鍙負闈欐侊紝鍙敤protected鍜宲rivate淇グ錛堣屽閮ㄧ被鍙兘浣跨敤public鍜岀己鐪佺殑鍖呰闂潈闄愶級銆傚唴閮ㄧ被涓昏鏈変互涓嬪嚑綾伙細鎴愬憳鍐呴儴綾匯佸眬閮ㄥ唴閮ㄧ被銆侀潤鎬佸唴閮ㄧ被銆佸尶鍚嶅唴閮ㄧ被

銆銆涓轟粈涔堥渶瑕佸唴閮ㄧ被錛?br />
銆銆鍏稿瀷鐨勬儏鍐墊槸錛屽唴閮ㄧ被緇ф壙鑷煇涓被鎴栧疄鐜版煇涓帴鍙o紝鍐呴儴綾葷殑浠g爜鎿嶄綔鍒涘緩鍏剁殑澶栧洿綾葷殑瀵硅薄銆傛墍浠ヤ綘鍙互璁や負鍐呴儴綾繪彁渚涗簡鏌愮榪涘叆鍏跺鍥寸被鐨勭獥鍙c備嬌鐢ㄥ唴閮ㄧ被鏈鍚稿紩浜虹殑鍘熷洜鏄細

銆銆姣忎釜鍐呴儴綾婚兘鑳界嫭绔嬪湴緇ф壙鑷竴涓紙鎺ュ彛鐨勶級瀹炵幇錛屾墍浠ユ棤璁哄鍥寸被鏄惁宸茬粡緇ф壙浜嗘煇涓紙鎺ュ彛鐨勶級瀹炵幇錛屽浜庡唴閮ㄧ被閮芥病鏈夊獎鍝嶃傚鏋滄病鏈夊唴閮ㄧ被鎻愪緵鐨勫彲浠ョ戶鎵垮涓叿浣撶殑鎴栨娊璞$殑綾葷殑鑳藉姏錛屼竴浜涜璁′笌緙栫▼闂灝卞緢闅捐В鍐熾備粠榪欎釜瑙掑害鐪嬶紝鍐呴儴綾諱嬌寰楀閲嶇戶鎵跨殑瑙e喅鏂規鍙樺緱瀹屾暣銆傛帴鍙hВ鍐充簡閮ㄥ垎闂錛岃屽唴閮ㄧ被鏈夋晥鍦板疄鐜頒簡“澶氶噸緇ф壙”銆?br />
銆銆A錛氭垚鍛樺唴閮ㄧ被

銆銆浣滀負澶栭儴綾葷殑涓涓垚鍛樺瓨鍦紝涓庡閮ㄧ被鐨勫睘鎬с佹柟娉曞茍鍒椼?br />
publicclass Outer {
privatestaticinti = 1;
privateintj = 10;
privateintk = 20;

publicstaticvoidouter_f1() {
}

publicvoidouter_f2() {
}

// 鎴愬憳鍐呴儴綾諱腑錛屼笉鑳藉畾涔夐潤鎬佹垚鍛?br />// 鎴愬憳鍐呴儴綾諱腑錛屽彲浠ヨ闂閮ㄧ被鐨勬墍鏈夋垚鍛?br />class Inner {
// static int inner_i = 100; //鍐呴儴綾諱腑涓嶅厑璁稿畾涔夐潤鎬佸彉閲?br />intj = 100; // 鍐呴儴綾誨拰澶栭儴綾葷殑瀹炰緥鍙橀噺鍙互鍏卞瓨
intinner_i = 1;

void inner_f1() {
System.out.println(i);
//鍦ㄥ唴閮ㄧ被涓闂唴閮ㄧ被鑷繁鐨勫彉閲忕洿鎺ョ敤鍙橀噺鍚?br />System.out.println(j);
//鍦ㄥ唴閮ㄧ被涓闂唴閮ㄧ被鑷繁鐨勫彉閲忎篃鍙互鐢╰his.鍙橀噺鍚?br />System.out.println(this.j);
//鍦ㄥ唴閮ㄧ被涓闂閮ㄧ被涓笌鍐呴儴綾誨悓鍚嶇殑瀹炰緥鍙橀噺鐢ㄥ閮ㄧ被鍚?this.鍙橀噺鍚?br />System.out.println(Outer.this.j);
//濡傛灉鍐呴儴綾諱腑娌℃湁涓庡閮ㄧ被鍚屽悕鐨勫彉閲忥紝鍒欏彲浠ョ洿鎺ョ敤鍙橀噺鍚嶈闂閮ㄧ被鍙橀噺
System.out.println(k);
outer_f1();
outer_f2();
}
}

//澶栭儴綾葷殑闈為潤鎬佹柟娉曡闂垚鍛樺唴閮ㄧ被
publicvoidouter_f3() {
Inner inner = new Inner();
inner.inner_f1();
}

// 澶栭儴綾葷殑闈欐佹柟娉曡闂垚鍛樺唴閮ㄧ被錛屼笌鍦ㄥ閮ㄧ被澶栭儴璁塊棶鎴愬憳鍐呴儴綾諱竴鏍?br />publicstaticvoidouter_f4() {
//step1 寤虹珛澶栭儴綾誨璞?br />Outer out = new Outer();
//step2 鏍規嵁澶栭儴綾誨璞″緩绔嬪唴閮ㄧ被瀵硅薄
Inner inner = out.new Inner();
//step3 璁塊棶鍐呴儴綾葷殑鏂規硶
inner.inner_f1();
}

publicstaticvoid main(String[] args) {
//outer_f4(); //璇ヨ鍙ョ殑杈撳嚭緇撴灉鍜屼笅闈笁鏉¤鍙ョ殑杈撳嚭緇撴灉涓鏍?br />//濡傛灉瑕佺洿鎺ュ垱寤哄唴閮ㄧ被鐨勫璞★紝涓嶈兘鎯沖綋鐒跺湴璁や負鍙渶鍔犱笂澶栧洿綾籓uter鐨勫悕瀛楋紝
//灝卞彲浠ユ寜鐓ч氬父鐨勬牱瀛愮敓鎴愬唴閮ㄧ被鐨勫璞★紝鑰屾槸蹇呴』浣跨敤姝ゅ鍥寸被鐨勪竴涓璞℃潵
//鍒涘緩鍏跺唴閮ㄧ被鐨勪竴涓璞★細
//Outer.Inner outin = out.new Inner()
//鍥犳錛岄櫎闈炰綘宸茬粡鏈変簡澶栧洿綾葷殑涓涓璞★紝鍚﹀垯涓嶅彲鑳界敓鎴愬唴閮ㄧ被鐨勫璞°傚洜涓烘
//鍐呴儴綾葷殑瀵硅薄浼氭倓鎮勫湴閾炬帴鍒板垱寤哄畠鐨勫鍥寸被鐨勫璞°傚鏋滀綘鐢ㄧ殑鏄潤鎬佺殑鍐呴儴綾伙紝
//閭e氨涓嶉渶瑕佸鍏跺鍥寸被瀵硅薄鐨勫紩鐢ㄣ?br />Outer out = new Outer();
Outer.Inner outin = out.new Inner();
outin.inner_f1();
}
}

銆銆娉ㄦ剰錛氬唴閮ㄧ被鏄竴涓紪璇戞椂鐨勬蹇碉紝涓鏃︾紪璇戞垚鍔燂紝灝變細鎴愪負瀹屽叏涓嶅悓鐨勪袱綾匯傚浜庝竴涓悕涓簅uter鐨勫閮ㄧ被鍜屽叾鍐呴儴瀹氫箟鐨勫悕涓篿nner鐨勫唴閮ㄧ被銆傜紪璇戝畬鎴愬悗鍑虹幇outer.class鍜宱uter$inner.class涓ょ被銆?br />
銆銆B:灞閮ㄥ唴閮ㄧ被

銆銆鍦ㄦ柟娉曚腑瀹氫箟鐨勫唴閮ㄧ被縐頒負灞閮ㄥ唴閮ㄧ被銆備笌灞閮ㄥ彉閲忕被浼鹼紝灞閮ㄥ唴閮ㄧ被涓嶈兘鏈夎闂鏄庣錛屽洜涓哄畠涓嶆槸澶栧洿綾葷殑涓閮ㄥ垎錛屼絾鏄畠鍙互璁塊棶褰撳墠浠g爜鍧楀唴鐨勫父閲忥紝鍜屾澶栧洿綾繪墍鏈夌殑鎴愬憳銆?br />
publicclass Outer {
privateints = 100;
privateintout_i = 1;

publicvoid f(finalint k) {
finalint s = 200;
int i = 1;
finalint j = 10;

//瀹氫箟鍦ㄦ柟娉曞唴閮?br />class Inner {
ints = 300; // 鍙互瀹氫箟涓庡閮ㄧ被鍚屽悕鐨勫彉閲?br />
// static int m = 20; //涓嶅彲浠ュ畾涔夐潤鎬佸彉閲?br />Inner(int k) {
inner_f(k);
}

intinner_i = 100;

voidinner_f(int k) {
//濡傛灉鍐呴儴綾繪病鏈変笌澶栭儴綾誨悓鍚嶇殑鍙橀噺錛屽湪鍐呴儴綾諱腑鍙互鐩存帴璁塊棶澶栭儴綾葷殑瀹炰緥鍙橀噺
System.out.println(out_i);
//鍙互璁塊棶澶栭儴綾葷殑灞閮ㄥ彉閲?鍗蟲柟娉曞唴鐨勫彉閲?錛屼絾鏄彉閲忓繀欏繪槸final鐨?br />System.out.println(j);
//System.out.println(i);
//濡傛灉鍐呴儴綾諱腑鏈変笌澶栭儴綾誨悓鍚嶇殑鍙橀噺錛岀洿鎺ョ敤鍙橀噺鍚嶈闂殑鏄唴閮ㄧ被鐨勫彉閲?br />System.out.println(s);
//鐢╰his.鍙橀噺鍚嶈闂殑涔熸槸鍐呴儴綾誨彉閲?br />System.out.println(this.s);
//鐢ㄥ閮ㄧ被鍚?this.鍐呴儴綾誨彉閲忓悕璁塊棶鐨勬槸澶栭儴綾誨彉閲?br />System.out.println(Outer.this.s);
}
}
new Inner(k);
}

publicstaticvoid main(String[] args) {
// 璁塊棶灞閮ㄥ唴閮ㄧ被蹇呴』鍏堟湁澶栭儴綾誨璞?br />Outer out = new Outer();
out.f(3);
}
}

銆銆C錛氶潤鎬佸唴閮ㄧ被(宓屽綾?錛氾紙娉ㄦ剰錛氬墠涓ょ鍐呴儴綾諱笌鍙橀噺綾諱技錛屾墍浠ュ彲浠ュ鐓у弬鑰冨彉閲忥級

銆銆濡傛灉浣犱笉闇瑕佸唴閮ㄧ被瀵硅薄涓庡叾澶栧洿綾誨璞′箣闂存湁鑱旂郴錛岄偅浣犲彲浠ュ皢鍐呴儴綾誨0鏄庝負static銆傝繖閫氬父縐頒負宓屽綾伙紙nested class錛夈傛兂瑕佺悊瑙tatic搴旂敤浜庡唴閮ㄧ被鏃剁殑鍚箟錛屼綘灝卞繀欏昏浣忥紝鏅氱殑鍐呴儴綾誨璞¢殣鍚湴淇濆瓨浜嗕竴涓紩鐢紝鎸囧悜鍒涘緩瀹冪殑澶栧洿綾誨璞°傜劧鑰岋紝褰撳唴閮ㄧ被鏄痵tatic鐨勬椂錛屽氨涓嶆槸榪欐牱浜嗐傚祵濂楃被鎰忓懗鐫錛?br />
銆銆1. 瑕佸垱寤哄祵濂楃被鐨勫璞★紝騫朵笉闇瑕佸叾澶栧洿綾葷殑瀵硅薄銆?br />銆銆2. 涓嶈兘浠庡祵濂楃被鐨勫璞′腑璁塊棶闈為潤鎬佺殑澶栧洿綾誨璞°?br />
publicclass Outer {
privatestaticinti = 1;
privateintj = 10;
publicstaticvoidouter_f1() {
}

publicvoidouter_f2() {
}

// 闈欐佸唴閮ㄧ被鍙互鐢╬ublic,protected,private淇グ
// 闈欐佸唴閮ㄧ被涓彲浠ュ畾涔夐潤鎬佹垨鑰呴潪闈欐佺殑鎴愬憳
staticclass Inner {
staticintinner_i = 100;
intinner_j = 200;
staticvoidinner_f1() {
//闈欐佸唴閮ㄧ被鍙兘璁塊棶澶栭儴綾葷殑闈欐佹垚鍛?鍖呮嫭闈欐佸彉閲忓拰闈欐佹柟娉?
System.out.println("Outer.i" + i);
outer_f1();
}

voidinner_f2() {
// 闈欐佸唴閮ㄧ被涓嶈兘璁塊棶澶栭儴綾葷殑闈為潤鎬佹垚鍛?鍖呮嫭闈為潤鎬佸彉閲忓拰闈為潤鎬佹柟娉?
// System.out.println("Outer.i"+j);
// outer_f2();
}
}

publicvoidouter_f3() {
// 澶栭儴綾昏闂唴閮ㄧ被鐨勯潤鎬佹垚鍛橈細鍐呴儴綾?闈欐佹垚鍛?br />System.out.println(Inner.inner_i);
Inner.inner_f1();
// 澶栭儴綾昏闂唴閮ㄧ被鐨勯潪闈欐佹垚鍛?瀹炰緥鍖栧唴閮ㄧ被鍗沖彲
Inner inner = new Inner();
inner.inner_f2();
}

publicstaticvoid main(String[] args) {
newOuter().outer_f3();
}
}

銆銆鐢熸垚涓涓潤鎬佸唴閮ㄧ被涓嶉渶瑕佸閮ㄧ被鎴愬憳錛氳繖鏄潤鎬佸唴閮ㄧ被鍜屾垚鍛樺唴閮ㄧ被鐨勫尯鍒傞潤鎬佸唴閮ㄧ被鐨勫璞″彲浠ョ洿鎺ョ敓鎴愶細Outer.Inner in = new Outer.Inner(); 鑰屼笉闇瑕侀氳繃鐢熸垚澶栭儴綾誨璞℃潵鐢熸垚銆傝繖鏍峰疄闄呬笂浣塊潤鎬佸唴閮ㄧ被鎴愪負浜嗕竴涓《綰х被(姝e父鎯呭喌涓嬶紝浣犱笉鑳藉湪鎺ュ彛鍐呴儴鏀劇疆浠諱綍浠g爜錛屼絾宓屽綾誨彲浠ヤ綔涓烘帴鍙g殑涓閮ㄥ垎錛屽洜涓哄畠鏄痵tatic 鐨勩傚彧鏄皢宓屽綾葷疆浜庢帴鍙g殑鍛藉悕絀洪棿鍐咃紝榪欏茍涓嶈繚鍙嶆帴鍙g殑瑙勫垯錛?br />
銆銆D錛氬尶鍚嶅唴閮ㄧ被(from thinking in java 3th)

銆銆綆鍗曞湴璇達細鍖垮悕鍐呴儴綾誨氨鏄病鏈夊悕瀛楃殑鍐呴儴綾匯備粈涔堟儏鍐典笅闇瑕佷嬌鐢ㄥ尶鍚嶅唴閮ㄧ被錛熷鏋滄弧瓚充笅闈㈢殑涓浜涙潯浠訛紝浣跨敤鍖垮悕鍐呴儴綾繪槸姣旇緝鍚堥傜殑錛?br />
銆銆·鍙敤鍒扮被鐨勪竴涓疄渚嬨?br />銆銆·綾誨湪瀹氫箟鍚庨┈涓婄敤鍒般?br />銆銆·綾婚潪甯稿皬錛圫UN鎺ㄨ崘鏄湪4琛屼唬鐮佷互涓嬶級
銆銆·緇欑被鍛藉悕騫朵笉浼氬鑷翠綘鐨勪唬鐮佹洿瀹規槗琚悊瑙c?br />銆銆鍦ㄤ嬌鐢ㄥ尶鍚嶅唴閮ㄧ被鏃訛紝瑕佽浣忎互涓嬪嚑涓師鍒欙細
銆銆·鍖垮悕鍐呴儴綾諱笉鑳芥湁鏋勯犳柟娉曘?br />銆銆·鍖垮悕鍐呴儴綾諱笉鑳藉畾涔変換浣曢潤鎬佹垚鍛樸佹柟娉曞拰綾匯?br />銆銆·鍖垮悕鍐呴儴綾諱笉鑳芥槸public,protected,private,static銆?br />銆銆·鍙兘鍒涘緩鍖垮悕鍐呴儴綾葷殑涓涓疄渚嬨?br />·涓涓尶鍚嶅唴閮ㄧ被涓瀹氭槸鍦╪ew鐨勫悗闈紝鐢ㄥ叾闅愬惈瀹炵幇涓涓帴鍙f垨瀹炵幇涓涓被銆?br />銆銆·鍥犲尶鍚嶅唴閮ㄧ被涓哄眬閮ㄥ唴閮ㄧ被錛屾墍浠ュ眬閮ㄥ唴閮ㄧ被鐨勬墍鏈夐檺鍒墮兘瀵瑰叾鐢熸晥銆?br />
銆銆涓嬮潰鐨勪緥瀛愮湅璧鋒潵鏈夌偣濂囨細

//鍦ㄦ柟娉曚腑榪斿洖涓涓尶鍚嶅唴閮ㄧ被
public class Parcel6 {
public Contents cont() {
return new Contents() {
private int i = 11;

public int value() {
return i;
}
}; // 鍦ㄨ繖閲岄渶瑕佷竴涓垎鍙?br />}

public static void main(String[] args) {
Parcel6 p = new Parcel6();
Contents c = p.cont();
}
}

銆銆cont()鏂規硶灝嗕笅闈袱涓姩浣滃悎騫跺湪涓璧鳳細榪斿洖鍊肩殑鐢熸垚錛屼笌琛ㄧず榪欎釜榪斿洖鍊肩殑綾葷殑瀹氫箟錛佽繘涓姝ヨ錛岃繖涓被鏄尶鍚嶇殑錛屽畠娌℃湁鍚嶅瓧銆傛洿緋熺殑鏄紝鐪嬭搗鏉ユ槸浣犳瑕佸垱寤轟竴涓狢ontents瀵硅薄錛?br />
return new Contents()

銆銆浣嗘槸錛屽湪鍒拌揪璇彞緇撴潫鐨勫垎鍙蜂箣鍓嶏紝浣犲嵈璇達細“絳変竴絳夛紝鎴戞兂鍦ㄨ繖閲屾彃鍏ヤ竴涓被鐨勫畾涔?#8221;:

return new Contents() {
private int i = 11;
public int value() { return i; }
};

銆銆榪欑濂囨殑璇硶鎸囩殑鏄細“鍒涘緩涓涓戶鎵胯嚜Contents鐨勫尶鍚嶇被鐨勫璞°?#8221;閫氳繃new 琛ㄨ揪寮忚繑鍥炵殑寮曠敤琚嚜鍔ㄥ悜涓婅漿鍨嬩負瀵笴ontents鐨勫紩鐢ㄣ傚尶鍚嶅唴閮ㄧ被鐨勮娉曟槸涓嬮潰渚嬪瓙鐨勭畝鐣ュ艦寮忥細

class MyContents implements Contents {
private int i = 11;
public int value() { return i; }
}
return new MyContents();

銆銆鍦ㄨ繖涓尶鍚嶅唴閮ㄧ被涓紝浣跨敤浜嗙己鐪佺殑鏋勯犲櫒鏉ョ敓鎴怌ontents銆備笅闈㈢殑浠g爜灞曠ず鐨勬槸錛屽鏋滀綘鐨勫熀綾婚渶瑕佷竴涓湁鍙傛暟鐨勬瀯閫犲櫒錛屽簲璇ユ庝箞鍔烇細

public class Parcel7 {
public Wrapping wrap(int x) {
// Base constructor call:
return new Wrapping(x) { // Pass constructor argument.
public int value() {
return super.value() * 47;
}
}; // Semicolon required
}
public static void main(String[] args) {
Parcel7 p = new Parcel7();
Wrapping w = p.wrap(10);
}
}

銆銆鍙渶綆鍗曞湴浼犻掑悎閫傜殑鍙傛暟緇欏熀綾葷殑鏋勯犲櫒鍗沖彲錛岃繖閲屾槸灝唜 浼犺繘new Wrapping(x)銆傚湪鍖垮悕鍐呴儴綾繪湯灝劇殑鍒嗗彿錛屽茍涓嶆槸鐢ㄦ潵鏍囪姝ゅ唴閮ㄧ被緇撴潫錛圕++涓槸閭f牱錛夈傚疄闄呬笂錛屽畠鏍囪鐨勬槸琛ㄨ揪寮忕殑緇撴潫錛屽彧涓嶈繃榪欎釜琛ㄨ揪寮忔宸у寘鍚簡鍐呴儴綾葷艦浜嗐傚洜姝わ紝榪欎笌鍒殑鍦版柟浣跨敤鐨勫垎鍙鋒槸涓鑷寸殑銆?br />
銆銆濡傛灉鍦ㄥ尶鍚嶇被涓畾涔夋垚鍛樺彉閲忥紝浣犲悓鏍瘋兘澶熷鍏舵墽琛屽垵濮嬪寲鎿嶄綔錛?br />
public class Parcel8 {
// Argument must be final to use inside
// anonymous inner class:
public Destination dest(final String dest) {
return new Destination() {
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel8 p = new Parcel8();
Destination d = p.dest("Tanzania");
}
}

銆銆濡傛灉浣犳湁涓涓尶鍚嶅唴閮ㄧ被錛屽畠瑕佷嬌鐢ㄤ竴涓湪瀹冪殑澶栭儴瀹氫箟鐨勫璞★紝緙栬瘧鍣ㄤ細瑕佹眰鍏跺弬鏁板紩鐢ㄦ槸final 鍨嬬殑錛屽氨鍍廳est()涓殑鍙傛暟銆傚鏋滀綘蹇樿浜嗭紝浼氬緱鍒頒竴涓紪璇戞湡閿欒淇℃伅銆傚鏋滃彧鏄畝鍗曞湴緇欎竴涓垚鍛樺彉閲忚祴鍊鹼紝閭d箞姝や緥涓殑鏂規硶灝卞彲浠ヤ簡銆備絾鏄紝濡傛灉浣犳兂鍋氫竴浜涚被浼兼瀯閫犲櫒鐨勮涓猴紝璇ユ庝箞鍔炲憿錛熷湪鍖垮悕綾諱腑涓嶅彲鑳芥湁宸插懡鍚嶇殑鏋勯犲櫒錛堝洜涓哄畠鏍規湰娌″悕瀛楋紒錛夛紝浣嗛氳繃瀹炰緥鍒濆鍖栵紝浣犲氨鑳藉杈懼埌涓哄尶鍚嶅唴閮ㄧ被“鍒朵綔”涓涓瀯閫犲櫒鐨勬晥鏋溿傚儚榪欐牱鍋氾細

abstract class Base {
public Base(int i) {
System.out.println("Base constructor, i = " + i);
}
public abstract void f();
}

public class AnonymousConstructor {
public static Base getBase(int i) {
return new Base(i) {
{
System.out.println("Inside instance initializer");
}
public void f() {
System.out.println("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
}

銆銆鍦ㄦ渚嬩腑錛屼笉瑕佹眰鍙橀噺i 涓瀹氭槸final 鐨勩傚洜涓篿 琚紶閫掔粰鍖垮悕綾葷殑鍩虹被鐨勬瀯閫犲櫒錛屽畠騫朵笉浼氬湪鍖垮悕綾誨唴閮ㄨ鐩存帴浣跨敤銆備笅渚嬫槸甯﹀疄渚嬪垵濮嬪寲鐨?#8220;parcel”褰㈠紡銆傛敞鎰廳est()鐨勫弬鏁板繀欏繪槸final錛屽洜涓哄畠浠槸鍦ㄥ尶鍚嶇被鍐呰浣跨敤鐨勩?br />
public class Parcel9 {
public Destinationdest(final String dest, final float price) {
return new Destination() {
private int cost;
// Instance initialization for each object:
{
cost = Math.round(price);
if(cost > 100)
System.out.println("Over budget!");
}

private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel9 p = new Parcel9();
Destination d = p.dest("Tanzania", 101.395F);
}
}

銆銆鍦ㄥ疄渚嬪垵濮嬪寲鐨勯儴鍒嗭紝浣犲彲浠ョ湅鍒版湁涓孌典唬鐮侊紝閭e師鏈槸涓嶈兘浣滀負鎴愬憳鍙橀噺鍒濆鍖栫殑涓閮ㄥ垎鑰屾墽琛岀殑錛堝氨鏄痠f 璇彞錛夈傛墍浠ュ浜庡尶鍚嶇被鑰岃█錛屽疄渚嬪垵濮嬪寲鐨勫疄闄呮晥鏋滃氨鏄瀯閫犲櫒銆傚綋鐒跺畠鍙楀埌浜嗛檺鍒訛細浣犱笉鑳介噸杞藉疄渚嬪垵濮嬪寲錛屾墍浠ヤ綘鍙兘鏈変竴涓瀯閫犲櫒銆?br />
銆銆浠庡灞傚祵濂楃被涓闂閮?br />
銆銆涓涓唴閮ㄧ被琚祵濂楀灝戝眰騫朵笉閲嶈錛屽畠鑳介忔槑鍦拌闂墍鏈夊畠鎵宓屽叆鐨勫鍥寸被鐨勬墍鏈夋垚鍛橈紝濡備笅鎵紺猴細

class MNA {
private void f() {}
class A {
private void g() {}
public class B {
void h() {
g();
f();
}
}
}
}
public class MultiNestingAccess {
public static void main(String[] args) {
MNA mna = new MNA();
MNA.A mnaa = mna.new A();
MNA.A.B mnaab = mnaa.new B();
mnaab.h();
}
}

銆銆鍙互鐪嬪埌鍦∕NA.A.B涓紝璋冪敤鏂規硶g()鍜宖()涓嶉渶瑕佷換浣曟潯浠訛紙鍗充嬌瀹冧滑琚畾涔変負private錛夈傝繖涓緥瀛愬悓鏃跺睍紺轟簡濡備綍浠庝笉鍚岀殑綾婚噷闈㈠垱寤哄灞傚祵濂楃殑鍐呴儴綾誨璞$殑鍩烘湰璇硶銆?#8220;.new”璇硶鑳戒駭鐢熸紜殑浣滅敤鍩燂紝鎵浠ヤ綘涓嶅繀鍦ㄨ皟鐢ㄦ瀯閫犲櫒鏃墮檺瀹氱被鍚嶃?br />
銆銆鍐呴儴綾葷殑閲嶈澆闂

銆銆濡傛灉浣犲垱寤轟簡涓涓唴閮ㄧ被錛岀劧鍚庣戶鎵垮叾澶栧洿綾誨茍閲嶆柊瀹氫箟姝ゅ唴閮ㄧ被鏃訛紝浼氬彂鐢熶粈涔堝憿錛熶篃灝辨槸璇達紝鍐呴儴綾誨彲浠ヨ閲嶈澆鍚楋紵榪欑湅璧鋒潵浼間箮鏄釜寰堟湁鐢ㄧ殑鐐瑰瓙錛屼絾鏄?#8220;閲嶈澆”鍐呴儴綾誨氨濂藉儚瀹冩槸澶栧洿綾葷殑涓涓柟娉曪紝鍏跺疄騫朵笉璧蜂粈涔堜綔鐢細

class Egg {
private Yolk y;

protectedclass Yolk {
public Yolk() {
System.out.println("Egg.Yolk()");
}
}

public Egg() {
System.out.println("New Egg()");
y = new Yolk();
}
}

publicclass BigEgg extends Egg {
publicclass Yolk {
public Yolk() {
System.out.println("BigEgg.Yolk()");
}
}

publicstaticvoid main(String[] args) {
new BigEgg();
}
}


銆銆杈撳嚭緇撴灉涓猴細

New Egg()
Egg.Yolk()

銆銆緙虹渷鐨勬瀯閫犲櫒鏄紪璇戝櫒鑷姩鐢熸垚鐨勶紝榪欓噷鏄皟鐢ㄥ熀綾葷殑緙虹渷鏋勯犲櫒銆備綘鍙兘璁や負鏃㈢劧鍒涘緩浜咮igEgg 鐨勫璞★紝閭d箞鎵浣跨敤鐨勫簲璇ユ槸琚?#8220;閲嶈澆”榪囩殑Yolk錛屼絾浣犲彲浠ヤ粠杈撳嚭涓湅鍒板疄闄呮儏鍐靛茍涓嶆槸榪欐牱鐨勩?br />
銆銆榪欎釜渚嬪瓙璇存槑錛屽綋浣犵戶鎵夸簡鏌愪釜澶栧洿綾葷殑鏃跺欙紝鍐呴儴綾誨茍娌℃湁鍙戠敓浠涔堢壒鍒濂囩殑鍙樺寲銆傝繖涓や釜鍐呴儴綾繪槸瀹屽叏鐙珛鐨勪袱涓疄浣擄紝鍚勮嚜鍦ㄨ嚜宸辯殑鍛藉悕絀洪棿鍐呫傚綋鐒訛紝鏄庣‘鍦扮戶鎵挎煇涓唴閮ㄧ被涔熸槸鍙互鐨勶細

class Egg2 {
protected class Yolk {
public Yolk() {
System.out.println("Egg2.Yolk()");
}

public void f() {
System.out.println("Egg2.Yolk.f()");
}
}

private Yolk y = new Yolk();

public Egg2() {
System.out.println("New Egg2()");
}

public void insertYolk(Yolk yy) {
y = yy;
}

public void g() {
y.f();
}
}

public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
public Yolk() {
System.out.println("BigEgg2.Yolk()");
}

public void f() {
System.out.println("BigEgg2.Yolk.f()");
}
}

public BigEgg2() {
insertYolk(new Yolk());
}

public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
}

銆銆杈撳嚭緇撴灉涓猴細

Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()

銆銆鐜板湪BigEgg2.Yolk 閫氳繃extends Egg2.Yolk 鏄庣‘鍦扮戶鎵夸簡姝ゅ唴閮ㄧ被錛屽茍涓旈噸杞戒簡鍏朵腑鐨勬柟娉曘侲gg2 鐨刬nsertYolk()鏂規硶浣垮緱BigEgg2 灝嗗畠鑷繁鐨刌olk 瀵硅薄鍚戜笂杞瀷錛岀劧鍚庝紶閫掔粰寮曠敤y銆傛墍浠ュ綋g()璋冪敤y.f()鏃訛紝閲嶈澆鍚庣殑鏂扮増鐨刦()琚墽琛屻傜浜屾璋冪敤Egg2.Yolk()鏄疊igEgg2.Yolk 鐨勬瀯閫犲櫒璋冪敤浜嗗叾鍩虹被鐨勬瀯閫犲櫒銆傚彲浠ョ湅鍒板湪璋冪敤g()鐨勬椂鍊欙紝鏂扮増鐨刦()琚皟鐢ㄤ簡銆?br />
銆銆鍐呴儴綾葷殑緇ф壙闂錛坱hinking in java 3th p294錛?br />
銆銆鍥犱負鍐呴儴綾葷殑鏋勯犲櫒瑕佺敤鍒板叾澶栧洿綾誨璞$殑寮曠敤錛屾墍浠ュ湪浣犵戶鎵夸竴涓唴閮ㄧ被鐨勬椂鍊欙紝浜嬫儏鍙樺緱鏈夌偣澶嶆潅銆傞棶棰樺湪浜庯紝閭d釜“縐樺瘑鐨?#8221;澶栧洿綾誨璞$殑寮曠敤蹇呴』琚垵濮嬪寲錛岃屽湪琚戶鎵跨殑綾諱腑騫朵笉瀛樺湪瑕佽仈鎺ョ殑緙虹渷瀵硅薄銆傝瑙e喅榪欎釜闂錛岄渶浣跨敤涓撻棬鐨勮娉曟潵鏄庣‘璇存竻瀹冧滑涔嬮棿鐨勫叧鑱旓細

class WithInner {
class Inner {
Inner(){
System.out.println("this is a constructor in WithInner.Inner");
};
}
}

public class InheritInner extends WithInner.Inner {
// ! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
System.out.println("this is a constructor in InheritInner");
}

public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}

銆銆杈撳嚭緇撴灉涓猴細

this is a constructor in WithInner.Inner
this is a constructor in InheritInner

銆銆鍙互鐪嬪埌錛孖nheritInner 鍙戶鎵胯嚜鍐呴儴綾伙紝鑰屼笉鏄鍥寸被銆備絾鏄綋瑕佺敓鎴愪竴涓瀯閫犲櫒鏃訛紝緙虹渷鐨勬瀯閫犲櫒騫朵笉綆楀ソ錛岃屼笖浣犱笉鑳藉彧鏄紶閫掍竴涓寚鍚戝鍥寸被瀵硅薄鐨勫紩鐢ㄣ傛澶栵紝浣犲繀欏誨湪鏋勯犲櫒鍐呬嬌鐢ㄥ涓嬭娉曪細

enclosingClassReference.super();

銆銆榪欐牱鎵嶆彁渚涗簡蹇呰鐨勫紩鐢紝鐒跺悗紼嬪簭鎵嶈兘緙栬瘧閫氳繃銆?br />


http://blog.sina.com.cn/s/blog_56898c310100a3i3.html

abin 2012-04-14 02:11 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 调兵山市| 杭锦后旗| 西昌市| 石城县| 邓州市| 漳浦县| 改则县| 图们市| 万山特区| 湾仔区| 天津市| 集安市| 十堰市| 昆明市| 南华县| 石首市| 东阳市| 新民市| 泽普县| 太保市| 财经| 万盛区| 嵊泗县| 洪洞县| 阿拉善左旗| 揭东县| 全南县| 宁城县| 双江| 马尔康县| 神农架林区| 上杭县| 易门县| 北票市| 钟祥市| 林芝县| 贺兰县| 交城县| 南和县| 阳城县| 盐城市|