类....
1.類的組成
類由屬性和方法組成
1.屬性
修飾符:
public:公共的類的屬性或變量,可以在該類以外調用和供其他類調用和使用
private:私有的類的屬性或變量,只能在本類使用,不可外調,外調是會報錯)
變量:
#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: ;} 變量 成員變量 局部變量 實例變量 不以static修飾 類變量 以static修飾 形參 代碼塊局部變量 方法局部變量2.方法
一個方法只執行一個功能,這樣方便以后的拓展。如果方法存在返回值的情況下,一定要return,return 0會終止方法。
通常情況下,如果一個方法要返回值,那么我們將這個方法視作一個值,若不返回值,那么這個方法就是調用一個語句。
this代表當前對象,這是為了防止對象中的局部變量和類變量重名所用的一個東西。
Java使用棧機制。main方法會在底層,然后不斷地調用其他的方法直到調用結束為止,然后一條一條的消失。
1.方法重載
方法名稱都必須相同,參數列表必須不同,方法的返回值類型可以相同也可以不同,僅僅返回類型不同不足以成為方法的重載。
2.方法的重寫
public class People{public void call(){System.out.println("你好,我是一個人");}} //子類繼承父類,改進方法叫做方法的重寫 public class Workers extends People{public void call(){System.out.println("你好,我是工作者");}} /* 創建對象時的用法:除了傳統的new對象的用法外還有People liLei=new Workers();這個用法也叫做向上轉型,即父類引用指向子類對象。2.類的封裝
封裝,即隱藏對象的屬性和實現細節,僅對外公開接口,控制在程序中屬性的讀和修改的訪問級別
類的封裝有以下好處:
如何去實現類的封裝呢?
3.類的繼承
extends實現類的繼承
類的繼承的語法:
class 子類 extends 父類
類的繼承的好處:
兩個類相似,那么它們會有許多重復的代碼,導致后果就是代碼量大且臃腫,通過繼承就可以解決這個問題。
繼承的特點:
- 子類擁有父類除 private 以外的所有屬性和方法。
- 子類可以擁有自己的屬性和方法。
- 子類可以重寫實現父類的方法。
- Java 中的繼承是單繼承,一個類只有一個父類。
注:Java 實現多繼承的一個辦法是 implements(實現)接口,但接口不能有非靜態的屬性,這一點請注意。
super 關鍵字在子類內部使用,代表父類對象。
4.多態
通過父類就能夠引用不同的子類,這就是多態.
例:
People liLei=new Workers();多態的實現條件
Java 實現多態有三個必要條件:繼承、重寫和向上轉型(即父類引用指向子類對象)。向上轉型,在運行時,會遺忘子類對象中與父類對象中不同的方法,也會覆蓋與父類中相同的方法——重寫(方法名,參數都相同)。
多態的實現方式
Java 中多態的實現方式:繼承父類進行方法重寫,抽象類和抽象方法,接口實現。
5.抽象類
抽象類是父類,抽象類是約束子類必須要實現哪些方法,而并不關注方法如何去實現。
抽象類的實現
例:
//抽象方法 public abstract class TelePhone {public abstract void call(); //抽象方法,打電話public abstract void message(); //抽象方法,發短信 } public class CellPhone extends TelePhone {@Overridepublic void call() {System.out.println("我可以打電話!");}@Overridepublic void message() {System.out.println("我可以發短信!");}public static void main(String[] args) {CellPhone cp = new CellPhone();cp.call();cp.message();}}6.接口
implemens實現接口
接口的語法聲明:
修飾符 interface 接口名稱 [extends 其他的接口名] {
// 聲明變量
// 抽象方法
}
例:
// Animal.java interface Animal {//int x;//編譯錯誤,x需要初始化,因為是 static final 類型int y = 5;public void eat();public void travel(); }接口的實現:
// 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{}注意點:
- 接口不能用于實例化對象。
- 接口中方法只能是抽象方法、default 方法、靜態方法。
- 接口成員是 static final 類型。
- 接口支持多繼承。
7.總結
類是一組具有相同屬性和方法的對象的集合,對象是類的一個實例化。
總結
- 上一篇: 中国第一国产计算机公司,【新中国的第一】
- 下一篇: 类学习(python)