欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

类....

发布时间:2024/1/18 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 类.... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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.类的封装

封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别

类的封装有以下好处:

  • 只能通过规定的方法访问数据。
  • 隐藏类的实例细节,方便修改和实现。
  • 如何去实现类的封装呢?
  • 修改属性的可见性,在属性的前面添加修饰符 (private)
  • 对每个值属性提供对外的公共方法访问,如创建 getter/setter(取值和赋值)方法,用于对私有属性的访问
  • 在 getter/setter 方法里加入属性的控制语句,例如我们可以加一个判断语句,对于非法输入给予否定。
  • public class People {//属性(成员变量)有什么,前面添加了访问修饰符private//变成了私有属性,必须通过方法调用private double height; //身高//属性已经封装好了,如果用户需要调用属性//必须用getter和setter方法进行调用//getter和setter方法需要程序员自己定义public double getHeight(){//getter 方法命名是get关键字加属性名(属性名首字母大写)//getter 方法一般是为了得到属性值return height;}//同理设置我们的setter方法//setter 方法命名是set关键字加属性名(首字母大写)//setter 方法一般是给属性值赋值,所以有一个参数public void setHeight(double newHeight){height = newHeight;} } public class NewObject {public static void main(String[] args) {People LiLei = new People(); //创建了一个People对象LiLei//利用setter方法为属性赋值LiLei.setHeight(170.0);//利用getter方法取属性值System.out.println("LiLei的身高是"+LiLei.getHeight());} } public void setAge(int age) {//this指代的就是已创建的对象,使用this就是为了防止重名this.age = age; } public int getAge() {return age; }

    3.类的继承

    extends实现类的继承

    类的继承的语法:

    class 子类 extends 父类

    类的继承的好处:

    两个类相似,那么它们会有许多重复的代码,导致后果就是代码量大且臃肿,通过继承就可以解决这个问题。

    继承的特点:

    • 子类拥有父类除 private 以外的所有属性和方法。
    • 子类可以拥有自己的属性和方法。
    • 子类可以重写实现父类的方法。
    • Java 中的继承是单继承,一个类只有一个父类。
    class Dog extends Animal {... } public class Animal {public int legNum; //动物四肢的数量//类方法public void bark() {System.out.println("动物叫!");} } public class Dog extends Animal { } public class Test{public static void main(String[] args) {Dog a = new Dog();a.legNum = 4;a.bark();} }

    注:Java 实现多继承的一个办法是 implements(实现)接口,但接口不能有非静态的属性,这一点请注意。

    super 关键字在子类内部使用,代表父类对象。
  • 访问父类的属性 super.属性名。
  • 访问父类的方法 super.bark()。
  • 子类构造方法需要调用父类的构造方法时,在子类的构造方法体里最前面的位置:super()。
  • 4.多态

    通过父类就能够引用不同的子类,这就是多态.

    例:

    People liLei=new Workers();
    多态的实现条件

    Java 实现多态有三个必要条件:继承、重写和向上转型(即父类引用指向子类对象)。向上转型,在运行时,会遗忘子类对象中与父类对象中不同的方法,也会覆盖与父类中相同的方法——重写(方法名,参数都相同)。

    多态的实现方式

    Java 中多态的实现方式:继承父类进行方法重写,抽象类和抽象方法,接口实现。

    5.抽象类

    抽象类是父类,抽象类是约束子类必须要实现哪些方法,而并不关注方法如何去实现。

    抽象类的实现
  • 用 abstract 修饰符定义抽象类。
  • 用 abstract 修饰符定义抽象方法,只用声明,不需要实现。
  • 包含抽象方法的类就是抽象类。
  • 抽象类中可以包含普通的方法,也可以没有抽象方法。(这也正是抽象类和接口的区别所在)
  • 抽象类的对象不能直接创建,通常是定义引用变量指向子类对象。
  • 例:

    //抽象方法 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.总结

    类是一组具有相同属性和方法的对象的集合,对象是类的一个实例化。

    总结

    以上是生活随笔为你收集整理的类....的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。