JAVA基础篇—继承

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA基础篇—继承相关的知识,希望对你有一定的参考价值。

父类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基础篇—继承的主要内容,如果未能解决你的问题,请参考以下文章

JAVA基础篇—继承

学习大数据:Java基础篇之继承

学习大数据:Java基础篇之继承

Java基础知识回顾之三 ----- 封装继承和多态

Java基础知识回顾之三 ----- 封装继承和多态

一脚踩进java之基础篇19——面向对象 (final,static关键字)