

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、<p> 附錄A 外文翻譯—原文部分</p><p> Everything Is an Object</p><p> “If we spoke a different language, we would perceive a some what different world.” Ludwig Wittgenstein (1889-1951)Although it i
2、s based on C++, Java is more of a “pure” object-oriented language.</p><p> Both C++ and Java are hybrid languages, but in Java the designers felt that the hybridization was not as important as it was in C++
3、. A hybrid language allows multiple programming styles; the reason C++ is hybrid is to support backward compatibility with the C language. Because C++ is a superset of the C language, it includes many of that language’s
4、undesirable features, which can make some aspects of C++ overly complicated.</p><p> The Java language assumes that you want to do only object-oriented programming. This means that before you can begin you
5、must shift your mindset into an object-oriented world (unless it’s already there). The benefit of this initial effort is the ability to program in a language that is simpler to learn and to use than many other OOP langua
6、ges. In this chapter you’ll see the basic components of a Java program and learn that (almost) everything in Java is an object.</p><p> You manipulate objects with references</p><p> Each prog
7、ramming language has its own means of manipulating elements in memory. Sometimes the programmer must be constantly aware of what type of manipulation is going on. Are you manipulating the element directly, or are you dea
8、ling with some kind of indirect representation (a pointer in C or C++) that must be treated with a special syntax?</p><p> All this is simplified in Java. You treat everything as an object, using a single c
9、onsistent syntax. Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object. You might imagine a television (the object) and a remote control 1(the reference). As l
10、ong as you’re holding this reference, you have a connection to the television, but when someone says, “Change the channel” or “Lower the volume,” what you’re manipulating is the reference, which in</p><p>
11、Also, the remote control can stand on its own, with no television. That is, just because you have a reference doesn’t mean there’s necessarily an object connected to it. So if you want to hold a word or sentence, you cre
12、ate a String reference:</p><p> But here you’ve created only the reference, not an object. If you decided to send a message to s at this point, you’ll get an error because s isn’t actually attached to anyth
13、ing (there’s no television). A safer practice, then, is always to initialize a reference when you create it:String s = "asdf";However, this uses a special Java feature: Strings can be initialized with quoted te
14、xt. Normally, you must use a more general type of initialization for objects.</p><p> Where storage lives</p><p> It’s useful to visualize some aspects of how things are laid out while the pro
15、gram is running—in particular how memory is arranged. There are five different places to store data:</p><p> 1. Registers. This is the fastest storage because it exists in a place different from that of ot
16、her storage: inside the processor. However, the number of registers is severely limited, so registers are allocated as they are needed. You don’t have direct control, nor do you see any evidence in your programs that reg
17、isters even exist (C & C++, on the other hand, allow you to suggest register allocation to the compiler).</p><p> 2. The stack. This lives in the general random-access memory (RAM) area, but has direct
18、 support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to
19、 registers. The Java system must know, while it is creating the program, the exact lifetime of all the items that are stored on the stack. This constraint places limits on the flexibility of y</p><p> Speci
20、al case: primitive types</p><p> One group of types, which you’ll use quite often in your programming, gets special treatment. You can think of these as “primitive” types. The reason for the special treatme
21、nt is that to create an object with new—especially a small, simple variable—isn’t very efficient, because new places objects on the heap. For these types Java falls back on the approach taken by C and C++. That is, inste
22、ad of creating the variable by using new, an “automatic” variable is created that is not a reference. The v</p><p> Java determines the size of each primitive type. These sizes don’t change from one machine
23、 architecture to another as they do in most languages. This size invariance is one reason Java programs are more portable than programs in most other languages.All numeric types are signed, so don’t look for unsigned typ
24、es.The size of the boolean type is not explicitly specified; it is only defined to be able to take the literal values true or false.The “wrapper” classes for the primitive data types allow y</p><p> char c
25、= ‘x’;</p><p> Character ch = new Character(c);</p><p> Or you could also use:</p><p> Character ch = new Character(‘x’);</p><p> Java SE5 autoboxing will automatic
26、ally convert from a primitive to a wrapper type:</p><p> Character ch = ‘x’;</p><p> and back:char c = ch;</p><p> The reasons for wrapping primitives will be shown in a later ch
27、apter.</p><p> High-precision numbers</p><p> Java includes two classes for performing high-precision arithmetic: BigInteger and BigDecimal. Although these approximately fit into the same cate
28、gory as the “wrapper” classes, neither one has a primitive analogue.Both classes have methods that provide analogues for the operations that you perform on primitive types. That is, you can do anything with a BigInteger
29、or BigDecimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s mo</p><p> BigInteger supports arbitrary-precision integers. This means th
30、at you can accurately represent integral values of any size without losing any information during operations.</p><p> BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurat
31、e monetary calculations, for example.</p><p> Consult the JDK documentation for details about the constructors and methods you can call for these two classes.</p><p> You never need todestroy
32、an object</p><p> In most programming languages, the concept of the lifetime of a variable occupies a significant portion of the programming effort. How long does the variable last? If you are supposed to d
33、estroy it, when should you? Confusion over variable lifetimes can lead to a lot of bugs, and this section shows how Java greatly simplifies the issue by doing all the cleanup work for you.</p><p> 附錄B 外文翻譯—
34、譯文部分</p><p><b> 一切都是對(duì)象</b></p><p> “如果我們說(shuō)另一種不同的語(yǔ)言,那么我們就會(huì)發(fā)覺(jué)一個(gè)有些不同的世界”。</p><p> —Ludwig Wittgenstein(1889-1951)</p><p> “盡管以C++為基礎(chǔ),但Java是一種更純粹的面向?qū)ο蟪绦蛟O(shè)計(jì)語(yǔ)言”
35、。</p><p> 無(wú)論C++還是Java都屬于雜合語(yǔ)言。但在Java中,設(shè)計(jì)者覺(jué)得這種雜合并不像在C++里那么重要。雜合語(yǔ)言允許采用多種編程風(fēng)格;之所以說(shuō)C++是一種雜合語(yǔ)言,是因?yàn)樗С峙cC語(yǔ)言的向后兼容能力。由于C++是C的一個(gè)超集,所以包含的許多特性都是后者不具備的,這些特性使C++在某些地方顯得過(guò)于復(fù)雜。</p><p> Java語(yǔ)言首先便假定了我們只希望進(jìn)行面向?qū)ο蟮某?/p>
36、序設(shè)計(jì)。也就是說(shuō),正式用它設(shè)計(jì)之前,必須先將自己的思想轉(zhuǎn)入一個(gè)面向?qū)ο蟮氖澜纾ǔ窃缫蚜?xí)慣了這個(gè)世界的思維方式)。只有做好這個(gè)準(zhǔn)備工作,與其他OOP語(yǔ)言相比,才能體會(huì)到Java的易學(xué)易用。在本章,我們將探討Java程序的基本組件,并體會(huì)為什么說(shuō)Java乃至Java程序內(nèi)的一切都是對(duì)象。</p><p><b> 用句柄操縱對(duì)象</b></p><p> 每種編程語(yǔ)
37、言都有自己的數(shù)據(jù)處理方式。有些時(shí)候,程序員必須時(shí)刻留意準(zhǔn)備處理的是什么類(lèi)型。您曾利用一些特殊語(yǔ)法直接操作過(guò)對(duì)象,或處理過(guò)一些間接表示的對(duì)象嗎(C或C++里的指針)?</p><p> 所有這些在Java里都得到了簡(jiǎn)化,任何東西都可看作對(duì)象,。因此,我們可采用一種統(tǒng)一的語(yǔ)法,任何地方均可照搬不誤。但要注意,盡管一切都“看作”對(duì)象,但操縱的標(biāo)識(shí)符實(shí)際是指向一個(gè)對(duì)象的“句柄”(Handle)。在其他Java參考書(shū)里,
38、還可看到有的人將其稱(chēng)作一個(gè)“引用”,甚至一個(gè)“指針”。可將這一情形想象成用遙控板(句柄)操縱電視機(jī)(對(duì)象)。只要握住這個(gè)遙控板,就相當(dāng)于掌握了與電視機(jī)連接的通道。但一旦需要“換頻道”或者“關(guān)小聲音”,我們實(shí)際操縱的是遙控板(句柄),再有遙控板自己操縱電視機(jī)(對(duì)象)。如果要在房間里四處走走,并想保持對(duì)電視機(jī)的控制,那么手上拿著的是遙控板,而非電視機(jī)。</p><p> 此外,即使沒(méi)有電視機(jī),遙控板亦可獨(dú)立存在。也
39、就是說(shuō),只是由于擁有一個(gè)句柄,并不表示必須有一個(gè)對(duì)象同它連接。所以如果想容納一個(gè)詞或句子,可創(chuàng)建一個(gè)String句柄:</p><p><b> String s;</b></p><p> 但這里創(chuàng)建的只是句柄,并不是對(duì)象。若此時(shí)向s發(fā)送一條消息,就會(huì)獲得一個(gè)錯(cuò)誤(運(yùn)行期)。</p><p> 這是由于s實(shí)際并未與任何東西連接(即“沒(méi)有
40、電視機(jī)”)。因此,一種更安全的做法是:創(chuàng)建一個(gè)句柄時(shí),記住無(wú)論如何都進(jìn)行初始化:</p><p> String s = “zyp”;</p><p> 然而,這里用到了Java語(yǔ)言的一個(gè)特性:字串可以用加引號(hào)的文本初始化。通常,必須為對(duì)象使用一種更通用的初始化方法。</p><p><b> 存儲(chǔ)到什么地方</b></p>
41、<p> 程序運(yùn)行時(shí),我們最好對(duì)數(shù)據(jù)保存到什么地方做到心中有數(shù)。特別要注意的是內(nèi)存的分配。有六個(gè)地方都可以保存數(shù)據(jù):</p><p> ?。?)寄存器。這是最快的保存區(qū)域,因?yàn)樗挥诓煌谄渌鎯?chǔ)區(qū)的地方——處理器內(nèi)部。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。我們對(duì)此沒(méi)有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡(另一方面,C和C++允許您向編譯器建議寄存器的
42、分配方式)。</p><p> ?。?)堆棧。駐留于常規(guī)RAM(隨機(jī)訪問(wèn)存儲(chǔ)器)區(qū)域,但可通過(guò)它的“堆棧指針”獲得處理的直接支持。堆棧指針若向下移,會(huì)創(chuàng)建新的內(nèi)存;若向上移,則會(huì)釋放那些內(nèi)存。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。創(chuàng)建程序時(shí),Java編譯器必須準(zhǔn)確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長(zhǎng)度”以及“存在時(shí)間”。這是由于它必須生成相應(yīng)的代碼,以便向上和向下移動(dòng)指針。這一限制無(wú)疑影響了程序的靈活
43、性,所以盡管有些Java數(shù)據(jù)要保存在堆棧里——特別是對(duì)象句柄,但Java對(duì)象并不存儲(chǔ)于其中。</p><p> ?。?)堆。一種通用的內(nèi)存池(也在RAM區(qū)域),用于存放所有的Java對(duì)象。和堆棧不同,“內(nèi)存堆”或“堆”(Heap)最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲(chǔ)空間,也不必知道存儲(chǔ)的數(shù)據(jù)要在堆里停留多長(zhǎng)時(shí)間。因此,用堆保存數(shù)據(jù)時(shí)會(huì)得到更大的靈活性。要求創(chuàng)建一個(gè)對(duì)象時(shí),只需要用new命令編制相關(guān)
44、的代碼即可。執(zhí)行這些代碼時(shí),會(huì)在堆里自動(dòng)進(jìn)行數(shù)據(jù)的保存。當(dāng)然,為達(dá)到這種靈活性,必然付出一定的代價(jià):在堆里分配存儲(chǔ)空間時(shí)會(huì)花掉更長(zhǎng)的時(shí)間(如果確實(shí)可以在Java中像在C++中一樣在棧中創(chuàng)建對(duì)象)。</p><p> (4)常量存儲(chǔ)。常數(shù)值通常直接置于程序代碼內(nèi)部。這樣做是安全的,因?yàn)樗麄冇肋h(yuǎn)都不會(huì)改變。有的常數(shù)需要嚴(yán)格地保護(hù),所以可考慮將他們置入只讀存儲(chǔ)器(ROM)。</p><p>
45、?。?)非RAM存儲(chǔ)。若數(shù)據(jù)完全獨(dú)立于一個(gè)程序之外,則程序不運(yùn)行時(shí)仍可存在,并在程序的控制范圍之外。其中兩個(gè)最重要的例子便是“流式對(duì)象”和“持久化對(duì)象”。對(duì)于流式對(duì)象,對(duì)象會(huì)轉(zhuǎn)化成字節(jié)流,通常會(huì)發(fā)給另一臺(tái)機(jī)器。而對(duì)于持久化對(duì)象,對(duì)象保存在磁盤(pán)中。即使程序中止運(yùn)行,他們?nèi)钥杀3肿约旱臓顟B(tài)不變。對(duì)于這些類(lèi)型的數(shù)據(jù)存儲(chǔ),一個(gè)特別有用的技巧就是它們能存在于其他媒體中。一旦需要,甚至能將他們恢復(fù)成普通的、基于RAM的對(duì)象。Java 提供了對(duì)輕量級(jí)
46、持久化的支持,而諸如JDBC和Hibernate這樣的機(jī)制提供了更加復(fù)雜的對(duì)數(shù)據(jù)庫(kù)中存儲(chǔ)和讀取對(duì)象信息的支持。</p><p><b> 特例:基本類(lèi)型</b></p><p> 在程序設(shè)計(jì)中經(jīng)常用到一系列類(lèi)型,他們需要特殊對(duì)待??梢园阉麄兿胂癯伞盎尽鳖?lèi)型。之所以特殊對(duì)待,是因?yàn)閚ew將對(duì)象存儲(chǔ)在“堆”里,故用new創(chuàng)建一個(gè)對(duì)象——特別是小的、簡(jiǎn)單的變量,往往不
47、是很有效。因此,對(duì)于這些類(lèi)型,Java采取與C和C++相同的方法。也就是說(shuō),不用new來(lái)創(chuàng)建變量,而是創(chuàng)建一個(gè)并非是引用的“自動(dòng)”變量。這個(gè)變量直接存儲(chǔ)“值”,并置于堆棧中,因此更加高效。</p><p> Java要確定每種基本類(lèi)型所占存儲(chǔ)空間的大小。他們的大小并不像其他大多數(shù)語(yǔ)言那樣隨機(jī)器硬件架構(gòu)的變化而變化。這種所占存儲(chǔ)空間大小的不變性是Java程序比用其他大多數(shù)語(yǔ)言編寫(xiě)的程序更具可移植性的原因。所有數(shù)值
48、類(lèi)型都有正負(fù)號(hào),所以不要去尋找無(wú)符號(hào)的數(shù)值類(lèi)型。Boolean類(lèi)型所占存儲(chǔ)空間的大小沒(méi)有明確指定,僅定義為能夠取字面值true或false?;绢?lèi)型具有的包裝器類(lèi),使得可以在堆中創(chuàng)建一個(gè)非基本對(duì)象,用來(lái)表示對(duì)應(yīng)的基本類(lèi)型。例如:</p><p> Char c = ‘x’;</p><p> Character ch = new Character(c);</p><
49、;p><b> 也可以這樣用:</b></p><p> Character ch = new Character(‘x’);</p><p> 包裝基本類(lèi)型的原因?qū)⒃谝院蟮恼鹿?jié)中說(shuō)明。</p><p><b> 高精度數(shù)字</b></p><p> Java提供了兩個(gè)用于高精度計(jì)算
50、的類(lèi):BigInteger 或 BigDecimal。雖然它們大體上屬于“包裝器類(lèi)”的范疇,但二者都沒(méi)有對(duì)應(yīng)的基本類(lèi)型。</p><p> 不過(guò),這兩個(gè)類(lèi)包含的方法,提供的操作與對(duì)基本類(lèi)型所能執(zhí)行的操作相識(shí)。也就是說(shuō),能作用于int或float的操作,也能作用于BigInteger或Big Decimal。</p><p> 只不過(guò)必須以方法調(diào)用方式取代運(yùn)算符方式來(lái)實(shí)現(xiàn)。由于這么做復(fù)雜
51、了許多,所以運(yùn)算速度會(huì)比較慢。在這里,我們以速度換取了精度。</p><p> BigInteger支持任意精度的整數(shù)。也就是說(shuō),在運(yùn)算中,可以準(zhǔn)確地表示任何大小的整數(shù)值,而不會(huì)丟失任何信息。</p><p> BigDecimal支持任何精度的定點(diǎn)數(shù),例如,可以用它進(jìn)行精確的貨幣計(jì)算。</p><p> 關(guān)于調(diào)用這兩個(gè)類(lèi)的構(gòu)造器和方法的詳細(xì)信息,請(qǐng)查閱JDK
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 面向?qū)ο笤O(shè)計(jì)外文翻譯 (2)
- 面向?qū)ο蟪绦蛟O(shè)計(jì)外文翻譯2
- 面向?qū)ο笾型馕姆g
- java的面向?qū)ο缶幊掏馕馁Y料翻譯
- 面向?qū)ο笤O(shè)計(jì)
- 面向?qū)ο笤O(shè)計(jì)類(lèi)和對(duì)象
- 外文翻譯---面向?qū)ο蟮囊簤合到y(tǒng)分析研究
- 網(wǎng)頁(yè)設(shè)計(jì)外文翻譯---使用xmlhttprequest對(duì)象
- 外文翻譯-對(duì)象技術(shù)
- 面向?qū)ο笤O(shè)計(jì)原則
- 計(jì)算機(jī)畢業(yè)論文外文翻譯---面向?qū)ο蠛蚦++
- 計(jì)算機(jī)系畢業(yè)設(shè)計(jì)外文翻譯---面向?qū)ο髷?shù)據(jù)庫(kù)系統(tǒng)
- 外文翻譯-機(jī)械模具自動(dòng)化【期刊】支持機(jī)床系統(tǒng)的面向?qū)ο笤O(shè)計(jì)-中英全
- 軟件數(shù)據(jù)庫(kù)的面向?qū)ο蟮囊暯峭馕奈墨I(xiàn)翻譯
- 《面向?qū)ο蟪绦蛟O(shè)計(jì)》
- 面向?qū)ο蠛兔嫦驅(qū)ο蟮母呒?jí)概念
- 《面向?qū)ο蟪绦蛟O(shè)計(jì)(java)》
- 面向?qū)ο笳n程設(shè)計(jì)報(bào)告
- 面向?qū)ο蟮姆治龊驮O(shè)計(jì)
評(píng)論
0/150
提交評(píng)論