public abstract class Vehicle {
10年的西夏网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整西夏建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“西夏网站设计”,“西夏网站推广”以来,每个客户项目都认真落实执行。
public int wheelNum = 4;//轮子数量
public abstract void getCharge();//计算收费的抽象方法
public abstract void getWheeiNum(int wheelNum);//得到轮子数的方
}
public class Taxi extends Vehicle{
private double qibujia;//起步价
private double danjia;//单价
private double lucheng;//里程数
public double getQibujia() {
return qibujia;
}
public void setQibujia(double qibujia) {
this.qibujia = qibujia;
}
public double getDanjia() {
return danjia;
}
public void setDanjia(double danjia) {
this.danjia = danjia;
}
public double getLucheng() {
return lucheng;
}
public void setLucheng(double lucheng) {
this.lucheng = lucheng;
}
@Override
public void getCharge() {
// TODO Auto-generated method stub
double shoufei = qibujia+(lucheng - 3.0)* danjia;
System.out.println("出租车的收费为:"+shoufei);
}
@Override
public void getWheeiNum(int wheelNum) {
this.wheelNum = wheelNum;
System.out.println("出租车的轮子有"+wheelNum+"个");
}
}
public class Pedicab extends Vehicle{
public Pedicab(int wheelNum){
this.wheelNum = 3;
}
@Override
public void getCharge() {
double shoufei = 3;
System.out.println("三轮车的收费为:"+shoufei);
}
@Override
public void getWheeiNum(int wheelNum) {
System.out.println("三轮车的轮子有"+wheelNum+"个");
}
}
public class Test {
public static void main(String[] args) {
Taxi t = new Taxi();
//创建一个出租车类的对象,并为各个属性赋初值
t.setQibujia(5.0);
t.setDanjia(3.0);
t.setLucheng(8.0);
//创建一个三轮车类的对象,调用该类的构造将轮子数初始化
Pedicab p = new Pedicab(3);
//调用出租车类的相应方法,输出收费和轮子数
t.getCharge();
t.getWheeiNum(4);
//调用三轮车类相应方法,输出三轮车对象的收费和轮子数
p.getCharge();
p.getWheeiNum(3);
}
}
除去测试,简单写一下你的程序。
public interface traffic {
void start();
void stop();
}
public class car implements traffic {
public void start() {
//这里写汽车启动
System.out.println("car is start");
}
public void stop() {
//这里写汽车停止
System.out.println("car is stop");
}
}
public class plane implements traffic {
public void start() {
//这里写飞机启动
System.out.println("plane is start");
}
public void stop() {
//这里写飞机停止
System.out.println("plane is stop");
}
}
package com.test;
/*
定义一个表示交通工具的抽象类(name(名字)、price(价格)、manufacturer(制造商),抽象方法有停止stop)
。定义一个EngineInterf表示发动机的接口,该接口中的方法有启动start,start方法具有一个参数来表示该发动机采用的燃料类型,
比如家用轿车采用汽油,飞机采用航空煤油、卡车采用柴油等等)。
定义一个飞机类和家用轿车类,并进行测试。*/
public abstract class Vehicle {
public String name;
public double price;
public String manufacturer;
public abstract void stop();
public static void main(String[] args) {
EngineInterf e = new Aeroplane();
e.start("煤油");
EngineInterf car = new Car();
car.start("汽油");
}
}
interface EngineInterf {
public void start(String flag);
}
class Aeroplane extends Vehicle implements EngineInterf {
@Override
public void start(String flag) {
System.err.println("我是用" + flag);
}
@Override
public void stop() {
}
}
class Car extends Vehicle implements EngineInterf {
@Override
public void start(String flag) {
System.err.println("我是用" + flag);
}
@Override
public void stop() {
}
}
class Vehicle {
int Speed;
public String getKind() {
return Kind;
}
public void setSpeed(int speed) {
Speed = speed;
}
public void setColor(String color) {
Color = color;
}
public void setKind(String kind) {
Kind = kind;
}
String Kind;
String Color;
public String getColor() {
return Color;
}
public Vehicle() {
// 默认构造方法
}
public Vehicle(int Speed, String Kind, String Color) {
this.Speed = Speed;
this.Kind = Kind;
this.Color = Color;
}
}
class Car extends Vehicle {
private int passenger;
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public Car() {
// 默认构造方法
}
public Car(int Speed, String Kind, String Color, int passenger) {
this.Speed = Speed;
this.Kind = Kind;
this.Color = Color;
this.passenger = passenger;
}
}
class Demo {
public static void main(String[] args) {
Car car =new Car(120,"宝马","红",5);
car.setSpeed(160);
car.setColor("黄");
car.setKind("奥迪");
System.out.print(car.getKind()+"\t"+car.getColor()+"\t"+car.Speed+"\t"+car.getPassenger());
}
}
public class Vehicle {
private int speed;
private String kind;
private String color;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public Vehicle(){
this.setSpeed(20);
this.setKind("自行车");
this.setColor("红");
}
public static void main(String[] args) {
Vehicle v = new Vehicle();
System.out.println("颜色:"+v.getKind()+" 类型:"+v.getKind()+" 速度:"+v.getSpeed());
v.setColor("黄");
v.setKind("汽车");
v.setSpeed(60);
System.out.println("颜色:"+v.getKind()+" 类型:"+v.getKind()+" 速度:"+v.getSpeed());
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款