【正文】
*/ public interface SolarPoweredMotor extends Motor { abstract public int getLumensToOperate()。 */ public interface BatteryPoweredMotor extends Motor { public int getTimeToRecharge()。 } } 實(shí)現(xiàn)接口 MyInterface 實(shí)現(xiàn)接口中的方法 ICMSP 演示 在 Java中使用接口 接口演示 ICMSP 多繼承 + g e t H o r s e p o w e r ( ) : i n t《 i n t e r f a c e 》 M o r t o r+ g e t T i m e T o R e c h a r g e ( ) : i n t《 i n t e r f a c e 》 B a t t e r y P o w e r e d M o t o r+ g e t L u m e n s T o O p e r a t e ( ) : i n t《 i n t e r f a c e 》 S o l a r P o w e r e d M o t o r+ m y C u s t o m M e t h o d ( ) : i n t+ g e t T i m e T o R e c h a r g e ( ) : i n t+ g e t L u m e n s T o O p e r a t e ( ) : i n t+ g e t H o r s e p o w e r ( ) : i n tD u a l P o w e r e d M o t o r發(fā)動(dòng)機(jī) 電驅(qū)動(dòng)發(fā)動(dòng)機(jī) 光驅(qū)動(dòng)發(fā)動(dòng)機(jī) 光電驅(qū)動(dòng)發(fā)動(dòng)機(jī) ICMSP 多繼承 /*查詢發(fā)動(dòng)機(jī)的馬力,發(fā)動(dòng)機(jī)將返回一個(gè)表示馬力的整數(shù) */ public interface Motor { public int getHorsepower()。 } void display(int x,int y,int z) { add(x,y)。 } 接口中的方法必須為 public修飾的 接口中只能定義 public類型的靜態(tài)常量 ICMSP 接口的實(shí)現(xiàn) class UserInterface implements MyInterface { public void add(int x, int y) { (x + y = + (x+ y))。 } // 定義程序使用的常量的接口 public interface myconstants { public static final double price = 。 } } ICMSP 演示 在 Java中使用 abstract修飾符 abstract修飾符演示 ICMSP 接口 ?接口就是某個(gè)事物對(duì)外提供的一些功能的聲明 ?可以利用接口實(shí)現(xiàn)多態(tài),同時(shí)接口也彌補(bǔ)了 Java單一繼承的弱點(diǎn) ?使用 interface關(guān)鍵字定義接口 ?一般使用接口聲明方法或常量,接口中的方法只能是聲明,不能是具體的實(shí)現(xiàn) ICMSP 接口的創(chuàng)建方法 // 定義方法的接口 public interface myinterface { public void add(int x, int y)。 r = rr。 r = wr。 WalkingRobot wr = new WalkingRobot(Lisa,blue,4)。 … … (機(jī)器人的驅(qū)動(dòng)數(shù)量 + drivenumber)。 = drivenumber。 } } 實(shí)現(xiàn)抽象父類中的抽象方法 class DrivingRobot extends Robot{ int drivenumber。 (“機(jī)器人的顏色 + color)。 } void show() { (“從子類 WalkingRobot 中輸出的信息 )。 } abstract void show()。 Robot(String name,String color) { = name。 ?聲明抽象類語(yǔ)法: abstract class{…… } ?父類中的某些抽象不包含任何邏輯,并需要在子類中重寫,子類提供這種抽象方法的實(shí)現(xiàn)細(xì)節(jié) ICMSP abstract修飾符 ?抽象類不能被實(shí)例化 ?構(gòu)造方法和靜態(tài)方法不能是抽象的 ?父類的抽象方法往往在子類中實(shí)現(xiàn) abstract class Robot { protected String name = Anna。 (==============================)。 (名稱 + name)。 = color。 protected String color = Pink。 final變量 的值不能被修改 final修飾變量 ,則成為常量 例如: final int variableName = 12。 ?對(duì)于類、方法和變量, final的含義分別為: final類 不能被繼承 。 ()。 } … … } 靜態(tài)方法 用于輸出靜態(tài)變量 public class DemoStatic { public static void main(String[] args) { Robot r1 = new Robot(第 1個(gè)機(jī)器人 ,color1)。 count++。 } public Robot(String name,String color) { = name。 String color = Pink。不能使用 s