版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、面向?qū)ο蟪绦蛟O(shè)計(jì)Java武漢大學(xué)國際軟件學(xué)院 桂浩第三章 面向?qū)ο筇卣鱪根本概念n類的定義n對(duì)象n類的承繼和多態(tài)n接口和包n常用工具類根本概念n什么是編程言語?n人和計(jì)算機(jī)進(jìn)展交流的工具和手段n編程言語的開展n機(jī)器言語: 0101001n匯編言語: mov, push, add, calln第三代言語: 高級(jí)言語,以C言語為代表,過程式編程言語(Procedural Programming Language)n第四代言語: 非過程化/面向?qū)ο蟮木幊萄哉Zn言語的開展: 籠統(tǒng)的過程n面向?qū)ο?Object Oriented-OO) n面向?qū)ο缶幊?Object Oriented Programmi
2、ng-OOP)n面向?qū)ο笫且环N軟件開發(fā)的方法,“面向?qū)ο蟮姆治雠c設(shè)計(jì)(OOA&OOD研討生課程)n第一個(gè)面向?qū)ο蟮难哉Z: Simula-67n第一個(gè)勝利的面向?qū)ο缶幊萄哉Z: SmalltalknC+, JAVA, C#, PERL等n用客觀世界中描畫事物的方法來描畫程序中要處理的問題n萬事萬物都是對(duì)象n程序便是成堆的對(duì)象,彼此經(jīng)過音訊的傳送,懇求其他對(duì)象進(jìn)展任務(wù) 根本概念n五個(gè)根本概念n對(duì)象n類n封裝性n承繼性n多態(tài)性根本概念n對(duì)象 (object)neverything is an objectn現(xiàn)實(shí)世界的對(duì)象: 桌子、書、自行車、電視、狗、文件、表格、按鈕、窗口n對(duì)象包含兩個(gè)特性n
3、形狀: 指對(duì)象本身的信息(內(nèi)部信息/內(nèi)部變量)n行為: 實(shí)現(xiàn)對(duì)信息的訪問/對(duì)象的操作n標(biāo)志: 代表對(duì)象的標(biāo)識(shí)符n山地車的形狀(兩個(gè)車輪、一些齒輪、速度、擋數(shù)),行為(剎車、加速、減速和換擋)n書包含很多信息,擁有訪問所包含信息的方法,一頁一頁的看,經(jīng)過目錄找到感興趣的內(nèi)容根本概念n對(duì)象 (object)n形狀變量(variables)n行為方法(method)n對(duì)象就是變量和相關(guān)方法的軟件集合n一個(gè)對(duì)象可以由其他對(duì)象組合而成,窗口(按鈕,菜單條,文本框,形狀欄等)n程序就是對(duì)象的集合,對(duì)象之間相互交互和通訊完成義務(wù)n搭積木nA program is a bunch of objects tel
4、ling each other what to do by sending messages根本概念n類 (class)n亞里士多德: the class of fishes and the class of birdsn現(xiàn)實(shí)世界中,存在很多同類的對(duì)象,很多桌子、書、自行車n一種原型,一種籠統(tǒng),一種共性,一個(gè)模板nEvery object has a typen實(shí)例(instance),某類對(duì)象的一個(gè)特定實(shí)體,類是對(duì)象的一個(gè)籠統(tǒng)n表格(類) 填入不同的個(gè)人信息不同的對(duì)象根本概念n封裝性n對(duì)象本身的數(shù)據(jù)得到維護(hù)/隱藏n其他對(duì)象僅僅需求知道對(duì)該對(duì)象的訪問方法(接口/interface)即可n益處n
5、模塊化-每個(gè)對(duì)象的源文件可以是相互獨(dú)立的,可以被不同的程序調(diào)用,每個(gè)對(duì)象是一塊積木,可以搭建不同的外形n信息隱藏-通常定義一個(gè)公共接口/方法實(shí)現(xiàn)對(duì)對(duì)象的訪問,可以調(diào)整對(duì)象的私有信息和方法,而不會(huì)對(duì)其他調(diào)用它的對(duì)象產(chǎn)生影響n可重用性n黑盒子n電腦的DIYn噴墨打印機(jī),硒鼓壞/彩色硒鼓根本概念n承繼性n為什么會(huì)有承繼?n建立一個(gè)類后,發(fā)現(xiàn)另一個(gè)新的類有一樣的特性,兩個(gè)選擇:重新定義一個(gè)新的類;在已有類的根底上,修正(加加/減減)n父類和子類,子類承繼(擁有)父類一切的數(shù)據(jù)和方法,同時(shí)子類可以有新的數(shù)據(jù)和方法,“青出于藍(lán),而勝于藍(lán)n樹型構(gòu)造(層次化構(gòu)造) n根(基類)根本概念n承繼性運(yùn)輸工具航空運(yùn)輸
6、工具陸地運(yùn)輸工具水上運(yùn)輸工具人力驅(qū)動(dòng)引擎驅(qū)動(dòng)二輪四輪客運(yùn)貨運(yùn)根本概念n承繼性n多重承繼類A類B類C類D類F類Gn一個(gè)類擁有多個(gè)父類產(chǎn)生二義性,例類C和類D都有一個(gè)同名的方法,類G?nJava中僅僅支持單一承繼,同時(shí)Java采用Interface(接口)實(shí)現(xiàn)多重承繼而防止父類二義性根本概念n多態(tài)性n表如今承繼中方法的重寫n子類從父類承繼(extends擴(kuò)展)而來n多個(gè)子類同屬一個(gè)父類,一切子類有一樣的父類n承繼父類的方法n在不同的子類中有不同的表現(xiàn)方式n表如今用一個(gè)類中方法的重載根本概念n多態(tài)性n多態(tài)性在承繼中的表現(xiàn)根本概念n多態(tài)性class Square extends Shape void
7、draw() System.out.println(Square.draw(); void erase() System.out.println(Square.erase()“); class Triangle extends Shape void draw() System.out.println(Triangle.draw(); void erase() System.out.println(Triangle.erase(); class Shape void draw() void erase() class Circle extends Shape void draw() System
8、.out.println(Circle.draw(); void erase() System.out.println(Circle.erase(); 根本概念n多態(tài)性public class Test public static Shape randShape() switch(int)(Math.random() * 3) default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); public static void main(String args)
9、Shape s = new Shape9; for(int i = 0; i s.length; i+) si = randShape(); for(int i = 0; i javac A.javax has private access in A a.x = 100; print() has private access in A a.print(); 2 errors類成員的訪問class A private int x;private void print() class B void test() A a = new A();a.x = 100;a.print();nprotecte
10、d (維護(hù)變量/維護(hù)方法)n允許類本身、子類(有一定限制)以及同一個(gè)包中一切類訪問(先構(gòu)造對(duì)象,再訪問)類成員的訪問class A protected int x;protected void print() class B void test() A a = new A();a.x = 100;a.print();package abc;class A protected int x;protected void print() package xyz;import abc.A;class B extends A void test(A a, B b) a.x = 100;a.print()
11、;b.x = 100;b.print();/illegal/illegal/legal/legaln無修飾 (友好變量/友好方法)n允許類本身以及同一個(gè)包中一切類訪問類成員的訪問class A int x;void print() class B void test() A a = new A();a.x = 100;a.print();package abc;class A int x;void print() package abc;class B void test() A a = new A();a.x = 100;a.print();n小結(jié)名稱訪問權(quán)修飾符類本身子類包所有類公共pub
12、lic默認(rèn)-保護(hù)protected *私有private* 指子類與父類不在同一個(gè)包中的情況類成員變量的訪問nstatic (靜態(tài)變量/靜態(tài)方法)n類的變量/方法,獨(dú)立于類的對(duì)象,可以直接根據(jù)類名調(diào)用nclass S nstatic int A = 12, B = 34;nstatic void print() nnclass Test npublic static void main(String args) nSystem.out.println(“A=“ + S.A + “ B=“ + S.B);nS.print();nn類成員變量nstatic (靜態(tài)變量/靜態(tài)方法)class Tes
13、t public void print(int x) System.out.println(x); public static void main(String args) int x = 3;print(x);D:javac Test.javaTest.java:9: non-static method print(int) cannot be referenced from a static context print(x); 1 errorclass Test public void print(int x) System.out.println(x); public static vo
14、id main(String args) int x = 3;Test t = new Test();t.print(x);class Test public static void print(int x) System.out.println(x); public static void main(String args) int x = 3;print(x);static方法中僅僅可以調(diào)用其他static方法類成員nfinal 變量/方法nfinal變量: 定義一個(gè)常數(shù),即變量值不能改動(dòng)nfinal double AVOGADRO = 6.022e23;nfinal類: 不能有子類nfi
15、nal方法: 方法不能被重寫(overriding)nclass ChessAlgorithm n . . .n final void nextMove(ChessPiece pieceMoved,n BoardLocation newLocation) n .n n . . .n類成員變量n小結(jié)n類: public, abstract, final, 無修飾, privaten類成員npublicnprotectednprivaten無修飾nstaticnfinal類成員變量n方法: 對(duì)象行為的描畫n完成某種功能的程序塊n定義: n訪問權(quán)限修飾符 方法前往類型 方法名 () throws 異
16、常名 n方法體;nn方法參數(shù)的傳送n變量的作用域n方法的重載(overloading)/重寫(overriding)n方法的遞歸調(diào)用類成員方法n訪問控制修飾符n公共訪問控制符public n被一切類訪問n默許訪問控制符n被同一包中其他類訪問n私有訪問控制符 privaten被該類本身訪問n維護(hù)訪問控制符 protectedn該類本身、同一個(gè)包中的其他類、其他包中的子類訪問n私有維護(hù)訪問控制符private protectedn該類本身、一切子類訪問類成員方法的訪問n非訪問控制修符n靜態(tài)方法staticn屬于類的方法n最終方法finaln不能被子類重新定義的方法n籠統(tǒng)方法abstractn只需方
17、法闡明,沒有詳細(xì)實(shí)現(xiàn)n本地方法nativen以其他言語實(shí)現(xiàn)方法功能n同步方法synchronizedn用于多線程程序中的協(xié)調(diào)和同步類成員方法的訪問n例成員方法class Test public static void main(String args) double d_product;Area myArea;myArea = new Area(); d_product = myAduct();System.out.println(“myArea的面積是: + d_product);class Area double width, height; void setV(double
18、 w, double h) width = w;height = h; double product() return width*height;n方法參數(shù)成員方法public static void main(String args) int x=3, y=4;Test p = new Test();System.out.println(x + + y);p.addI(x, y);System.out.println(x + + y);Vector c = new Vector();c.addElement(s1);c.addElement(s2);System.out.println(c.
19、size();p.addV(c);System.out.println(c.size();import java.util.Vector;class Test private void addV(Vector v) v.addElement(s3); private void addI(int i1, int i2) i1 +; i2 +; 形參和實(shí)參D:java Parameter3 43 423留意1: 類型匹配留意2:根本類型與復(fù)合類型參數(shù)傳送的結(jié)果不同Pass by ValueIn Java methods, arguments are passed by value. When in
20、voked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the obje
21、cts methods and modify the accessible variables within the object. n例成員方法class Test public static void main(String args) ex A = new ex();A.p();class ex int u, v;void p2(int x, int y) int i, j;for (i=1; i=x;i+) j = y+i; System.out.print(j+“ ); void p( ) u=3; v=2; p2(u, v);System.out.println();u+=v; v
22、*=u; p2(u, v); u=3; v=2;u=5; v=10;輸出結(jié)果:3 4 511 12 13 14 15n變量的作用域(成員變量/部分變量)成員方法n方法的重載(overload)n方法名一樣,但方法的參數(shù)不同n方法描畫 方法名(標(biāo)識(shí)符) 參數(shù)類型 方法前往值 n絕對(duì)值 abs(a) int intn絕對(duì)值 abs(a) long longn絕對(duì)值 abs(a) float floatn絕對(duì)值 abs(a) double doublen npublic static int abs(inta) npublic static long abs(longa) npublic stati
23、c float abs(floata)npublic static double abs(doublea) 成員方法n方法的重寫(overriding)n子類重寫父類的方法成員方法class Father void display( ) ; class Son extends Father void display() ; Father f = new Father();f.display();Son s = new Son();s.display();構(gòu)造方法n創(chuàng)建對(duì)象/實(shí)例化對(duì)象new n例1: Apple a = new Apple(); (創(chuàng)建對(duì)象)n例2: Apple a; (對(duì)象的
24、闡明)n a = new Apple(); (實(shí)例化對(duì)象)n對(duì)象的實(shí)例化經(jīng)過構(gòu)造方法(constructor)來實(shí)現(xiàn)n構(gòu)造方法的名字與類名一樣n構(gòu)造方法沒有前往值n構(gòu)造方法可以有多個(gè),構(gòu)成方法的重載(overload)n例: 對(duì)象的實(shí)例化和初始化class jex6_8 public static void main(String args) Qangle q1 = new Qangle(); Qangle q2 = new Qangle(20, 50); Qangle q3 = new Qangle(q1); System.out.println(q1.width() +“ +q1.heig
25、ht(); System.out.println(q2.width() +“ +q2.height(); System.out.println(q3.width() +“ +q3.height();class Qangle int a, h;Qangle () a = 10; h = 20;Qangle(int x, int y) a = x; h = y;Qangle(Qangle r) a = r.width(); h = r.height();int width() return a;int height() return h;輸出結(jié)果: 2020 5010 20構(gòu)造方法n再談方法的重載
26、(overload)構(gòu)造方法class Tree int height; Tree() prt(“Planting a seeding); height = 0; Tree(int i) prt(“Creating new Tree that is + i + “ feet tall);height = i; void info() prt(“Tree is + height + “ feet height); void info(String s) prt(s + “: Tree is + height +“ feet height); static void prt(String s) S
27、ystem.out.println(s); for (int i =0; i 5; i+) Tree t = new Tree(i); t(); t(“my tree);new Tree();n默許構(gòu)造方法n例 class Apple nint color; nnApple a = new Apple();n對(duì)象實(shí)例的判別: nulln例 Apple a;n if (a = null) nSystem.out.println(“Day dream);構(gòu)造方法運(yùn)轉(zhuǎn)時(shí)系統(tǒng)自動(dòng)賦予一個(gè)空構(gòu)造函數(shù)如 Apple() n再談構(gòu)造方法構(gòu)造方法 class Cmethod Cmethod (boolean
28、b) public static void main (String args) Cmethod c1 = new Cmethod(); Cmethod c2 = new Cmethod(false); class Cmethod Cmethod (boolean b) public static void main (String args) /Cmethod c1 = new Cmethod(); Cmethod c2 = new Cmethod(false); class Cmethod Cmethod (boolean b) Cmethod () public static void
29、main (String args) Cmethod c1 = new Cmethod(); Cmethod c2 = new Cmethod(false); 運(yùn)轉(zhuǎn)時(shí)系統(tǒng)自動(dòng)賦予一個(gè)空構(gòu)造方法,僅僅當(dāng)該類沒定義構(gòu)造方法的情況下static變量的初始化class Smember static int i = 100;static boolean b = true;char c = t;public Smember() public static void main(String args) Smember m1 = new Smember();Smember m2 = new Smember()
30、;不論產(chǎn)生多少個(gè)對(duì)象,屬于類的靜態(tài)變量只需一份,即只占有一份存儲(chǔ)空間class Smember static int i; static boolean b;static i = 100;b = true;System.out.println(“In static block);char c = t;public Smember() System.out.println(“In constructor); public static void main(String args) Smember m1 = new Smember();Smember m2 = new Smember();輸出結(jié)果
31、In static blockIn constructorIn constructor設(shè)計(jì)方式的例子class Singleton private static Singleton me = new Singleton();public static Singleton getSingleton() return me;private Singleton() public static void main(String args) Singleton s1 = Singleton.getSingleton();Singleton s2 = Singleton.getSingleton();if
32、 (s1 = s2) System.out.println(s1 is s2);else System.out.println(s1 is not s2);該Singleton類只需一個(gè)實(shí)例,即該Singleton類只能構(gòu)造一個(gè)對(duì)象“Design Patterns: Elements of Reusable Object-Oriented Software(設(shè)計(jì)方式)Erich Gamma、Richard Helm、Ralph Johnson、John Vlissides Addison-Wesley, 2019這幾位作者常被稱為“四人組(Gang of Four), 而這本書也就被稱為“四人
33、組(或 GoF)書設(shè)計(jì)方式: 在一定的環(huán)境中處理某一問題的方案23 種設(shè)計(jì)方式, 3大類創(chuàng)建型方式(Creational pattern)規(guī)定了創(chuàng)建對(duì)象的方式,如Singleton方式、Factory Method方式構(gòu)外型方式(Structural pattern)規(guī)定了如何組織類和對(duì)象。這些方式涉及類如何相互承繼或如何從其它類組合,如Adapter、Proxy 和 Decorator 方式行為方式(Behavioral pattern)規(guī)定了對(duì)象之間交互的方式。如Observer方式、Strategy和Template 方式并發(fā)方式(Concurrency pattern)規(guī)定協(xié)調(diào)或順序?qū)?/p>
34、共享資源訪問的方式MVC(Model/View/Controller, 模型/視圖/控制器)設(shè)計(jì)方式的例子public class Sequence private static Sequence instance; private static int counter; private Sequence() counter = 0; public static synchronized Sequence getInstance() if (instance=null) instance = new Sequence(); return instance; public static sync
35、hronized int getNext() return +counter; n這個(gè)類不可以再有子類,由于構(gòu)造方法是privatenLazy instantiation (Lazy initialization),僅在需求的時(shí)候才實(shí)例化對(duì)象private static Sequence instance = new Sequence();籠統(tǒng)類(abstract class)n一個(gè)未完成的類n僅僅籠統(tǒng)類可以包含籠統(tǒng)方法(abstract methods)n籠統(tǒng)方法: 僅僅聲明了方法,但未實(shí)現(xiàn)n有訪問修飾詞n前往值類型n方法名n參數(shù)列表n無方法體籠統(tǒng)類(abstract class)abstr
36、act class Point int x = 1, y = 1;void move(int dx, int dy) x += dx;y += dy;alert();abstract void alert();abstract class ColoredPoint extends Point int color;class SimplePoint extends Point void alert() 籠統(tǒng)方法: 有訪問修飾詞、前往值類型、方法名和參數(shù)列表,無方法體籠統(tǒng)類(abstract class)n籠統(tǒng)類不能被實(shí)例化,例Point p = new Point(); n子類承繼籠統(tǒng)類時(shí),必需
37、重寫籠統(tǒng)方法,否那么仍為籠統(tǒng)類abstract class Point int x = 1, y = 1;void move(int dx, int dy) x += dx;y += dy;alert();abstract void alert();abstract class ColoredPoint extends Point int color;class SimplePoint extends Point void alert() Point p = new SimplePoint();籠統(tǒng)類(abstract class)n多態(tài)性abstract class Graphics abs
38、tract void parameter();abstract void area();class Rectangle extends Graphics double h, w;Rectangle (double u, double v) h = u; w = v;void parameter() System.out.println(h + “ “ + w);void area() System.out.println(h*w); Rectangle rec = new Rectangle(1.0, 2.0);Circle cir = new Circle(3.0, “Red);Graphi
39、cs g = rec, cir;for (int i = 0; i g.length; i+) gi.parameter(); gi.area();class Circle extends Graphics double r; String c;Circle (double u, String v) r = u; c = v;void parameter() System.out.println(r + “ “ + c); void area() System.out.println(Math.PI*r*r); 運(yùn)轉(zhuǎn)結(jié)果1.0 2.02.03.0 Red28.274333882308多態(tài)性(p
40、olymorphism)運(yùn)用場所: 不同子類可以用不同的方式實(shí)現(xiàn)同一共同接口,該共同接口建立了一個(gè)根本方式第三章 面向?qū)ο筇卣鱪根本概念n類的定義n對(duì)象n類的承繼和多態(tài)n接口和包n常用工具類對(duì)象n援用對(duì)象的變量n格式: 對(duì)象名.變量名n援用對(duì)象的方法n格式: 對(duì)象名.方法名n例1nVector v = new Vector();nv.addElement(“hello world);n例2nint a= 1, 2, 3, 4, 5; nint size = a.length;n例3nSystem.out.println();對(duì)象n經(jīng)過對(duì)象援用對(duì)象的成員變量和成員方法class Qangle i
41、nt a, h;Qangle () a = 10; h = 20;Qangle(int x, int y) a = x; h = y;Qangle(Qangle r) a = r.width(); h = r.height();void set(int x, int y) a=x; h =y;q1.set(30, 40);q1.a = 30;q1.h = 40;目的一樣第一方式更平安、更面向?qū)ο?數(shù)據(jù)封裝)直接支配變量對(duì)象的釋放n將對(duì)象從內(nèi)存中去除n內(nèi)存的管理(枯燥、容易出錯(cuò))n渣滓回收(Garbage Collection)n渣滓搜集器(Garbage Collector)n周期性地釋放不再
42、被援用的對(duì)象,自動(dòng)完成n手動(dòng)完成,System.gc();npublic static void gc() - Runs the garbage collector. The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you dont have to worry about destroying them. The Java runtime environment deletes objects
43、when it determines that they are no longer being used. This process is called garbage collection. 對(duì)象的運(yùn)用n訪問對(duì)象的私有(private)成員n經(jīng)過定義一個(gè)公共方法來實(shí)現(xiàn)class Student private String name;private String id;Student(String s1, String s2) name = s1; id = s2;String getName() return name;void setName(String s) name=s;Stud
44、ent st = new Student(“aloha, “001);String n = st.getName(); st.setName(“csma);n = st.getName(); 對(duì)象的運(yùn)用n對(duì)象作為方法的參數(shù)n訪問權(quán)限修飾符 方法前往類型 方法名(參數(shù)) throws 異常名n方法體;nn參數(shù): 類型 變量名, n類型: 根本數(shù)據(jù)類型/復(fù)合類型(對(duì)象)n參數(shù)的傳送nPass by valuen例 對(duì)象用作方法的參數(shù)對(duì)象的運(yùn)用class jex6_10 public static void main(String args) Spot s = new Spot(2, 3); Sys
45、tem.out.println(“s點(diǎn)的坐標(biāo): + +s.getX()+“,+s.getY(); Trans ts = new Trans(); ts.move(s, 4, 5); System.out.println(“s點(diǎn)的坐標(biāo): + +s.getX()+“,+s.getY();class Spot private int x, y;Spot (int u, int v) setX(u); setY(v);void setX(int x1) x=x1;void setY(int y1) y=y1;int getX() return x;int getY() return y;class T
46、rans void move(Spot p, int h, int k) p.setX(p.getX() + h);p.setY(p.getY() + k);class jex6_10 public static void main(String args) Spot s = new Spot(2, 3); System.out.println(“s點(diǎn)的坐標(biāo): + s.getX()+“,+s.getY(); Trans.move(s, 4, 5); System.out.println(“s點(diǎn)的坐標(biāo): + s.getX()+“,+s.getY();class Spot private int
47、x, y;Spot (int u, int v) setX(u); setY(v);void setX(int x1) x=x1;void setY(int y1) y=y1;int getX() return x;int getY() return y;class Trans static void move(Spot p, int h, int k)p.setX(p.getX() + h);p.setY(p.getY() + k);class jex6_10 public static void main(String args) Spot s = new Spot(2, 3); Syst
48、em.out.println(“s點(diǎn)的坐標(biāo): + s.getX()+“,+s.getY(); s.move(4, 5); System.out.println(“s點(diǎn)的坐標(biāo): + s.getX()+“,+s.getY();class Spot private int x, y;Spot (int u, int v) setX(u); setY(v);void setX(int x1) x=x1;void setY(int y1) y=y1;int getX() return x;int getY() return y;void move(int h, int k)x = x + h;y = y
49、 + k;對(duì)象的運(yùn)用n對(duì)象的訪問n對(duì)象作為方法的前往值n訪問權(quán)限修飾符 方法前往類型 方法名(參數(shù)) throws 異常名n方法體;nn前往類型n有前往值: 根本數(shù)據(jù)類型/復(fù)合類型(對(duì)象)n無前往值: void對(duì)象的運(yùn)用n對(duì)象作為方法的前往值n例: 求兩點(diǎn)坐標(biāo)之間的中點(diǎn)坐標(biāo)n思緒: (x1, y1) 和(x2, y2)(x, y)nx=(x1+x2)/2, y=(y1+y2)/2nSpot s1 = new Spot(2, 3);nSpot s2 = new Spot(4, 5);nSpot s = s1.midSpot(s2);n例 對(duì)象用作方法的前往值對(duì)象的運(yùn)用class jex6_13
50、public static void main(String args) Spot s1 = new Spot(3.0, 5.0); Spot s2 = new Spot(6.0, 8.0); System.out.println(“s1點(diǎn)的坐標(biāo): + s1.getX()+“,+s1.getY(); System.out.println(“s2點(diǎn)的坐標(biāo): + s2.getX()+“,+s2.getY(); Spot s = s1.midSpot(s2); System.out.println(“中點(diǎn)的坐標(biāo): + s.getX()+“,+s.getY();class Spot private d
51、ouble x, y;Spot (double u, double v) setX(u); setY(v);void setX(double x1) x=x1;void setY(double y1) y=y1;double getX() return x;double getY() return y;Spot midSpot(Spot s)double midX=(x+s.getX()/2;double midY=(y+s.getY()/2;return new Spot(midX, midY);對(duì)象的運(yùn)用n數(shù)組: 類型一樣的一列元素n簡單的數(shù)組public class ArrayDemo
52、public static void main(String args) int anArray = new int10; for (int i = 0; i anArray.length; i+) anArrayi = i; System.out.print(anArrayi + ); System.out.println(); 對(duì)象的運(yùn)用n對(duì)象數(shù)組class Test public static void main(String args) int a = new int5;for (int i = 0; i a.length; i+) System.out.println(ai);Str
53、ing s = new String5;for (int i = 0; i s.length; i+) System.out.println(si);System.out.println(si.length();對(duì)象的運(yùn)用n對(duì)象數(shù)組class Student private String name;private String id;Student (String s1, String s2) name = s1; id = s2; String getName() return name;void setName (String s) name=s; void display () Syst
54、em.out.println(name + “ +id); Student st = new Student10;for (int i = 0; i st.length; i+) sti = new Student();for (int i = 0; i st.length; i+) sti.display();對(duì)象的運(yùn)用n對(duì)象作為另一個(gè)對(duì)象的成員變量n一個(gè)對(duì)象中包含另一個(gè)對(duì)象,組合關(guān)系class Student private String name;private String id;Student (String s1, String s2) name = s1; id = s2; St
55、ring getName() return name; void setName (String s) name=s; 對(duì)象的運(yùn)用n對(duì)象作為另一個(gè)對(duì)象的成員變量class MobilePhone private String type;private Watch w;MobilePhone (String s) type = s; void setWatch(Watch a) w = a; long getTime () return w.getTime(); class Watch long getTime() return System.currentTimeMillis(); Mobil
56、ePhone mp = new MobilePhone(“nokia);Watch w = new Watch();mp.setWatch(w);long l = mp.getTime();public static long currentTimeMillis()the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC對(duì)象的運(yùn)用n關(guān)鍵詞 thisnthis指當(dāng)前對(duì)象n運(yùn)用1: 加強(qiáng)程序可讀性(this可有可無)對(duì)象的運(yùn)用n例1: Demo1.java
57、 (調(diào)用變量)class Demo1 double x, y;Demo1(double i, double j) this.x=i; this.y=j;double ave() return (x+y)/2; public static void main(String args) Demo1 d = new Demo1(3, 4);System.out.println(d.ave();對(duì)象的運(yùn)用n例2: Demo2.java (調(diào)用方法)class Demo2 int x, y, z;Demo2(int a, int b) x =a; y=b; this.sort(a, b); void s
58、ort(int a, int b) int t;if (x y) t=x; x=y; y=t 對(duì)象的運(yùn)用n例3: Demo3.java (調(diào)用方法)class Demo3 int x, y;Demo3(int a, int b) x =a; y=b;void change (int i, int j) x=i; y=j; this.sort();void sort() int t;if (x y) t=x; x=y; y=t 對(duì)象的運(yùn)用n關(guān)鍵詞 thisnthis指當(dāng)前對(duì)象n運(yùn)用2: 對(duì)同一個(gè)對(duì)象執(zhí)行多次方法調(diào)用class Leaf int i = 0;Leaf increment() i+;
59、return this;void print() System.out.println(“i= + i); Leaf x = new Leaf();x.increment().increment().increment().print();對(duì)象的運(yùn)用n關(guān)鍵詞 thisnthis指當(dāng)前對(duì)象n運(yùn)用3: 在一個(gè)構(gòu)造函數(shù)中調(diào)用另一個(gè)構(gòu)造函數(shù)class Flower String name;int price;Flower () this(tulip, 10);Flower (String s, int i) name = s;price = i; void print () System.out.pr
60、intln(name + + price); public static void main(String args)Flower f = new Flower();f.print(); 第三章 面向?qū)ο筇卣鱪根本概念n類的定義n對(duì)象n類的承繼和多態(tài)n接口和包n常用工具類類的承繼n父類與子類的關(guān)系 (extends)n根本內(nèi)容n子類可調(diào)用父類的方法和變量,子類可添加父類中沒有的方法和變量n子類可重新定義父類的靜態(tài)/實(shí)例變量n子類可重新定義父類的靜態(tài)/實(shí)例方法n承繼中的構(gòu)造方法n類成員訪問修飾符與承繼的關(guān)系類的承繼n子類可調(diào)用父類的方法和變量n子類可添加父類中沒有的方法和變量class Vehicle
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 如何解決研究院的科研工作者在實(shí)驗(yàn)室內(nèi)部高效溝通等問題-TIKOOL太酷無線內(nèi)通
- 餐椅質(zhì)保合同范本
- 北京市商品房現(xiàn)房買賣合同示范文本
- 湖南省益陽市赫山區(qū)海棠學(xué)校2024-2025學(xué)年上學(xué)期期中考試八年級(jí)英語試題(含答案)
- 輸單員年終總結(jié)
- 教師工作總結(jié)述職報(bào)告
- 調(diào)查物聯(lián)網(wǎng)安全
- 市春季火災(zāi)隱患排查整治方案例文(6篇)
- 三廢銀行業(yè)污染整治行動(dòng)方案例文(6篇)
- 數(shù)字經(jīng)濟(jì)全景白皮書
- 煤礦消防安全管理制度范本
- 小學(xué)三年級(jí)六班上學(xué)期班主任家長會(huì)課件
- 和易充智能充電系統(tǒng)(PPT課件)
- 30MW光伏項(xiàng)目送出系統(tǒng)工程施工組織總設(shè)計(jì)1
- 建筑抗震設(shè)計(jì)規(guī)范2010
- 100以內(nèi)退位減法經(jīng)典實(shí)用
- HGO GNSS數(shù)據(jù)處理軟件
- (核電站)反應(yīng)堆棚和水補(bǔ)給系統(tǒng)(REA)
- 抗菌藥物使用強(qiáng)度整改的PDCA案例
- 昔日中國數(shù)學(xué)物理奧賽選手今何在
- 自來水廠操作規(guī)程手冊(cè)
評(píng)論
0/150
提交評(píng)論