Java考试编程,小汽车定义成抽象类,成员方法定义成抽象方法,重写实现抽象方法,怎么用子类实现抽象方法,考试编程题,在详细解释下,谢谢,
1 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
//file1 package com.car; public abstract class Car { private String name; private String color; public abstract String getType(); public String getName() { return name; } public void setName(String name) { this .name = name; } public String getColor() { return color; } public void setColor(String color) { this .color = color; } } //file2 package com.car; public class BMWCar extends Car { private String type; public BMWCar(String name, String color) { setName(name); setColor(color); this .type= "BMW" ; } @Override public String getType() { return type; } public static void main(String[] args) { Car car = new BMWCar( "baoma" , "white" ); System.out.println(car.getType()); } } |
添加回答
举报
0/150
提交
取消