第6講向對象特征2_第1頁
第6講向對象特征2_第2頁
第6講向對象特征2_第3頁
第6講向對象特征2_第4頁
第6講向對象特征2_第5頁
已閱讀5頁,還剩46頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、1第第6講講 面向對象特征面向對象特征 (2)武漢大學國際軟件學院2n接口是對abstract類的進一步擴展n接口中的方法都是未實現(xiàn)的(類似于抽象方法),目的是在實現(xiàn)接口的類之間建立一種協(xié)議n接口中的變量都是常量n定義n一個類符合某個或一組接口,利用implements6.1 接口 (interface)an interface is a named collection of method definitions (without implementations). an interface can also declare constants. public interface 接口名 成

2、員變量;方法聲明; class 類名 implements 接口1, 接口2 3n接口名修飾npublic: 無任何訪問限制n無修飾: 僅限于本包中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無修飾n但在實現(xiàn)接口方法的類中,修飾符為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將多個接口合并為一個新的接口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利用接口實現(xiàn)多重繼承(multiple inheritance)nclass extends 父類 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 接口合并時的命名沖突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類更容易發(fā)現(xiàn)和使用n防止命名沖突n進行訪問控制 n層次結構,特定的功能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容易地決定那些類和接口是相互關聯(lián)的n知道從哪里找到提供圖形功能的類和接口n由于包建立了一個新的名字空間,所以你定義的類不會同其他包中的類名有沖突n可以容許在同一個包中無訪問限制,同時可對在本包外的訪問進行限制11

13、n包的創(chuàng)建 package graphics; public class circle extends graphic implements draggable . . .n包的命名n層次結構對應實際的目錄結構ncom.sun.norg.w3c.norg.jalpha.6.2.1 包的創(chuàng)建12n包的使用n僅僅公共的(public)包成員(類、接口)可以在其所定義的包外被訪問n三種方式n利用包成員的規(guī)范名(包名+類名)n引入(import)包成員名n引入(import)整個包成員6.2.2 包的使用13n例 package graphics; public class circle extend

14、s graphic implements draggable . . . n利用包成員的規(guī)范名(包名+類名) graphics.circle mycir = new graphics.circle();n引入(import)包成員名import graphics.circle; circle mycir = new circle(); n引入(import)整個包成員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文件的對應關系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文件的對應

17、關系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 常用工具類 njava 2 platform packagesnjavatm 2 platform, standard edition, v 1.4.1 api specificationnjava語言基礎類、java類庫n定義描述見java2sdk文檔n135個包(package)njava.applet; java.awt; java.awt.eventnjava.io; java.lang; njava.sql; java.text; java.utilnjavax.swingn n2723個類(class)和接口(interface)n實現(xiàn)d:j2sdk1.4.1_

19、01jrelibrt.jar(22.4mb)njar tvf rt.jar | more186.3.1 java.lang包njava.lang包nobject類nsystem類nmath類n基本數(shù)據(jù)類型的包裝類n字符串操作類nstring類nstringbuffer類nstringtokenizer類(java.util包)nruntime類196.3.2 java.lang.object類 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子類可以對object類的方法進行重寫構造方法 - public objectobject()實例方法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類n靜態(tài)變量npublic static final inputstream in (標準輸入流)npublic static final printstream out (標準輸出流)npublic static final printstream err (標準錯誤輸出流)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獲取當前時間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類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ù)類型的包裝類 n基本數(shù)據(jù)類型: byte, short, int, long, float, double, boolean, char n對應的包裝類: byte, short, integer, long, float, double, boolean, charactern作用n包裝類對象中包含有一個對應基本類型的值n提供有基本類型和字符串(string)之間的轉換函數(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基本類型和字符串(string)之間的轉換ninteger類舉例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基本類型和字符串(st

29、ring)之間的轉換ninteger類舉例n整數(shù)轉換為字符串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 字符串操作類n三個類njava.lang.strin

31、g類njava.lang.stringbuffer類njava.util.stringtokenizer類n不同的應用場合28njava.lang.string類字符串/字符序列n構造方法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類字符串/字符序列n構造方法的使用string s = new string();char c = a, b, c;string s = new string(c);char c = 語, 言;string s = new string(c);byte b = 97, 98, 99;string s = new string(b);string s = “abc”;string s = “語言”;30njava.lang.string類字符串/字符序列n判斷字符串相等的方

33、法npublic boolean equals(object anobject) 判斷是否是同一個對象,是ture;然后anobject是否為一個字符串對象,否false;判斷是否內(nèi)容相同npublic boolean equalsignorecase(string anotherstring) 判斷邏輯與equals相同,僅僅在判斷內(nèi)容上不考慮大小寫npublic int compareto(object o) 若o不是字符串對象,則拋錯;若是則調用下面的函數(shù)npublic int compareto(string anotherstring) 判斷兩個字符串的內(nèi)容是否相同,是0;否不等于0

34、的整數(shù)npublic int comparetoignorecase(string str) 基本功能同上,僅僅在判斷時不考慮大小寫31njava.lang.string類字符串/字符序列n判斷字符串相等的方法string s1 = java語言;string s2 = java語言;system.out.println(s1.equals(s2);system.out.println(s1.equalsignorecase(s2);system.out.println(pareto(s2);system.out.println(paretoignorecase(s2);運行結果:false

35、true 32 032njava.lang.string類字符串/字符序列n獲取長度npublic int length() 字符串的長度,即包含多少個字符n獲取特定子串(substring)和字符npublic string substring(int beginindex) npublic string substring(int beginindex, int endindex) nbeginindex: 起始索引位置(包含)nendindex: 結束索引位置(不包含)npublic char charat(int index) 33njava.lang.string類字符串/字符序列n

36、方法舉例string s1 = java語言;string s2 = java語言;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);運行結果:66java語言java語

37、言j34njava.lang.string類字符串/字符序列n字符串前綴(prefix)/后綴(suffix)的判斷npublic boolean startswith(string prefix) 判斷字符串是否以一特定的字符串開頭npublic boolean startswith(string prefix, int toffset) npublic boolean endswith(string suffix) 判斷字符串是否以一特定的字符串開頭system.out.println(java語言.startswith(java);system.out.println(java語言.st

38、artswith(ava, 1);system.out.println(java語言.endswith(語言);35njava.lang.string類字符串/字符序列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類字符串/字符序列n方法舉例string s = “java語言”;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(“語言”);system.out.println(s.lastindexof(a);system.out.println(s.lastindexof(v, 1);system.out.println(s.lastindexof(“語言”);system.out.println(s.lastindexof(“v”, 2);運行結果:13143-14237njava.lang.string類字符串/字符序列n字符串轉變?yōu)閿?shù)組npublic byte getbytes() 將字符串轉變?yōu)橐粋€字節(jié)數(shù)組npublic byte getbyte

41、s(stringcharsetname) throws unsupportedencodingexception 按特定的字符編碼格式將字符串轉變?yōu)橐粋€字節(jié)數(shù)組npublic char tochararray() 將字符串轉變?yōu)橐粋€字符數(shù)組38njava.lang.string類字符串/字符序列n方法舉例string s = java語言;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);運行結果:686中文windows操作系統(tǒng): 默認字符集 gb2312其他系統(tǒng): 默認字符集 iso-8859-139njava.lang.string類字符串/字符序列n字符串npublic string split(string regex) 40njava.lang.string類字符串/字符序列n其他方法npublic string concat(stringstr) 連接字符串 cares.concat(s) returns caress to.concat(get).concat(her) returns togethe

43、r npublic string replace(charoldchar, charnewchar) 在字符串中進行字符替換 mesquite in your cellar.replace(e, o) returns mosquito in your collar” jonl.replace(q, x) returns jonl (no change) 41njava.lang.string類字符串/字符序列n其他方法npublic string trim() 將字符串頭尾的空格字符刪除npublic string tolowercase() 字符串中字符轉為小寫npublic string

44、touppercase() 字符串中字符轉為大寫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類字符串/字符序列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);運行結果: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);結論:1. string s1 = “abc”; 字符串常量對象 (immutable) string s2 = “abc”; 不同常量對象共享同一對象2. 其他字符串對象則可理解為對字符串常量對象進行了 一層包裝system.out.println(ern() = s4);true43njava.lang.string類字符串/字符序列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; 運行結果:truetrue truetruefalsetruepackage other;public class other static string hello = hello; 結論: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類n一個可變(mutable)/動態(tài)的字符序列n構造方法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轉換為字符串 - public string tostring() 45njava.lang.stringbuffer類方法舉例string s = java語言;stringbuffer buffer = new stringbuffer(s);buffer.append(“easy”);buffer.insert(6, “ is “);s = buffer.tostri

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論