ads2569268136
2018-10-19 08:36
采纳率: 20%
浏览 471
已采纳

求各位大佬帮忙写下java程序,初学多态真的搞不懂研究一天了

图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 创业的鱼 2018-10-19 08:55
    已采纳

    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());
    }

    已采纳该答案
    打赏 评论
  • weixin_40852944 2018-10-19 09:32
     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());
        }
    }
    
    打赏 评论

相关推荐 更多相似问题