2017-11-04 1 views
-4

Dies ist Waffle.java, es ist abgeschlossen Erfolg nach dem kompiliert.Java erfordern Konstruktor und super, aber ich habe bereits definiert

public class Waffle extends Customer{ 

    int waffle_favourite; 
    double amount1; 

     static double Waffle(int waffle_favourite, double amount1){ 

if(waffle_favourite == 1){ 
    System.out.println("You have picked CHOCHOLATE."); 
    System.out.println("The price is RM0.80."); 
     amount1 = 0.80; 
     return amount1;} 

    else if(waffle_favourite == 2){ 
    System.out.println("You have picked HONEY."); 
    System.out.println("The price is RM1.00."); 
     amount1 = 1.00; 
     return amount1;} 

    else if(waffle_favourite == 3){ 
    System.out.println("You have picked YOGURT."); 
    System.out.println("The price is RM1.50."); 
     amount1 = 1.50; 
     return amount1;} 

    else{ 
    System.out.println("Invalid value.");}return amount1;}} 

Dies ist Waffle2.java. CMD wurde benötigt, Konstruktor, aber ich habe bereits Waffle.java eingebaut. Wie kann ich es lösen ??

import java.util.Scanner; 

    public class Waffle2 extends Waffle{ 

    Scanner scan = new Scanner(System.in); 

    int waffle_state; 
    double amount2; 
    double total_amount; 
    double payment; 

    public Waffle2(int waffle_favourite, double amount1, int waffle_state, double amount2, double total_amount){ 
    super(waffle_favourite, amount1); 

    if(waffle_state == 1){ 
     System.out.println(""); 
    System.out.println("You have picked SOFT."); 
    System.out.println("The price is RM1.00."); 
     amount2 = 1.00; 
     total_amount = amount1 + amount2; 
     System.out.println("Your total amount is:RM " + total_amount); 
     System.out.println(""); 

for(int i=0; i <= 1; i++){ 

    System.out.print("Please keyin your payment:RM"); 
    payment = scan.nextDouble(); 

    if(payment < total_amount){ 
     total_amount -= payment; 

    System.out.println("You still need to pay RM:" + total_amount);} 

    else if(payment == total_amount){ 
     System.out.println("Thank you for your order."); 
      break;}    
else{ 
     System.out.println("Invalid value.");}}} 

    else if(waffle_state == 2){ 
     System.out.println(""); 
    System.out.println("You have picked CRISPY."); 
    System.out.println("The price is RM1.20."); 
     amount2 = 1.20; 
     total_amount = amount1 + amount2; 
     System.out.println("Your total amount is:RM " + total_amount); 
     System.out.println(""); 

    for(int i=0; i <= 1; i++){ 

    System.out.print("Please keyin your payment:RM"); 
    payment = scan.nextDouble(); 

    if(payment < total_amount){ 
     total_amount -= payment; 

    System.out.println("You still need to pay RM:" + total_amount);} 

    else if(payment == total_amount){ 
     System.out.println("Thank you for your order."); 
      break;}    
else{ 
     System.out.println("Invalid value.");}}} 
    else if(waffle_state == 3){ 
     System.out.println(""); 
    System.out.println("You have picked CHEWY."); 
    System.out.println("The price is RM1.50."); 
     amount2 = 1.50; 
     total_amount = amount1 + amount2; 
     System.out.println("Your total amount is:RM " + total_amount); 
     System.out.println(""); 

    for(int i=0; i <= 1; i++){ 

    System.out.print("Please keyin your payment:RM"); 
    payment = scan.nextDouble(); 

    if(payment < total_amount){ 
     total_amount -= payment; 

    System.out.println("You still need to pay RM:" + total_amount);} 

    else if(payment == total_amount){ 
     System.out.println("Thank you for your order."); 
      break;}    
else{ 
     System.out.println("Invalid value.");}}}}} 

ich diese Klasse zusammengestellt haben, kommt nach kompiliert aus dieser Nachricht nicht Symbol Symbol finden: Konstruktor Waffle (int, double) Lage: Klasse Waffle Super (waffle_favourite, amount1); ^ 1 Fehler

Ich habe viele Art und Weise versuchen, aber immer noch Fehler passieren, thx für HELP.

+2

poste deinen Code nicht als Bild –

+0

'Waffle' hat keinen Konstruktor. Das ist eine statische Methode. –

+1

Siehe [Wie stelle ich eine gute Frage?] (Https://stackoverflow.com/help/how-to-ask) – khelwood

Antwort

0

Waffle hat keinen Konstruktor, außer dem Standardkonstruktor no-arg. Dies:

static double Waffle(int waffle_favourite, double amount1){ 

ist kein Konstruktor. Sie können das sagen, weil a) es static ist, b) es hat einen Rückgabetyp; Konstrukteure haben keine.

Als solche können Sie super(waffle_favourite, amount1); nicht aufrufen, da es keinen solchen Konstruktor gibt. Das einzige, was Sie dort aufrufen können, ist super(), aber Sie müssen das nie explizit aufrufen. Sie können jedoch Waffle.Waffle(waffle_favourite, amount1) aufrufen, wenn Sie möchten.

Aber es sieht überhaupt nicht wie ein Konstruktor aus: Konstruktoren Konstrukt die Instanz. Du konstruierst nichts, du machst nur Dinge mit den Parametern. Dies ist nur eine Methode.

Waffle2hat haben einen Konstruktor, syntaktisch, aber auch hier ist es nicht, Dinge zu tun, die den Aufbau eines Waffle2 Instanz verwandt sind: Sie die Elementvariablen zugreifst, aber es ist nicht klar, warum, und nicht nur Mitglied Variablen . Am Ende eines Konstruktors sollten die Invarianten der Waffle2 Klasse festgelegt werden, wie in den Regeln, die diese Waffle2 "gültig" machen, sollte wahr sein. Gibt es solche Regeln? Wenn nicht, brauchen Sie keinen Konstruktor.

Verwandte Themen