锘??xml version="1.0" encoding="utf-8" standalone="yes"?>99成人精品,久久99国内精品,精品国产免费久久久久久尖叫http://www.aygfsteel.com/shaojiahao/category/42743.htmlMickey's Java roadzh-cnMon, 16 Nov 2009 04:56:02 GMTMon, 16 Nov 2009 04:56:02 GMT60銆愯漿銆慗ava:璋堣皥protected璁塊棶鏉冮檺http://www.aygfsteel.com/shaojiahao/archive/2009/11/16/302517.htmlMickey.ShaoMickey.ShaoMon, 16 Nov 2009 04:46:00 GMThttp://www.aygfsteel.com/shaojiahao/archive/2009/11/16/302517.htmlhttp://www.aygfsteel.com/shaojiahao/comments/302517.htmlhttp://www.aygfsteel.com/shaojiahao/archive/2009/11/16/302517.html#Feedback0http://www.aygfsteel.com/shaojiahao/comments/commentRss/302517.htmlhttp://www.aygfsteel.com/shaojiahao/services/trackbacks/302517.html鏉ヨ皥璋坧rotected璁塊棶鏉冮檺闂銆傜湅涓嬮潰紺轟緥1錛?/h1>

銆銆Test.java

class MyObject {}
public class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile error.
}
}

銆銆姝ゆ椂鍑虹幇涓婃枃鎻愬埌鐨勯敊璇細(xì)The method clone from the type Object is not visiuable.

 鎴戜滑宸茬粡娓呮Object.clone()鏄痯rotected鏂規(guī)硶銆傝繖璇存槑錛岃鏂規(guī)硶鍙互琚悓鍖咃紙java.lang錛変笅鍜屽畠錛坖ava.lang.Object錛夌殑瀛愮被璁塊棶銆傝繖閲屾槸MyObject綾伙紙榛樿緇ф壙java.lang.Object錛夈?/clk>

銆銆鍚屾牱Test涔熸槸java.lang.Object鐨勫瓙綾匯備絾鏄紝涓嶈兘鍦ㄤ竴涓瓙綾諱腑璁塊棶鍙︿竴涓瓙綾葷殑protected鏂規(guī)硶錛屽敖綆¤繖涓や釜瀛愮被緇ф壙鑷悓涓涓埗綾匯?/p>

銆銆鍐嶇湅紺轟緥2錛?/p>

銆銆Test2.java

class MyObject2 {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test2 {
public static void main(String[] args) throws CloneNotSupportedException {
MyObject2 obj = new MyObject2();
obj.clone(); // Compile OK.
}
}

銆銆榪欓噷錛屾垜浠湪MyObject2綾諱腑瑕嗙洊錛坥verride錛夌埗綾葷殑clone錛堬級鏂規(guī)硶錛屽湪鍙︿竴涓被Test2涓皟鐢╟lone錛堬級鏂規(guī)硶錛岀紪璇戦氳繃銆?/p>

銆銆緙栬瘧閫氳繃鐨勫師鍥犳樉鑰屾槗瑙侊紝褰撲綘鍦∕yObject2綾諱腑瑕嗙洊clone錛堬級鏂規(guī)硶鏃訛紝MyObject2綾誨拰Test2綾誨湪鍚屼竴涓寘涓嬶紝鎵浠ユprotected鏂規(guī)硶瀵筎est2綾誨彲瑙併?/p>

鍒嗘瀽鍒拌繖閲岋紝鎴戜滑鍦ㄥ洖蹇嗕竴涓婮ava涓殑嫻呭鍒朵笌娣卞鍒舵枃涓紝绔犺妭2.2涓殑澹版槑錛屸憽鍦ㄦ淳鐢熺被涓鐩栧熀綾葷殑clone()鏂規(guī)硶錛屽茍澹版槑涓簆ublic銆? 鐜板湪鏄庣櫧榪欏彞璇濈殑鍘熷洜浜嗗惂錛堜負(fù)浜嗚鍏跺畠綾昏兘璋冪敤榪欎釜綾葷殑clone()鏂規(guī)硶錛岄噸杞戒箣鍚庤鎶奵lone()鏂規(guī)硶鐨勫睘鎬ц緗負(fù)public錛夈?/p>

銆銆涓嬮潰鍐嶆潵鐪嬬ず渚?錛?/p>

銆銆Test3.java

package 1
class MyObject3 {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

銆銆package 2

public class Test3 extends MyObject3 {
public static void main(String args[]) {
MyObject3 obj = new MyObject3();
obj.clone(); // Compile error.
Test3 tobj = new Test3();
tobj.clone();// Complie OK.
}
}

銆銆榪欓噷鎴戠敤Test3綾葷戶鎵縈yObject3錛屾敞鎰忚繖涓や釜綾繪槸涓嶅悓鍖呯殑錛屽惁鍒欏氨鏄ず渚?鐨勬儏褰€傚湪Test3綾諱腑璋冪敤Test3綾葷殑瀹炰緥tobj鐨刢lone錛堬級鏂規(guī)硶錛岀紪璇戦氳繃銆傝屽悓鏍瘋皟鐢∕yObject3綾葷殑瀹炰緥obj鐨刢lone錛堬級鏂規(guī)硶錛岀紪璇戦敊璇紒

銆銆鎰忔兂涓嶅埌鐨勭粨鏋滐紝protected鏂規(guī)硶涓嶆槸鍙互琚戶鎵跨被璁塊棶鍚楋紵

銆銆蹇呴』鏄庣‘錛岀被Test3紜疄鏄戶鎵夸簡綾籑yObject3錛堝寘鎷畠鐨刢lone鏂規(guī)硶錛夛紝鎵浠ュ湪綾籘est3涓彲浠ヨ皟鐢ㄨ嚜宸辯殑clone鏂規(guī)硶銆備絾綾籑yObject3鐨刾rotected鏂規(guī)硶瀵瑰叾涓嶅悓鍖呭瓙綾籘est3鏉ヨ錛屾槸涓嶅彲瑙佺殑銆?/p>

銆銆榪欓噷鍐嶇粰鍑恒妀ava in a nutshell銆嬩腑鐨勪竴孌佃瘽錛?/p>

protected access requires a little more elaboration. Suppose class A declares a protected field x and is extended by a class B, which is defined in a different package (this last point is important). Class B inherits the protected field x, and its code can access that field in the current instance of B or in any other instances of B that the code can refer to. This does not mean, however, that the code of class B can start reading the protected fields of arbitrary instances of A! If an object is an instance of A but is not an instance of B, its fields are obviously not inherited by B, and the code of class B cannot read them.

銆銆欏轟究璇翠袱鍙ワ紝鍥藉唴鐨勫緢澶欽ava涔︾睄鍦ㄤ粙緇嶈闂潈闄愭椂錛屼竴鑸兘榪欐牱鎻忚堪錛堝艦寮忓悇寮傦紝鍐呭涓鑷達(dá)級錛?/p>

銆銆鏂規(guī)硶鐨勮闂帶鍒訛細(xì)


public protected default private
鍚岀被 T T T T
鍚屽寘 T T T
瀛愮被(涓嶅悓鍖? T T

涓嶅悓鍖呬腑鏃犵戶鎵垮叧緋葷殑綾?/td> T


 鎵浠ユ垜鎯寵鐨勬槸錛屽璇諱簺鑻辮鍘熺増涔︾睄銆?/clk>

銆銆鏈枃鍑鴻嚜 “瀛?瀛?#8221; 鍗氬錛岃鍔″繀淇濈暀姝ゅ嚭澶刪ttp://zhangjunhd.blog.51cto.com/113473/19287



Mickey.Shao 2009-11-16 12:46 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 财经| 河西区| 乃东县| 广宗县| 大方县| 临猗县| 福泉市| 托克托县| 黄龙县| 扎鲁特旗| 南靖县| 吉隆县| 绍兴市| 成武县| 丹棱县| 腾冲县| 龙胜| 社会| 盐津县| 合江县| 湘潭县| 沈丘县| 宁德市| 辽源市| 高台县| 汶川县| 汕尾市| 阿鲁科尔沁旗| 婺源县| 南投市| 汶川县| 义乌市| 永昌县| 石门县| 廊坊市| 平潭县| 陇西县| 文水县| 遂溪县| 玛曲县| 铜山县|