Интерфейсы и наследование
DESCRIPTION
Интерфейсы и наследование. Интерфейсы. Объявление public interface OperateCar { // constant declarations, if any // method signatures int turn(Direction direction , double radius, double startSpeed ); int changeLanes (Direction direction , double startSpeed , double endSpeed ); - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/1.jpg)
Интерфейсы и наследование
![Page 2: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/2.jpg)
Интерфейсы
• Объявлениеpublic interface OperateCar { // constant declarations, if any // method signatures int turn(Direction direction, double radius, double startSpeed); int changeLanes(Direction direction, double startSpeed, double endSpeed);}
• Реализацияpublic class OperateBMW760i implements OperateCar {
// the OperateCar method signatures, with implementation , for example: int signalTurn(Direction direction, boolean signalOn) {
// code to turn BMW's LEFT turn indicator lights on // code to turn BMW's LEFT turn indicator lights off
}
// other members, as needed -- for example, helper classes not // visible to clients of the interface }
![Page 3: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/3.jpg)
Расширение
• Интерфейсовpublic interface GroupedInterface extends Interface1, Interface2, Interface3 {
// constant declarations // base of natural logarithms double E = 2.718282; // method signatures void doSomething (int i, double x); int doSomethingElse(String s);
}
![Page 4: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/4.jpg)
Наследованиеpublic class Bicycle {
//implement}
public class MountainBike extends Bicycle { // the MountainBike subclass adds one field public int seatHeight;
// the MountainBike subclass has one constructor public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } }
![Page 5: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/5.jpg)
Приведение типов
MountainBike myBike = new MountainBike(0, 0, 0, 0);Object obj = new MountainBike(0, 0, 0, 0);MountainBike myBike1 = (MountainBike)obj;if (obj instanceof MountainBike) {
MountainBike myBike2 = (MountainBike)obj;}
![Page 6: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/6.jpg)
Полиморфизм
Демо TestBike
![Page 7: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/7.jpg)
Final и Астрактны Классы
• Finalfinal class ChessAlgorithm {
enum ChessPlayer { WHITE, BLACK } ... final ChessPlayer getFirstPlayer() {
return ChessPlayer.WHITE; } ...
}
• Abstractpublic abstract class GraphicObject {
// declare fields // declare non-abstract methods abstract void draw();
}
![Page 8: Интерфейсы и наследование](https://reader035.vdocument.in/reader035/viewer/2022062315/56816459550346895dd62a96/html5/thumbnails/8.jpg)
Q&A