类....
1.類的組成
類由屬性和方法組成
1.屬性
修飾符:
public:公共的類的屬性或變量,可以在該類以外調(diào)用和供其他類調(diào)用和使用
private:私有的類的屬性或變量,只能在本類使用,不可外調(diào),外調(diào)是會(huì)報(bào)錯(cuò))
變量:
#mermaid-svg-KEM7U2UwPV9oBRYR .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .label text{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .node rect,#mermaid-svg-KEM7U2UwPV9oBRYR .node circle,#mermaid-svg-KEM7U2UwPV9oBRYR .node ellipse,#mermaid-svg-KEM7U2UwPV9oBRYR .node polygon,#mermaid-svg-KEM7U2UwPV9oBRYR .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-KEM7U2UwPV9oBRYR .node .label{text-align:center;fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .node.clickable{cursor:pointer}#mermaid-svg-KEM7U2UwPV9oBRYR .arrowheadPath{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-KEM7U2UwPV9oBRYR .flowchart-link{stroke:#333;fill:none}#mermaid-svg-KEM7U2UwPV9oBRYR .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-KEM7U2UwPV9oBRYR .edgeLabel rect{opacity:0.9}#mermaid-svg-KEM7U2UwPV9oBRYR .edgeLabel span{color:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-KEM7U2UwPV9oBRYR .cluster text{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-KEM7U2UwPV9oBRYR .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-KEM7U2UwPV9oBRYR text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-KEM7U2UwPV9oBRYR .actor-line{stroke:grey}#mermaid-svg-KEM7U2UwPV9oBRYR .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-KEM7U2UwPV9oBRYR #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .sequenceNumber{fill:#fff}#mermaid-svg-KEM7U2UwPV9oBRYR #sequencenumber{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR #crosshead path{fill:#333;stroke:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .messageText{fill:#333;stroke:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-KEM7U2UwPV9oBRYR .labelText,#mermaid-svg-KEM7U2UwPV9oBRYR .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-KEM7U2UwPV9oBRYR .loopText,#mermaid-svg-KEM7U2UwPV9oBRYR .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-KEM7U2UwPV9oBRYR .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-KEM7U2UwPV9oBRYR .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-KEM7U2UwPV9oBRYR .noteText,#mermaid-svg-KEM7U2UwPV9oBRYR .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-KEM7U2UwPV9oBRYR .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-KEM7U2UwPV9oBRYR .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-KEM7U2UwPV9oBRYR .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-KEM7U2UwPV9oBRYR .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .section{stroke:none;opacity:0.2}#mermaid-svg-KEM7U2UwPV9oBRYR .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-KEM7U2UwPV9oBRYR .section2{fill:#fff400}#mermaid-svg-KEM7U2UwPV9oBRYR .section1,#mermaid-svg-KEM7U2UwPV9oBRYR .section3{fill:#fff;opacity:0.2}#mermaid-svg-KEM7U2UwPV9oBRYR .sectionTitle0{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .sectionTitle1{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .sectionTitle2{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .sectionTitle3{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-KEM7U2UwPV9oBRYR .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .grid path{stroke-width:0}#mermaid-svg-KEM7U2UwPV9oBRYR .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-KEM7U2UwPV9oBRYR .task{stroke-width:2}#mermaid-svg-KEM7U2UwPV9oBRYR .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .taskText:not([font-size]){font-size:11px}#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-KEM7U2UwPV9oBRYR .task.clickable{cursor:pointer}#mermaid-svg-KEM7U2UwPV9oBRYR .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-KEM7U2UwPV9oBRYR .taskText0,#mermaid-svg-KEM7U2UwPV9oBRYR .taskText1,#mermaid-svg-KEM7U2UwPV9oBRYR .taskText2,#mermaid-svg-KEM7U2UwPV9oBRYR .taskText3{fill:#fff}#mermaid-svg-KEM7U2UwPV9oBRYR .task0,#mermaid-svg-KEM7U2UwPV9oBRYR .task1,#mermaid-svg-KEM7U2UwPV9oBRYR .task2,#mermaid-svg-KEM7U2UwPV9oBRYR .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutside0,#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutside2{fill:#000}#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutside1,#mermaid-svg-KEM7U2UwPV9oBRYR .taskTextOutside3{fill:#000}#mermaid-svg-KEM7U2UwPV9oBRYR .active0,#mermaid-svg-KEM7U2UwPV9oBRYR .active1,#mermaid-svg-KEM7U2UwPV9oBRYR .active2,#mermaid-svg-KEM7U2UwPV9oBRYR .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-KEM7U2UwPV9oBRYR .activeText0,#mermaid-svg-KEM7U2UwPV9oBRYR .activeText1,#mermaid-svg-KEM7U2UwPV9oBRYR .activeText2,#mermaid-svg-KEM7U2UwPV9oBRYR .activeText3{fill:#000 !important}#mermaid-svg-KEM7U2UwPV9oBRYR .done0,#mermaid-svg-KEM7U2UwPV9oBRYR .done1,#mermaid-svg-KEM7U2UwPV9oBRYR .done2,#mermaid-svg-KEM7U2UwPV9oBRYR .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-KEM7U2UwPV9oBRYR .doneText0,#mermaid-svg-KEM7U2UwPV9oBRYR .doneText1,#mermaid-svg-KEM7U2UwPV9oBRYR .doneText2,#mermaid-svg-KEM7U2UwPV9oBRYR .doneText3{fill:#000 !important}#mermaid-svg-KEM7U2UwPV9oBRYR .crit0,#mermaid-svg-KEM7U2UwPV9oBRYR .crit1,#mermaid-svg-KEM7U2UwPV9oBRYR .crit2,#mermaid-svg-KEM7U2UwPV9oBRYR .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-KEM7U2UwPV9oBRYR .activeCrit0,#mermaid-svg-KEM7U2UwPV9oBRYR .activeCrit1,#mermaid-svg-KEM7U2UwPV9oBRYR .activeCrit2,#mermaid-svg-KEM7U2UwPV9oBRYR .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-KEM7U2UwPV9oBRYR .doneCrit0,#mermaid-svg-KEM7U2UwPV9oBRYR .doneCrit1,#mermaid-svg-KEM7U2UwPV9oBRYR .doneCrit2,#mermaid-svg-KEM7U2UwPV9oBRYR .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-KEM7U2UwPV9oBRYR .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-KEM7U2UwPV9oBRYR .milestoneText{font-style:italic}#mermaid-svg-KEM7U2UwPV9oBRYR .doneCritText0,#mermaid-svg-KEM7U2UwPV9oBRYR .doneCritText1,#mermaid-svg-KEM7U2UwPV9oBRYR .doneCritText2,#mermaid-svg-KEM7U2UwPV9oBRYR .doneCritText3{fill:#000 !important}#mermaid-svg-KEM7U2UwPV9oBRYR .activeCritText0,#mermaid-svg-KEM7U2UwPV9oBRYR .activeCritText1,#mermaid-svg-KEM7U2UwPV9oBRYR .activeCritText2,#mermaid-svg-KEM7U2UwPV9oBRYR .activeCritText3{fill:#000 !important}#mermaid-svg-KEM7U2UwPV9oBRYR .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-KEM7U2UwPV9oBRYR g.classGroup text .title{font-weight:bolder}#mermaid-svg-KEM7U2UwPV9oBRYR g.clickable{cursor:pointer}#mermaid-svg-KEM7U2UwPV9oBRYR g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-KEM7U2UwPV9oBRYR g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-KEM7U2UwPV9oBRYR .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-KEM7U2UwPV9oBRYR .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-KEM7U2UwPV9oBRYR .dashed-line{stroke-dasharray:3}#mermaid-svg-KEM7U2UwPV9oBRYR #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR .commit-id,#mermaid-svg-KEM7U2UwPV9oBRYR .commit-msg,#mermaid-svg-KEM7U2UwPV9oBRYR .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-KEM7U2UwPV9oBRYR g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-KEM7U2UwPV9oBRYR g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-KEM7U2UwPV9oBRYR g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-KEM7U2UwPV9oBRYR .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-KEM7U2UwPV9oBRYR .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-KEM7U2UwPV9oBRYR .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-KEM7U2UwPV9oBRYR .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-KEM7U2UwPV9oBRYR .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-KEM7U2UwPV9oBRYR .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-KEM7U2UwPV9oBRYR .edgeLabel text{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-KEM7U2UwPV9oBRYR .node circle.state-start{fill:black;stroke:black}#mermaid-svg-KEM7U2UwPV9oBRYR .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-KEM7U2UwPV9oBRYR #statediagram-barbEnd{fill:#9370db}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-state .divider{stroke:#9370db}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-KEM7U2UwPV9oBRYR .note-edge{stroke-dasharray:5}#mermaid-svg-KEM7U2UwPV9oBRYR .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-KEM7U2UwPV9oBRYR .error-icon{fill:#522}#mermaid-svg-KEM7U2UwPV9oBRYR .error-text{fill:#522;stroke:#522}#mermaid-svg-KEM7U2UwPV9oBRYR .edge-thickness-normal{stroke-width:2px}#mermaid-svg-KEM7U2UwPV9oBRYR .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-KEM7U2UwPV9oBRYR .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-KEM7U2UwPV9oBRYR .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-KEM7U2UwPV9oBRYR .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-KEM7U2UwPV9oBRYR .marker{fill:#333}#mermaid-svg-KEM7U2UwPV9oBRYR .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-KEM7U2UwPV9oBRYR {color: rgba(0, 0, 0, 0.75);font: ;} 變量 成員變量 局部變量 實(shí)例變量 不以static修飾 類變量 以static修飾 形參 代碼塊局部變量 方法局部變量2.方法
一個(gè)方法只執(zhí)行一個(gè)功能,這樣方便以后的拓展。如果方法存在返回值的情況下,一定要return,return 0會(huì)終止方法。
通常情況下,如果一個(gè)方法要返回值,那么我們將這個(gè)方法視作一個(gè)值,若不返回值,那么這個(gè)方法就是調(diào)用一個(gè)語句。
this代表當(dāng)前對(duì)象,這是為了防止對(duì)象中的局部變量和類變量重名所用的一個(gè)東西。
Java使用棧機(jī)制。main方法會(huì)在底層,然后不斷地調(diào)用其他的方法直到調(diào)用結(jié)束為止,然后一條一條的消失。
1.方法重載
方法名稱都必須相同,參數(shù)列表必須不同,方法的返回值類型可以相同也可以不同,僅僅返回類型不同不足以成為方法的重載。
2.方法的重寫
public class People{public void call(){System.out.println("你好,我是一個(gè)人");}} //子類繼承父類,改進(jìn)方法叫做方法的重寫 public class Workers extends People{public void call(){System.out.println("你好,我是工作者");}} /* 創(chuàng)建對(duì)象時(shí)的用法:除了傳統(tǒng)的new對(duì)象的用法外還有People liLei=new Workers();這個(gè)用法也叫做向上轉(zhuǎn)型,即父類引用指向子類對(duì)象。2.類的封裝
封裝,即隱藏對(duì)象的屬性和實(shí)現(xiàn)細(xì)節(jié),僅對(duì)外公開接口,控制在程序中屬性的讀和修改的訪問級(jí)別
類的封裝有以下好處:
如何去實(shí)現(xiàn)類的封裝呢?
3.類的繼承
extends實(shí)現(xiàn)類的繼承
類的繼承的語法:
class 子類 extends 父類
類的繼承的好處:
兩個(gè)類相似,那么它們會(huì)有許多重復(fù)的代碼,導(dǎo)致后果就是代碼量大且臃腫,通過繼承就可以解決這個(gè)問題。
繼承的特點(diǎn):
- 子類擁有父類除 private 以外的所有屬性和方法。
- 子類可以擁有自己的屬性和方法。
- 子類可以重寫實(shí)現(xiàn)父類的方法。
- Java 中的繼承是單繼承,一個(gè)類只有一個(gè)父類。
注:Java 實(shí)現(xiàn)多繼承的一個(gè)辦法是 implements(實(shí)現(xiàn))接口,但接口不能有非靜態(tài)的屬性,這一點(diǎn)請(qǐng)注意。
super 關(guān)鍵字在子類內(nèi)部使用,代表父類對(duì)象。
4.多態(tài)
通過父類就能夠引用不同的子類,這就是多態(tài).
例:
People liLei=new Workers();多態(tài)的實(shí)現(xiàn)條件
Java 實(shí)現(xiàn)多態(tài)有三個(gè)必要條件:繼承、重寫和向上轉(zhuǎn)型(即父類引用指向子類對(duì)象)。向上轉(zhuǎn)型,在運(yùn)行時(shí),會(huì)遺忘子類對(duì)象中與父類對(duì)象中不同的方法,也會(huì)覆蓋與父類中相同的方法——重寫(方法名,參數(shù)都相同)。
多態(tài)的實(shí)現(xiàn)方式
Java 中多態(tài)的實(shí)現(xiàn)方式:繼承父類進(jìn)行方法重寫,抽象類和抽象方法,接口實(shí)現(xiàn)。
5.抽象類
抽象類是父類,抽象類是約束子類必須要實(shí)現(xiàn)哪些方法,而并不關(guān)注方法如何去實(shí)現(xiàn)。
抽象類的實(shí)現(xiàn)
例:
//抽象方法 public abstract class TelePhone {public abstract void call(); //抽象方法,打電話public abstract void message(); //抽象方法,發(fā)短信 } public class CellPhone extends TelePhone {@Overridepublic void call() {System.out.println("我可以打電話!");}@Overridepublic void message() {System.out.println("我可以發(fā)短信!");}public static void main(String[] args) {CellPhone cp = new CellPhone();cp.call();cp.message();}}6.接口
implemens實(shí)現(xiàn)接口
接口的語法聲明:
修飾符 interface 接口名稱 [extends 其他的接口名] {
// 聲明變量
// 抽象方法
}
例:
// Animal.java interface Animal {//int x;//編譯錯(cuò)誤,x需要初始化,因?yàn)槭?static final 類型int y = 5;public void eat();public void travel(); }接口的實(shí)現(xiàn):
// Cat.java public class Cat implements Animal{public void eat(){System.out.println("Cat eats");}public void travel(){System.out.println("Cat travels");}public static void main(String[] args) {Cat cat = new Cat();cat.eat();cat.travel();} }多繼承
修飾符 interface A extends 接口1,接口2{}修飾符 class A implements 接口1,接口2{}注意點(diǎn):
- 接口不能用于實(shí)例化對(duì)象。
- 接口中方法只能是抽象方法、default 方法、靜態(tài)方法。
- 接口成員是 static final 類型。
- 接口支持多繼承。
7.總結(jié)
類是一組具有相同屬性和方法的對(duì)象的集合,對(duì)象是類的一個(gè)實(shí)例化。
總結(jié)
- 上一篇: 中国第一国产计算机公司,【新中国的第一】
- 下一篇: 宅米网技术变迁——初创互联网公司的技术发