码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA基础篇—继承

时间:2017-08-18 22:24:47      阅读:205      评论:0      收藏:0      [点我收藏+]

标签: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     @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 }

 

JAVA基础篇—继承

标签:private   nal   code   get   ret   xtend   final   out   ade   

原文地址:http://www.cnblogs.com/lc-java/p/7392317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!