標籤:private nal code get ret xtend final out ade
父類Vehicle
package com.car;public class Vehicle { final String brand;// String color;// double speed;// public Vehicle( String b,String c){ this.brand=b; this.color=c; this.speed=0.0; }public String getColor() {return color;}public String getBrand() {return brand;}public void setColor(String color) {this.color = color;}public double getSpeed() {return speed;}public void setSpeed(double speed) {this.speed = speed;} public void run(){ System.out.println("車開的6"); } public static void main(String[] args) {Vehicle s=new Vehicle("benz","black");System.out.printf(s.brand+s.color);s. run();}}
子類Car
1 package com.car; 2 3 public class Car extends Vehicle{ 4 public Car(String b, String c, int loader) { 5 super(b, c); 6 this.loader = loader; 7 super.speed=0; 8 } 9 private int loader;//10 11 public int getLoader() {12 return loader;13 }14 15 public void setLoader(int loader) {16 this.loader = loader;17 }18 19 20 @Override21 public void run() {22 // TODO Auto-generated method stub23 System.out.println("不怎麼6");24 }25 26 public static void main(String[] args) {27 Car d=new Car("Honda", "red",2);28 System.out.printf(d.brand+d.color+d.loader);29 d.run();30 31 }32 }
JAVA基礎篇—繼承