java--面向对象之继承2
2017-12-12 09:27
190 查看
Auto:
package JC1;
public class Auto {
private String wheel;
private String color;
private int weight;
private double speed;
public Auto(String wheel, String color, int weight, double speed) {
this.wheel = wheel;
this.color = color;
this.weight = weight;
this.speed = speed;
}
public void speedUp(){
this.speed+=10;
System.out.println("白色的"+this.color+",重量为"+this.weight+"kg"+",速度为"+this.speed+"km/h行驶!");
}
public void speedDown(){
if(this.speed-10>0){
this.speed-=10;
System.out.println("白色的"+this.color+",重量为"+this.weight+"kg"+",速度为"+this.speed+"km/h减速行驶!");
}
}
public void stop(){
this.speed=0;
System.out.println("车已停止!"+this.speed+"km/h");
}
public String getWheel() {
return wheel;
}
public String getColor() {
return color;
}
public int getWeight() {
return weight;
}
public double getSpeed() {
return speed;
}
}
Car:
package JC1;
public class Car extends Auto {
private String aircondition;
private String CD;
public Car(String wheel, String color, int weight, double speed,
String aircondition, String cD) {
super(wheel, color, weight, speed);
this.aircondition = aircondition;
CD = cD;
}
//重写
public void speedUp(){
System.out.println("白色的"+super.getColor()+",重量为"+super.getWeight()+"kg"+",速度为"+super.getSpeed()+"km/h行驶!");
}
public void speedDown(){
System.out.println("白色的"+super.getColor()+",重量为"+super.getWeight()+"kg"+",速度为"+super.getSpeed()+"km/h减速行驶!");
}
}
Test:
package JC1;
import org.junit.Test;
public class jTest {
@Test
public void test1(){
Auto auto=new Auto("4", "black", 50000, 40);
Car car=new Car("4","white",52000,65, "美的", "eason");
//System.out.println(auto.getSpeed());
auto.speedUp();
auto.speedUp();
auto.speedUp();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.stop();
}
}
package JC1;
public class Auto {
private String wheel;
private String color;
private int weight;
private double speed;
public Auto(String wheel, String color, int weight, double speed) {
this.wheel = wheel;
this.color = color;
this.weight = weight;
this.speed = speed;
}
public void speedUp(){
this.speed+=10;
System.out.println("白色的"+this.color+",重量为"+this.weight+"kg"+",速度为"+this.speed+"km/h行驶!");
}
public void speedDown(){
if(this.speed-10>0){
this.speed-=10;
System.out.println("白色的"+this.color+",重量为"+this.weight+"kg"+",速度为"+this.speed+"km/h减速行驶!");
}
}
public void stop(){
this.speed=0;
System.out.println("车已停止!"+this.speed+"km/h");
}
public String getWheel() {
return wheel;
}
public String getColor() {
return color;
}
public int getWeight() {
return weight;
}
public double getSpeed() {
return speed;
}
}
Car:
package JC1;
public class Car extends Auto {
private String aircondition;
private String CD;
public Car(String wheel, String color, int weight, double speed,
String aircondition, String cD) {
super(wheel, color, weight, speed);
this.aircondition = aircondition;
CD = cD;
}
//重写
public void speedUp(){
System.out.println("白色的"+super.getColor()+",重量为"+super.getWeight()+"kg"+",速度为"+super.getSpeed()+"km/h行驶!");
}
public void speedDown(){
System.out.println("白色的"+super.getColor()+",重量为"+super.getWeight()+"kg"+",速度为"+super.getSpeed()+"km/h减速行驶!");
}
}
Test:
package JC1;
import org.junit.Test;
public class jTest {
@Test
public void test1(){
Auto auto=new Auto("4", "black", 50000, 40);
Car car=new Car("4","white",52000,65, "美的", "eason");
//System.out.println(auto.getSpeed());
auto.speedUp();
auto.speedUp();
auto.speedUp();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.stop();
}
}
相关文章推荐
- Java面向对象(二)面向对象的特征 --- 封装 、 继承、 多态
- JAVA基础学习(七)---面向对象三---继承,接口与 抽象类
- 黑马程序员--03.Java面向对象--05.【继承】【子父类中成员的特点】【子父类中构造函数的特点】
- Java面向对象之继承
- Java第四次作业--面向对象高级特性(继承和多态)
- java面向对象基本特征:封装、继承、多态
- java面向对象浅析--抽象类、接口与多继承
- Java面向对象基本特征:继承
- 2、Java面向对象笔记(继承)(抽象类)(多态)
- 笔记——Java面向对象之继承
- 复习java面向对象(继承)
- JAVA进阶 面向对象程序设计——第4章 继承与多态【不同访问属性使子类在使用这些成员时不同】
- java面向对象基本思想--封装继承多态
- JAVA面向对象2-静态;继承;重写
- 黑马程序员-JAVA面向对象之继承
- java面向对象之继承
- Java面向对象之继承
- java 面向对象中的继承
- 重新认识java(三) ---- 面向对象之继承!
- 设计模式学习(一):java 面向对象特征:抽象、封装、多态、继承