日本亚洲欧美日韩中文字幕_精品欧美一区二区三区久久久_久久av高潮av无码av_成在人线av无码免费_亚洲中文字幕无码久久精品1

13140882082

全國統(tǒng)一學(xué)習(xí)專線 8:30-21:00

南寧達(dá)內(nèi):Java類繼承以及接口實(shí)現(xiàn)實(shí)例


  Java類繼承以及接口實(shí)現(xiàn)實(shí)例:這個實(shí)例非常容易理解,貼在這里與大家共享。

  1 //抽象父類,交通工具(Vehicle) 2 public abstract class Vehicle{ 3

  private String name; 4

  private double cost; 5

  6

  //無參構(gòu)造函數(shù)

  7

  public Vehicle(){ 8

  } 9 10

  //有參構(gòu)造函數(shù) 11

  public Vehicle(String name, double cost){ 12

  this.name = name; 13

  this.cost = cost; 14

  } 15 16

  //父類的抽象方法,在子類中要具體實(shí)現(xiàn)。

  17

  public abstract void run();

  18

  public abstract void stop(); 19 20

  //getter方法

  21

  public String getName(){ 22

  return this.name; 23

  } 24 25

  public double getCost(){ 26

  return this.cost; 27

  } 28 29

  //setter方法 30

  public void setName(String name){ 31

  this.name = name; 32

  } 33 34

  public void setCost (double cost){ 35

  this.cost = cost; 36

  }

  37 } 38 39 40 41 //上繳養(yǎng)路費(fèi)的接口 42 public interface interRoadTax{ 43

  public void tax(); 44 } 45 46 47 48 //子類自行車Bike,繼承Vehicle 49 public class Bike extends Vehicle{ 50

  private int bikeType; 51 52

  //無參構(gòu)造函數(shù) 53

  public Bike(){} 54

  55

  //有參構(gòu)造函數(shù) 56

  public Bike(String name, double cost, int type){ 57

  super(name,cost); //通過父類方法進(jìn)行賦值 58

  this.bikeType = type; 59

  } 60 61

  //setter方法

  62

  public void setBikeType(int type){ 63

  this.bikeType = type; 64

  } 65

  66

  //getter方法 67

  public int getBikeType(){ 68

  return this.bikeType; 69

  } 70

  71

  //繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。 72

  public void run(){ 73

  System.out.println("i am bike and running……"); 74

  } 75 76

  //繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。

  77

  public void stop(){ 78

  System.out.println("i am bike and stopping……"); 79

  } 80 } 81 82 83 84 //子類汽車Car,繼承Vehicle,同時實(shí)現(xiàn)interRoadTax接口 85 public class Car extends Vehicle implements interRoadTax{ 86

  private int numberOfPeople; 87

  88

  //無參構(gòu)造函數(shù) 89

  public Car(){} 90

  91

  //有參構(gòu)造函數(shù) 92

  public Car(String name, double cost, int numberOfPeople){ 93

  super(name,cost);//通過父類方法進(jìn)行賦值 94

  this.numberOfPeople = numberOfPeople; 95

  } 96

  97

  //getter方法 98

  public int getNumberOfPeople(){ 99

  return this.numberOfPeople;100

  }101

  102

  public void setNumberOfPeople(int numberOfPeople){103

  this.numberOfPeople = numberOfPeople;104

  }105

  106

  //繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。107

  public void run(){108

  System.out.println("i am car and running……");109

  }110 111

  //繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。112 public void stop(){113

  System.out.println("i am car and stopping……");114

  }115 116

  //實(shí)現(xiàn)上繳養(yǎng)路費(fèi)的接口方法

  117

  public void tax(){118

  System.out.println("the car tax is " + getCost() * 0.1);119

  }

  120 }121 122 123 124 //子類摩托車Motor,繼承Vehicle,同時實(shí)現(xiàn)interRoadTax接口125 public class Motor extends Vehicle implements interRoadTax{126

  public Motor(){}127

  public Motor(String name,double cost){128

  super(name,cost);129

  }130

  131

溫馨提示:為不影響您的學(xué)業(yè),來校區(qū)前請先電話咨詢,方便我校安排相關(guān)的專業(yè)老師為您解答

姓名不能為空
手機(jī)號格式錯誤