版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1第第6講講 面向?qū)ο筇卣髅嫦驅(qū)ο筇卣?(2)武漢大學(xué)國(guó)際軟件學(xué)院2n接口是對(duì)abstract類(lèi)的進(jìn)一步擴(kuò)展n接口中的方法都是未實(shí)現(xiàn)的(類(lèi)似于抽象方法),目的是在實(shí)現(xiàn)接口的類(lèi)之間建立一種協(xié)議n接口中的變量都是常量n定義n一個(gè)類(lèi)符合某個(gè)或一組接口,利用implements6.1 接口 (interface)an interface is a named collection of method definitions (without implementations). an interface can also declare constants. public interface 接口名 成
2、員變量;方法聲明; class 類(lèi)名 implements 接口1, 接口2 3n接口名修飾npublic: 無(wú)任何訪問(wèn)限制n無(wú)修飾: 僅限于本包中n接口變量默認(rèn)都是“public static final”public interface months int january=1, february=2, march=3,april=4, may=5, june=6, july=7,august=8, september=9,october=10,november=11,december=12;6.1.1 接口名修飾46.1.2 接口方法n接口方法n無(wú)修飾n但在實(shí)現(xiàn)接口方法的類(lèi)中,修飾符為p
3、ublicinterface figure double half=0.5,pi=3.14159; void parameter(); void area();class triangle implements figure double b, h; triangle (double u, double v) b = u; h = v; public void parameter() system.out.println(b + “ “ + h); public void area() system.out.println(half*h*b); class circle implements
4、figure double x, y, r; circle(double u, double v, double m) x=u; y=v; r=m; public void parameter() system.out.println(x+“ “+y+“ “+r); public void area() system.out.println(pi*r*r); triangle t = new triangle(2, 3);circle c = new circle(4, 5, 6);figure f = t, c;for (int i =0; i f.length; i+) fi.parame
5、ter();fi.area();56.1.3 接口的繼承 extendsn接口的繼承 extendsn將多個(gè)接口合并為一個(gè)新的接口interface dc int add(int x, int y);interface db extends dc int sub(int x, int y);interface da extends db int mul(int x, int y);interface dy int div(int x, int y);interface dx extends dy int mod(int x, int y);class dd implements da, dx
6、public int add(int x, int y) return x+y; public int sub(int x, int y) return x-y; public int mul(int x, int y) return x*y; public int div(int x, int y) return x/y; public int mod(int x, int y) return x%y; 66.1.4 接口多重繼承n利用接口實(shí)現(xiàn)多重繼承(multiple inheritance)nclass extends 父類(lèi) implements 接口interface canfight
7、 void fight();interface canswim void swim();interface canfly void fly();class actioncharacter public void fight() class hero extends actioncharacter implements canfight, canswim, canfly public void swim() public void fly() 76.1.5 接口合并時(shí)的命名沖突interface a1 void f();interface a2 int f(int i);interface a3
8、 int f();class c public int f() return 4; class c1 implments a1, a2 public void f() public int f(int i) return 5; class c2 extends c implments a2 public int f(int i) return 5; class c3 extends c implments a3 public int f() return 5; class c4 extends c implements a1 /overload/overload/implements and
9、overriding86.1.6 接口繼承中的命名沖突interface basecolors int red = 1, green = 2, blue = 4;interface rainbowcolors extends basecolors int yellow = 3, orange = 5, violet = 6;interface printcolors extends basecolors int yellow = 8, cyan = 16, magenta = 32;interface lotsofcolors extends rainbowcolors, printcolor
10、s int fuchsia = 17, chartreuse = red+90;class dd implements lotsofcolors public static void main(string args) system.out.println(yellow);class dd public static void main(string args) system.out.println(lotsofcolors.yellow);reference to yellow is ambiguous, both variable yellow in rainbowcolors and v
11、ariable yellow in printcolors match9n為什么需要包?n使java類(lèi)更容易發(fā)現(xiàn)和使用n防止命名沖突n進(jìn)行訪問(wèn)控制 n層次結(jié)構(gòu),特定的功能na package is a collection of related classes and interfaces providing access protection and namespace management. nimport6.2 包 (package)10/graphic.java filepublic abstract class graphic . . ./circle.java filepublic
12、 class circle extends graphic implements draggable . . ./rectangle.java filepublic class rectangle extends graphic implements draggable . . ./draggable.java filepublic interface draggable . . .n容易地決定那些類(lèi)和接口是相互關(guān)聯(lián)的n知道從哪里找到提供圖形功能的類(lèi)和接口n由于包建立了一個(gè)新的名字空間,所以你定義的類(lèi)不會(huì)同其他包中的類(lèi)名有沖突n可以容許在同一個(gè)包中無(wú)訪問(wèn)限制,同時(shí)可對(duì)在本包外的訪問(wèn)進(jìn)行限制11
13、n包的創(chuàng)建 package graphics; public class circle extends graphic implements draggable . . .n包的命名n層次結(jié)構(gòu)對(duì)應(yīng)實(shí)際的目錄結(jié)構(gòu)ncom.sun.norg.w3c.norg.jalpha.6.2.1 包的創(chuàng)建12n包的使用n僅僅公共的(public)包成員(類(lèi)、接口)可以在其所定義的包外被訪問(wèn)n三種方式n利用包成員的規(guī)范名(包名+類(lèi)名)n引入(import)包成員名n引入(import)整個(gè)包成員6.2.2 包的使用13n例 package graphics; public class circle extend
14、s graphic implements draggable . . . n利用包成員的規(guī)范名(包名+類(lèi)名) graphics.circle mycir = new graphics.circle();n引入(import)包成員名import graphics.circle; circle mycir = new circle(); n引入(import)整個(gè)包成員import graphics.*; circle mycir = new circle(); 6.2.3 包的引入14n如何防止名字沖突/graphics.circle.classpackage graphics;public
15、class circle . . ./mygraphics.circle.classpackage mygraphics;public class circle . . .6.2.4 防止名字沖突import graphics.*;import mygraphics.*;class test /circle c;import graphics.*;import mygraphics.*;class test graphics.circle c = new graphics.circle();mygraphics.circle c = new mygraphics.circle(); 156.2
16、.5 包與java文件的對(duì)應(yīng)關(guān)系package org.jalpha;public class helloworld . . .根目錄d:src源文件d:srcorgjalphahelloworld.java 編譯cd d:srcjavac orgjalphahelloworld.javaclass文件d:srcorgjalphahelloworld.class 執(zhí)行(在根目錄) cd d:src java org.jalpha.helloworld執(zhí)行(在其他目錄)d: java classpath d:src org.jalpha.helloworld166.2.5 包與java文件的對(duì)應(yīng)
17、關(guān)系package org.aloha;import org.jalpha.*;import org.w3c.*;public class test . . .class文件(org.jalpha) d:src1orgjalpha*.class class文件(org.w3c) d:src2orgw3c*.class test.class文件的根目錄 d:src執(zhí)行(在根目錄) d:src java classpath d:src1;d:src2;. org.aloha.test執(zhí)行(在其他目錄) d: java classpath d:src1;d:src2;d:src org.aloha.
18、test176.3 常用工具類(lèi) njava 2 platform packagesnjavatm 2 platform, standard edition, v 1.4.1 api specificationnjava語(yǔ)言基礎(chǔ)類(lèi)、java類(lèi)庫(kù)n定義描述見(jiàn)java2sdk文檔n135個(gè)包(package)njava.applet; java.awt; java.awt.eventnjava.io; java.lang; njava.sql; java.text; java.utilnjavax.swingn n2723個(gè)類(lèi)(class)和接口(interface)n實(shí)現(xiàn)d:j2sdk1.4.1_
19、01jrelibrt.jar(22.4mb)njar tvf rt.jar | more186.3.1 java.lang包njava.lang包nobject類(lèi)nsystem類(lèi)nmath類(lèi)n基本數(shù)據(jù)類(lèi)型的包裝類(lèi)n字符串操作類(lèi)nstring類(lèi)nstringbuffer類(lèi)nstringtokenizer類(lèi)(java.util包)nruntime類(lèi)196.3.2 java.lang.object類(lèi) nclass object is the root of the class hierarchy. every class has object as a superclass. nall object
20、s, including arrays, implement the methods of this class. n子類(lèi)可以對(duì)object類(lèi)的方法進(jìn)行重寫(xiě)構(gòu)造方法 - public objectobject()實(shí)例方法1.protected object clone() throws clonenotsupportedexception2.public boolean equals(object obj)3.protected void finalize() throws throwable4.public final class getclass()5.public int hashcod
21、e()6.public final void notify()7.public final void notifyall()8.public string tostring()9.public final void wait() throws interruptedexception10. public final void wait(long timeout) throws interruptedexception11. public final void wait(long timeout, int nanos) throws interruptedexception206.3.3 jav
22、a.lang.system類(lèi)n靜態(tài)變量npublic static final inputstream in (標(biāo)準(zhǔn)輸入流)npublic static final printstream out (標(biāo)準(zhǔn)輸出流)npublic static final printstream err (標(biāo)準(zhǔn)錯(cuò)誤輸出流)n靜態(tài)方法npublic static void arraycopy(object src, int srcpos, object dest, int destpos,int length)npublic static void exit(int status)npublic static vo
23、id gc()npublic static long currenttimemillis()n 21n獲取當(dāng)前時(shí)間npublic static long currenttimemillis()nreturns: the difference, measured in milliseconds, between the current time and midnight, january 1, 1970 utc (universal time coordinated).public static void main(string args) long start = system.current
24、timemillis(); long end = system.currenttimemillis();system.out.println(end - start); 226.3.4 java.lang.math類(lèi)n靜態(tài)變量npublic static final double enpublic static final double pin靜態(tài)方法npublic static double abs(double a)npublic static double ceil(double a)npublic static double floor(double a)npublic static
25、double max(double a, double b)n 236.3.5 基本數(shù)據(jù)類(lèi)型的包裝類(lèi) n基本數(shù)據(jù)類(lèi)型: byte, short, int, long, float, double, boolean, char n對(duì)應(yīng)的包裝類(lèi): byte, short, integer, long, float, double, boolean, charactern作用n包裝類(lèi)對(duì)象中包含有一個(gè)對(duì)應(yīng)基本類(lèi)型的值n提供有基本類(lèi)型和字符串(string)之間的轉(zhuǎn)換函數(shù)n定義有一些常數(shù)和方法24n常數(shù)定義byte largestbyte= byte.max_value; /127short large
26、stshort = short.max_value; /32767int largestinteger = integer.max_value; /2147483647long largestlong = long.max_value; /9223372036854775807float largestfloat = float.max_value; /3.40282e+38double largestdouble= double.max_value; /1.79769e+30825n基本類(lèi)型和字符串(string)之間的轉(zhuǎn)換ninteger類(lèi)舉例n字符串轉(zhuǎn)換為整數(shù)npublic static
27、 int parseint(string s) throws numberformatexceptionnpublic static int parseint(string s, int radix) throws numberformatexceptionstring s = “123”;int i = integer.parseint(s);parseint(0, 10) parseint(473, 10) parseint(-0, 10) parseint(-ff, 16) parseint(1100110, 2) parseint(2147483647, 10) parseint(-2
28、147483648, 10) parseint(2147483648, 10) parseint(99, 8) parseint(kona, 10) parseint(kona, 27)returns 0returns 473returns 0returns -255returns 102returns 2147483647returns -2147483648throws a numberformatexceptionthrows a numberformatexceptionthrows a numberformatexceptionreturns 41178726n基本類(lèi)型和字符串(st
29、ring)之間的轉(zhuǎn)換ninteger類(lèi)舉例n整數(shù)轉(zhuǎn)換為字符串npublic static string tostring(int i)npublic static string tostring(int i, int radix)npublic static string tobinarystring(int i)npublic static string tohexstring(int i)npublic static string tooctalstring(int i)int i = 123;string s1 = integer.tostring(i);string s2 = inte
30、ger.tostring(i, 10);string s3 = integer.tostring(i, 2);string s4 = integer.tostring(i, 8);string s5 = integer.tostring(i, 16);string s6 = integer.tobinarystring(i);string s7 = integer.tohexstring(i);string s8 = integer.tooctalstring(i);12312311110111737b11110117b173276.3.6 字符串操作類(lèi)n三個(gè)類(lèi)njava.lang.strin
31、g類(lèi)njava.lang.stringbuffer類(lèi)njava.util.stringtokenizer類(lèi)n不同的應(yīng)用場(chǎng)合28njava.lang.string類(lèi)字符串/字符序列n構(gòu)造方法npublic string() npublic string(byte bytes) npublic string(byte bytes, int offset, int length)npublic string(byte bytes, string charsetname) npublic string(char value) npublic string(char value, int offset,
32、 int count)npublic string(string original) 29njava.lang.string類(lèi)字符串/字符序列n構(gòu)造方法的使用string s = new string();char c = a, b, c;string s = new string(c);char c = 語(yǔ), 言;string s = new string(c);byte b = 97, 98, 99;string s = new string(b);string s = “abc”;string s = “語(yǔ)言”;30njava.lang.string類(lèi)字符串/字符序列n判斷字符串相等的方
33、法npublic boolean equals(object anobject) 判斷是否是同一個(gè)對(duì)象,是ture;然后anobject是否為一個(gè)字符串對(duì)象,否false;判斷是否內(nèi)容相同npublic boolean equalsignorecase(string anotherstring) 判斷邏輯與equals相同,僅僅在判斷內(nèi)容上不考慮大小寫(xiě)npublic int compareto(object o) 若o不是字符串對(duì)象,則拋錯(cuò);若是則調(diào)用下面的函數(shù)npublic int compareto(string anotherstring) 判斷兩個(gè)字符串的內(nèi)容是否相同,是0;否不等于0
34、的整數(shù)npublic int comparetoignorecase(string str) 基本功能同上,僅僅在判斷時(shí)不考慮大小寫(xiě)31njava.lang.string類(lèi)字符串/字符序列n判斷字符串相等的方法string s1 = java語(yǔ)言;string s2 = java語(yǔ)言;system.out.println(s1.equals(s2);system.out.println(s1.equalsignorecase(s2);system.out.println(pareto(s2);system.out.println(paretoignorecase(s2);運(yùn)行結(jié)果:false
35、true 32 032njava.lang.string類(lèi)字符串/字符序列n獲取長(zhǎng)度npublic int length() 字符串的長(zhǎng)度,即包含多少個(gè)字符n獲取特定子串(substring)和字符npublic string substring(int beginindex) npublic string substring(int beginindex, int endindex) nbeginindex: 起始索引位置(包含)nendindex: 結(jié)束索引位置(不包含)npublic char charat(int index) 33njava.lang.string類(lèi)字符串/字符序列n
36、方法舉例string s1 = java語(yǔ)言;string s2 = java語(yǔ)言;system.out.println(s1.length();system.out.println(s2.length();system.out.println(s1.substring(0, 4);system.out.println(s1.substring(4);system.out.println(s2.substring(0, 4);system.out.println(s2.substring(4);system.out.println(s1.charat(0);運(yùn)行結(jié)果:66java語(yǔ)言java語(yǔ)
37、言j34njava.lang.string類(lèi)字符串/字符序列n字符串前綴(prefix)/后綴(suffix)的判斷npublic boolean startswith(string prefix) 判斷字符串是否以一特定的字符串開(kāi)頭npublic boolean startswith(string prefix, int toffset) npublic boolean endswith(string suffix) 判斷字符串是否以一特定的字符串開(kāi)頭system.out.println(java語(yǔ)言.startswith(java);system.out.println(java語(yǔ)言.st
38、artswith(ava, 1);system.out.println(java語(yǔ)言.endswith(語(yǔ)言);35njava.lang.string類(lèi)字符串/字符序列n查詢(xún)特定字符/字符串的位置npublic int indexof(int ch) 該字符在字符串中第一次出現(xiàn)位置的索引值;否則返回-1npublic int indexof(int ch, int fromindex) npublic int indexof(string str) npublic int indexof(string str, int fromindex) npublic int lastindexof(in
39、t ch) npublic int lastindexof(int ch, int fromindex) npublic int lastindexof(string str) npublic int lastindexof(string str, int fromindex) 從前往后從后往前36njava.lang.string類(lèi)字符串/字符序列n方法舉例string s = “java語(yǔ)言”;system.out.println(s.indexof(a);system.out.println(s.indexof(a, 2);system.out.println(s.indexof(“a”
40、);system.out.println(s.indexof(“語(yǔ)言”);system.out.println(s.lastindexof(a);system.out.println(s.lastindexof(v, 1);system.out.println(s.lastindexof(“語(yǔ)言”);system.out.println(s.lastindexof(“v”, 2);運(yùn)行結(jié)果:13143-14237njava.lang.string類(lèi)字符串/字符序列n字符串轉(zhuǎn)變?yōu)閿?shù)組npublic byte getbytes() 將字符串轉(zhuǎn)變?yōu)橐粋€(gè)字節(jié)數(shù)組npublic byte getbyte
41、s(stringcharsetname) throws unsupportedencodingexception 按特定的字符編碼格式將字符串轉(zhuǎn)變?yōu)橐粋€(gè)字節(jié)數(shù)組npublic char tochararray() 將字符串轉(zhuǎn)變?yōu)橐粋€(gè)字符數(shù)組38njava.lang.string類(lèi)字符串/字符序列n方法舉例string s = java語(yǔ)言;char c = s.tochararray();system.out.println(c.length);byte b = s.getbytes();system.out.println(b.length);b = s.getbytes(iso8859-1
42、);system.out.println(b.length);運(yùn)行結(jié)果:686中文windows操作系統(tǒng): 默認(rèn)字符集 gb2312其他系統(tǒng): 默認(rèn)字符集 iso-8859-139njava.lang.string類(lèi)字符串/字符序列n字符串npublic string split(string regex) 40njava.lang.string類(lèi)字符串/字符序列n其他方法npublic string concat(stringstr) 連接字符串 cares.concat(s) returns caress to.concat(get).concat(her) returns togethe
43、r npublic string replace(charoldchar, charnewchar) 在字符串中進(jìn)行字符替換 mesquite in your cellar.replace(e, o) returns mosquito in your collar” jonl.replace(q, x) returns jonl (no change) 41njava.lang.string類(lèi)字符串/字符序列n其他方法npublic string trim() 將字符串頭尾的空格字符刪除npublic string tolowercase() 字符串中字符轉(zhuǎn)為小寫(xiě)npublic string
44、touppercase() 字符串中字符轉(zhuǎn)為大寫(xiě)n一些靜態(tài)方法npublic static string valueof(booleanb) npublic static string valueof(charc) npublic static string valueof(inti) npublic static string valueof(longl) npublic static string valueof(floatf) npublic static string valueof(doubled) 42njava.lang.string類(lèi)字符串/字符序列nquizstring s1
45、 = new string(“java”);string s2 = new string(“java”);system.out.println(s1 = s2);system.out.println(s1.equals(s2);system.out.println(pareto(s2);運(yùn)行結(jié)果:falsetrue 0truetrue0falsetrue0string s3 = “java”;string s4 = “java”;system.out.println(s3 = s4);system.out.println(s3.equals(s4);system.out.println(par
46、eto(s4);system.out.println(s2 = s4);system.out.println(s2.equals(s4);system.out.println(pareto(s4);結(jié)論:1. string s1 = “abc”; 字符串常量對(duì)象 (immutable) string s2 = “abc”; 不同常量對(duì)象共享同一對(duì)象2. 其他字符串對(duì)象則可理解為對(duì)字符串常量對(duì)象進(jìn)行了 一層包裝system.out.println(ern() = s4);true43njava.lang.string類(lèi)字符串/字符序列nquizpackage testpackage;
47、class test public static void main(string args) string hello = hello, lo = lo;system.out.println(hello = hello);system.out.println(other.hello = hello);system.out.println(other.other.hello = hello);system.out.println(hello = (hel+lo);system.out.println(hello = (hel+lo);system.out.println(hello = (he
48、l+lo).intern();class other static string hello = hello; 運(yùn)行結(jié)果:truetrue truetruefalsetruepackage other;public class other static string hello = hello; 結(jié)論:1. strings computed by constant expressions are computed at compile time and then treated as if they were literals. 2. strings computed at run time
49、are newly created and therefore distinct. 44njava.lang.stringbuffer類(lèi)n一個(gè)可變(mutable)/動(dòng)態(tài)的字符序列n構(gòu)造方法npublic stringbuffer() npublic stringbuffer(string str) n主要方法n添加(append)和插入(insert, 指定位置)npublic stringbuffer append(booleanb) npublic stringbuffer insert(intoffset, booleanb) nboolean, char, char, double, float, int, long, stringn轉(zhuǎn)換為字符串 - public string tostring() 45njava.lang.stringbuffer類(lèi)方法舉例string s = java語(yǔ)言;stringbuffer buffer = new stringbuffer(s);buffer.append(“easy”);buffer.insert(6, “ is “);s = buffer.tostri
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 合伙協(xié)議書(shū)和合伙合同
- 2025年粵人版九年級(jí)歷史上冊(cè)月考試卷
- 2025年外研銜接版七年級(jí)物理下冊(cè)月考試卷含答案
- 2025年粵教滬科版九年級(jí)歷史下冊(cè)階段測(cè)試試卷含答案
- 2025年牛津上海版選擇性必修3生物上冊(cè)階段測(cè)試試卷含答案
- 2025年滬科版七年級(jí)生物上冊(cè)階段測(cè)試試卷
- 2025年粵教新版選修四地理下冊(cè)月考試卷
- 2025年滬教版選修歷史下冊(cè)月考試卷
- 2025年滬教新版八年級(jí)歷史下冊(cè)月考試卷含答案
- 二零二五版苗圃場(chǎng)技術(shù)員園藝研發(fā)聘用合同書(shū)4篇
- SYT 6968-2021 油氣輸送管道工程水平定向鉆穿越設(shè)計(jì)規(guī)范-PDF解密
- 冷庫(kù)制冷負(fù)荷計(jì)算表
- 肩袖損傷護(hù)理查房
- 設(shè)備運(yùn)維管理安全規(guī)范標(biāo)準(zhǔn)
- 辦文辦會(huì)辦事實(shí)務(wù)課件
- 大學(xué)宿舍人際關(guān)系
- 2023光明小升初(語(yǔ)文)試卷
- GB/T 14600-2009電子工業(yè)用氣體氧化亞氮
- 申請(qǐng)使用物業(yè)專(zhuān)項(xiàng)維修資金征求業(yè)主意見(jiàn)表
- 房屋買(mǎi)賣(mài)合同簡(jiǎn)單范本 房屋買(mǎi)賣(mài)合同簡(jiǎn)易范本
- 無(wú)抽搐電休克治療規(guī)范
評(píng)論
0/150
提交評(píng)論