代码改变世界

JAVA基础篇—继承

2017-08-18 21:33  lc_java  阅读(199)  评论(0编辑  收藏  举报

父类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     @Override
21     public  void run() {
22         // TODO Auto-generated method stub
23         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 }