类和构造函数抛出错误的类型错误

时间:2018-05-21 12:23:32

标签: java class

你好我已经尝试了几天不断得到同样的错误。 基本上我得到了这个错误: VisaCardDemo.java:24:错误:

public class CreditCard
{
    int numberRepayments = 0;
    int numberDebits = 0;
    double creditCard_Bal;
    double Annual_Interest;
    double monthlyServiceCharges;
    double creditCard_limit;
    //double 


    public CreditCard(double Balance, double interest, double serviceCharge, double limit)
    {
    //I'm unsure if im doing this correctly 

    creditCard_Bal = Balance;
    Annual_Interest = interest;
    monthlyServiceCharges = serviceCharge;
    creditCard_limit = limit;
    //double 
    }
    public void repayment(double repayments){

        creditCard_Bal = creditCard_Bal - repayments;
        numberRepayments = numberRepayments + 1;

    }
    public int debit(double debit){
        creditCard_Bal = creditCard_Bal - debit;
        numberDebits = numberDebits + 1;
        return numberDebits;
    }
    public void calcInterest(){
        //Monthly_Interest_Rate = (Annual_Interest / 12);
        //Monthly_Interest = creditCard_Bal * Monthly_Interest;
        creditCard_Bal = creditCard_Bal * (1 + (Annual_Interest / 12));

    }
    public int monthlyProcess(){
        creditCard_Bal = creditCard_Bal - monthlyServiceCharges;
        calcInterest();
        numberRepayments = 0;
        numberDebits = 0;
        return numberDebits;
    }
    public double setMonthlyServiceCharges(double placeHolder){
        monthlyServiceCharges = placeHolder;
        return monthlyServiceCharges;

    }
    public double getMonthlyServiceCharges(){
        return monthlyServiceCharges;            // the reason that ive only put the variable 
                                         //is because i think the metho will work as 
    }                                    //in the VisaCardDemo
                                         // fprintf("the monthly service charge = " + cosc120VisaCard.getMonthlyServiceCharges)
                                         //will equal fprintf("the monthly service charge =" monthlyServiceCharges )
                                         // please let me know if this is not the case and i need to do more to it
    public double getBalance(){
        return creditCard_Bal;
    }
    public int getNumRepayments(){
        return numberRepayments;

    }
    public int getNumDebits(){
       return numberDebits;
    }
    public double getInterestRate(){
       return Annual_Interest;
    }    
   }

必需:没有参数   发现:双,双,双,双   原因:实际和正式的参数列表长度不同 1错误

基本上我不知道为什么会发生这种与构造函数没有交互的事情

源自此代码:

import java.text.DecimalFormat;

public class VisaCardDemo
{
   public static void main(String[] args)
   {
      // Create a Decimalformat object for formatting output.
      DecimalFormat dollar = new DecimalFormat("#,##0.00");

      // Create a VisaCard object with a $100 balance, 
      // 3% interest rate, a monthly service charge
      // of $2.50, and a credit limit of $500.
      VisaCard cosc120VisaCard = 
                   new VisaCard(100.0, 0.03, 2.50, 500.0);

                   //
                   //
      CreditCard cosc120CreditCard = 
                   new CreditCard(100.0, 0.03, 2.50, 500.0);
                   //
      //
      // Display what we've got.
      System.out.println("Balance: $" + 
                         dollar.format(cosc120VisaCard.getBalance()));
      System.out.println("Number of repayments: " + 
                         cosc120VisaCard.getNumRepayments());
      System.out.println("Number of debits: " + 
                         cosc120VisaCard.getNumDebits());
      System.out.println();

      // Make some repayments.
      cosc120VisaCard.repayment(25.00);
      cosc120VisaCard.repayment(10.00);
      cosc120VisaCard.repayment(35.00);

      // Display what we've done so far.
      System.out.println("Balance: $" + 
                         dollar.format(cosc120VisaCard.getBalance()));
      System.out.println("Number of repayments: " + 
                         cosc120VisaCard.getNumRepayments());
      System.out.println("Number of debits: " + 
                         cosc120VisaCard.getNumDebits());
      System.out.println();

      // Make some debits.
      cosc120VisaCard.debit(450.00);
      cosc120VisaCard.debit(50.00);  // credit card account should become inactive
      cosc120VisaCard.debit(10.00);
      cosc120VisaCard.debit(10.00);
      cosc120VisaCard.debit(10.00);

      // Display what we've done so far.
      System.out.println("Balance: $" + 
                         dollar.format(cosc120VisaCard.getBalance()));
      System.out.println("Number of repayments: " + 
                         cosc120VisaCard.getNumRepayments());
      System.out.println("Number of debits: " + 
                         cosc120VisaCard.getNumDebits());
      System.out.println();

      // Do the monthly processing.
      cosc120VisaCard.monthlyProcess();

      // Display what we've done so far.
      System.out.println("Balance: $" + 
                         dollar.format(cosc120VisaCard.getBalance()));
      System.out.println("Number of repayments: " + 
                         cosc120VisaCard.getNumRepayments());
      System.out.println("Number of debits: " + 
                         cosc120VisaCard.getNumDebits());
   }
}
public class VisaCard extends CreditCard
{                            
   // double visaCard_Bal;
   // double visaCard_Interest;
    //double visaCard_ServiceCharge;
    //double visaCard_Limit;
    boolean tf = false;
    double temporary_storage;

    public VisaCard (double V_Balance, double V_interest, double V_serviceCharge, double V_limit)
    { 
        creditCard_Bal = V_Balance;       //dose this make the VisaCard class the super?

               //end constructor
        }

                public int debit(double debit)             
                {
                if (creditCard_Bal >= creditCard_Bal) 
                    tf = false;
                else{
                    creditCard_Bal = creditCard_Bal - debit;
                    numberDebits = numberDebits + 1;
                    return numberDebits;
                    }
                }

                public double repayments(double money)//calls  the superclass version
                {
                super.repayment(money);

                if (creditCard_limit >= creditCard_Bal) 
                     tf = true;
                }



                public int monthlyProcess()
                {
                if (numberDebits > 10)
                    temporary_storage = monthlyServiceCharges;
                    monthlyServiceCharges = monthlyServiceCharges + (10 - numberDebits);
                    if (creditCard_Bal > creditCard_limit)
                        tf = false;
                    creditCard_Bal = creditCard_Bal + monthlyServiceCharges;
                    super.monthlyProcess();
                    monthlyServiceCharges = temporary_storage;


            }                                       

}
stream1.leftJoin(stream2, valueJoiner);

valueJoiner = (s1, s2) -> {if (s2 != null) {
      return false
   } else {
     return true;
   } 
};

1 个答案:

答案 0 :(得分:0)

首先,错误是完全正确的,它告诉编译器正在寻找什么。它正在寻找没有arg构造函数并找到参数化构造函数。 所以这里有一些基础知识 -

  1. 只有在没有看到任何构造函数的情况下,Java才会在类中创建默认构造函数。在这里,您创建了自己的参数化缺点。所以java没有提供她自己的默认缺点。
  2. 无论是写还是不写,子类构造函数的第一次调用都是对父类构造函数的调用。如果你写明确,那么你可以调用super()或super(args)。如果你不打电话超级利弊。在儿童阶层利弊的第一线。然后java会自动将super()调用放到父类默认缺点。
  3. 在您的情况下,您没有默认缺点。在信用卡类中并定义了参数化缺点。因此java没有提供它的默认缺点。 现在在儿童阶层的缺点。 visacard,你没有打电话给任何利弊。在第一行

    public CreditCard(){
       //default cons
    }
    

    所以即使你没有进行任何超级调用,但java默认提供了super()。现在您的父类缺少​​类似下面的内容

    {{1}}

    所以它必然会失败并失败。在你定义无争论缺点之前,它会一直失败。在父类或调用参数化的缺点。明确地在你的孩子阶级缺点。非常第一线。 您还可以访问以下链接 -

    1. if constexpr
    2. https://stackoverflow.com/a/2054040
    3. Why do this() and super() have to be the first statement in a constructor?
    4. 作为最佳实践,默认缺点是好的。如果你正在创建参数化的利弊。在你的班级(直到你确定有人永远不会继承它)。你永远不知道谁成为你班上的孩子,也不会叫参数化的缺点。你的父类,因此它将失败此错误。 Java会寻找缺点。没有参数但会找到带参数的东西。

      希望它有所帮助。