2014-02-08 48 views
0

我已閱讀了關於超類,抽象等。我理解了大部分的一般想法。 我現在有3類(主,超類,子類)從java中的超類返回數據

第一類:抓取用戶類型的汽車的用戶里程和加侖,發送數據到子類。子類將數據設置爲變量。

我想找出一種方法來打印每加侖汽車的里程數。這種計算方法在超類內,但我找不到打印方法的方法。 這裏是我的代碼:

主要


import java.util.Scanner; 
public class StartingPoint { 

    public static void main (String[]args) 
    { 
     Scanner input = new Scanner(System.in); //Get user input 

     Mercedes mCar = new Mercedes(); //Accesses subclass of the Car superclass (Mercedes) 
     Toyota tCar = new Toyota(); //Accesses subclass of the Car superclass (Toyota) 

     System.out.println("Do you own a mercedes or a toyota?"); //Get car type 

     String carName = input.nextLine(); 

     if (carName.charAt(0) == 'm' || carName.charAt(0) == 'M') 
     { 
      System.out.println("How many miles have you travelled?"); 
      double miles = input.nextDouble(); 

      System.out.println("How many gallons have you used?"); 
      double gallons = input.nextDouble(); 

      mCar.setGallons(gallons); 
      mCar.setMiles(miles); 

     } 

     else if (carName.charAt(0) == 't' || carName.charAt(0) == 'T') 
     { 
      //NOT CODED YET 
     } 

     else 
     { 
      System.out.println("Not a valid car name for the system..."); 
     } 





    } 

} 

超級類


public abstract class Car { 

    public double calcMPG(double miles, double gallons) 
    { 
     return (miles/gallons); 
    } 
} 

超類(汽車)的子類


public class Mercedes extends Car { 

    private double miles; 
    private double gallons; 

    public Mercedes() 
    { 
     miles = 0; 
     gallons = 0; 
    } 

    public void setMiles(double inputMiles) 
    { 
     miles = inputMiles; 
    } 

    public void setGallons(double inputGallons) 
    { 
     gallons = inputGallons; 
    } 







} 
+0

我將此行添加到我的主類,不知道是否是最好的方式去做這件事。的System.out.println(Mercedes.calcMPG(英里,加侖)); – KnowledgeGeek

+0

我也想到我可以添加一個方法(計算)到梅賽德斯班,並呼籲super.calcMPG(英里,加侖),並有該方法打印.. System.out.println(super.calcMPG(英里,加侖)和然後調用mCar.calc();它會打印 – KnowledgeGeek

回答

0

好了,我這是怎麼解決這個..

在我的奔馳類我創建了一個名爲計算方法。在我調用的方法裏面: System.out.print(super.calcMPG(miles,gallons));

回到我的主要方法,我叫mCar.calc();

這樣它成功地計算出所有的數據並打印出來。

+0

我不認爲繼承的使用是一個好主意,Car超類的唯一目的是使方法calcMPG可用。有更多的使用證明是正當的,但我看到你剛開始使用Java,所以你的實驗運氣好運! – tilois

+0

好的,謝謝澄清。我對這種理解很陌生,但我更多的是在試驗一切事物之間的相互關係。我希望今天能夠提高我對這個想法的理解。 – KnowledgeGeek

0

您可以通過在超類中重寫toString方法來完成此操作,然後僅使用System.out.print打印該對象。爲了使這個工作,你必須重建你的超類和子類。這是我做到的方式,它的工作原理,但我不確定這是你想要的。希望它以任何方式幫助。

主要方法:

package CarProject; 

import java.util.Scanner; 
public class StartingPoint { 

    public static void main (String[]args) 
    { 
     Scanner input = new Scanner(System.in); //Get user input 

     Mercedes mCar = new Mercedes(); //Accesses subclass of the Car superclass (Mercedes) 
     Toyota tCar = new Toyota(); //Accesses subclass of the Car superclass (Toyota) 

     System.out.println("Do you own a mercedes or a toyota?"); //Get car type 

     String carName = input.nextLine(); 

     if (carName.charAt(0) == 'm' || carName.charAt(0) == 'M') 
     { 
      System.out.println("How many miles have you travelled?"); 
      double miles = input.nextDouble(); 

      System.out.println("How many gallons have you used?"); 
      double gallons = input.nextDouble(); 

      mCar.setGallons(gallons); 
      mCar.setMiles(miles); 

     } 

     else if (carName.charAt(0) == 't' || carName.charAt(0) == 'T') 
     { 
      System.out.println("How many miles have you travelled?"); 
      double miles = input.nextDouble(); 

      System.out.println("How many gallons have you used?"); 
      double gallons = input.nextDouble(); 

      mCar.setGallons(gallons); 
      mCar.setMiles(miles); 
     } 

     else 
     { 
      System.out.println("Not a valid car name for the system..."); 
     } 

     if (carName.charAt(0) == 't' || carName.charAt(0) == 'T') { 

      System.out.println("Miles per gallon for your Toyota is " +tCar); 

     } else if (carName.charAt(0) == 'm' || carName.charAt(0) == 'M') { 

      System.out.println("Miles per gallon for your Mercedes is " + mCar); 

     } 
    } 
} 

超類

package CarProject; 

public class Car { 

    double miles; 
    double gallons; 

    @Override 
    public String toString() { 
     return String.valueOf(miles/gallons); 
    } 

} 

子類梅賽德斯

package CarProject; 

public class Mercedes extends Car { 

    public Mercedes() 
    { 
     miles = 0; 
     gallons = 0; 
    } 

    public void setMiles(double inputMiles) 
    { 
     miles = inputMiles; 
    } 

    public void setGallons(double inputGallons) 
    { 
     gallons = inputGallons; 
    } 
} 

子類豐田

package CarProject; 

public class Toyota extends Car { 

    public Toyota() 
    { 
     miles = 0; 
     gallons = 0; 
    } 

    public void setMiles(double inputMiles) 
    { 
     miles = inputMiles; 
    } 

    public void setGallons(double inputGallons) 
    { 
     gallons = inputGallons; 
    } 
} 
+0

這是行不通的,因爲超級車Car不知道'miles'和'gallons'字段。這些只是傳遞給它的唯一'方法'。 – tilois

+0

我已經改變了超級'汽車'現在有'英里'和'加侖'實施。每種類型的汽車都會繼承這些領域,並且可以針對每輛汽車設定不同的設置。它應該以這種方式工作,我也是Java新手,只是想幫助。 – msmolcic

0
import java.io.*; 

class parent { 
    int a; //VARIABLE DECLARATIONS IS ALLOWED ONLY HERE 

    public void get1() throws Exception { 
     DataInputStream in = new DataInputStream(System.in); 
     System.out.print("\n ENTER THE INTEGER:"); 
     a = Integer.parseInt(in.readLine()); 
    } 
} 

class child extends parent { 
    int b; //VARIABLE DECLARATIONS IS ALLOWED ONLY HERE 

    public void get2() throws Exception { 
     DataInputStream in = new DataInputStream(System.in); 
     System.out.print("\n ENTER THE INTEGER:"); 
     b = Integer.parseInt(in.readLine()); 
    } 
} 

public class sample { 
    public static void main(String args[]) throws Exception { 
     child obj = new child(); 
     obj.get1(); 
     obj.get2(); 
     System.out.println("\n SUM="+(obj.a+obj.b)); 
    } 
} 
+0

歡迎來到Stackoverflow,謝謝你給OP的問題提供一個答案。如果您爲答案增加更多解釋,這將使OP對未來的OP和其他讀者更有用。請參閱https://stackoverflow.com/help/how-to-answer關於如何寫出一個好答案。 –

+0

爲你的答案增加更多解釋 – Mostafiz