public abstract class Automatic {
// 省略属性
public abstract int pay();
}
public class Car extends Automatic {
public int pay() {
return 5;
}
}
public class Truck {
private int weight;
public Truck(int weight) {
this.weight = weight;
}
public int pay() {
return weight < 20 ? 10 : weight >= 20 && weight <= 30 ? 15 : weight > 30 ? 20 : -1;
}
}
public static void main(String[] args) {
Automatic a = new Truck(27);
shoufei(a);
}
public static void shoufei(Automatic a) {
System.out.println(a.pay());
}
public abstract class Automatic {
private String name;
private String color;
abstract int pay();
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;
}
}
class Car extends Automatic{
@Override
public int pay() {
return 5;
}
}
class Truck extends Automatic{
private int weight;
@Override
public int pay() {
if(this.weight<20){
return 10;
}else if(this.weight>=20 && this.weight<=30){
return 15;
}else if(this.weight>30){
return 20;
}
return 0;
}
public Truck(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
class Bus extends Automatic{
private int num;
@Override
int pay() {
if(this.num<15){
return 10;
}else if(this.num>=15 && this.num<=30){
return 20;
}else if(this.num>30){
return 25;
}
return 0;
}
public Bus(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
class Test{
public static void main(String[] args) {
Automatic carAutomatic = new Car();
shoufei(carAutomatic);
Automatic truck = new Truck(27);
shoufei(truck);
Automatic busAutomatic = new Bus(20);
shoufei(busAutomatic);
}
public static void shoufei(Automatic automatic){
System.out.println("本次收费:"+automatic.pay());
}
}