【问题标题】:How to stop printing half of my print method如何停止打印我的打印方法的一半
【发布时间】:2018-10-16 06:40:30
【问题描述】:

我只是需要一些帮助来停止打印方法。它将我的输出打印两次作为 car1.print(); car2.print();在底部的打印方法中。我如何在不删除它的情况下排除它。它必须放在 super.print() 部分。

class Vehicle {  // base class

   int capacity;
   String make;

   Vehicle(int theCapacity, String theMake) {
      capacity = theCapacity;
      make = theMake;
   }

   void print() {
      System.out.println("Vehicle Info:");
      System.out.println("  capacity = " + capacity + "cc" );
      System.out.println("  make = " + make );
   }
}

class Car extends Vehicle {
   public String type;
   public String model;

   public Car(int theCapacity, String theMake, String theType, String theModel) {
      super(theCapacity, theMake); 
      type = theType;
      model = theModel;
      
      super.print(); 
      {
         System.out.println("  type = " + theType);
         System.out.println("  Model = " + theModel);
      }
   }
}


class Task1 {

   public static void main(String[] args) {
      Car car1 = new Car(1200,"Holden","sedan","Barina");
      Car car2 = new Car(1500,"Mazda","sedan","323");
      car1.print();
      car2.print();
   }
}

【问题讨论】:

标签: java class inheritance superclass


【解决方案1】:

您可以在构造函数中使用super 关键字来调用超类的构造函数并将参数传递给它。 注意它必须是构造函数中的第一条语句:

class Car extends Vehicle {
   public String type;
   public String model;


   public Car(int theCapacity, String theMake, String theType, String theModel) {
      super(theCapacity, theMake); // Here
      type = theType;
      model = theModel;
   }
}

【讨论】:

    【解决方案2】:

    你错过了Constructor

    public Car (int theCapacity, String theMake, String theType, String theModel) {
      capacity = theCapacity;
      make = theMake;
      Type = theType;
      Model = theModel;
    }
    

    public Car (int theCapacity, String theMake, String theType, String theModel) {
      super (theCapacity, theMake);
      Type = theType;
      Model = theModel;
    }
    

    【讨论】:

      【解决方案3】:

      您必须通过在子类中传递简单的参数来调用超级构造函数。

      public Car(int capacity, String make, String type, String model) {
            super(capacity, make); // simply call super
            this.type = type;
            this.model = model;
         } 
      

      【讨论】:

        【解决方案4】:

        其中一个解决方案是使用 super 关键字从子类调用基类构造函数,并从子类构造函数中添加其他参数,如 @Mureinik 所述

        根据基类的要求,您也可以尝试使用抽象方法。示例代码如下。

        abstract class Vehicle {
        
           static int capacity;
           static String make;
        
           Vehicle(int theCapacity, String theMake) {
              capacity = theCapacity;
              make = theMake;
           }
        
           protected static void print() {
              System.out.println("Vehicle Info:");
              System.out.println("  capacity = " + capacity + "cc" );
              System.out.println("  make = " + make );
              // you can use these methods where you want in this base class.
              System.out.println("  type = " + getType());
              System.out.println("  model = " + getModel());
        
           }
           protected abstract  String getType();
           protected abstract  String getModel();
        }
        
        
        public class Car extends Vehicle{
        
            Car(int theCapacity, String theMake) {
                super(theCapacity, theMake);
            }
        /**
         * @param args
         */
            public static void main(){
        
                print();
            }
        
            @Override
            protected String getType() {
                // TODO Auto-generated method stub
                return "Audi";
            }
            @Override
            protected String getModel() {
                // TODO Auto-generated method stub
                return "Q7";
            }
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-11-30
          • 2022-12-05
          • 2012-04-08
          • 2021-12-29
          • 2021-09-12
          • 2021-12-05
          • 2013-07-23
          相关资源
          最近更新 更多