【问题标题】:How to use OOP, constructors, and objects to pass variables to methods and main method如何使用 OOP、构造函数和对象将变量传递给方法和主方法
【发布时间】:2019-06-25 05:32:18
【问题描述】:

我有一个程序,我在其中计算并打印出油耗信息。我正在处理的任务希望我(我认为它希望我)使用 OOP、构造函数和对象将变量从构造函数传递到我的整个程序。这对我来说毫无意义。

以下是确切的分配说明: https://drive.google.com/file/d/1D1FZKHrU4duu1UyWusPvsjVum33B8kdt/view?usp=sharing

我尝试将我的原始变量放在 main 方法中的私有静态变量中,并将它们放在我的类头的正下方。这行得通,但我认为说明希望我的构造函数以某种方式包含变量。这对我来说根本没有任何意义。我没有得到任何构造函数或对象以及如何在这个应用程序中使用它们,这让我很沮丧。

CarV3,指令引用的程序,我应该面向对象,看起来像这样:

public class CarV3 {
    public static int calcDistance(int sMiles1, int eMiles1){
        return eMiles1 - sMiles1;
    }

    public static double calcMPG(int dist1, double gals1){
        return dist1 / gals1;
    }

    public static void main(String[]Args) {
        int sMiles1 = 43305;
        int eMiles1 = 43422;
        int dist1;
        double gals1 = 7.8;
        double MPG1;
        String carType1 = "14 Acura MDX";

        CarV3 car1 = new CarV3();

        dist1 = car1.calcDistance(sMiles1, eMiles1);
        MPG1 = car1.calcMPG(dist1, gals1);

        System.out.printf("%52s%n%s%16s%14s%13s%12s%14s%n%s%n", "Gas Mileage Calculations", "Type of Car", "Start Miles", "End Miles", "Distance", "Gallons", "Miles/Gal",
"================================================================================");
        System.out.printf("%s%15d%14d%13d%12.1f%14.1f", carType1, sMiles1, 
        eMiles1, dist1, gals1, MPG1);
    }
}

该代码以前有效。 指令要求稍后计算 GPM 和其他数据,所以我的程序在 CarV5 中变成了更像这样的东西:

public class CarV5 {
    private static int sMiles1 = 43266;
    private static int eMiles1 = 43422;
    private static int dist1;
    private static double gals1 = 7.8;
    private static double MPG1;
    private static double pricePerGallon1 = 2.98;
    private static double GPM1;
    private static double totalCost1;
    private static String carType1 = "14 Acura MDX";

    CarV5() {
    }

    public int calcDistance(int sMiles1, int eMiles1){
        return eMiles1 - sMiles1;
    }

    public static double calcMPG(int dist1, double gals1){
        return dist1 / gals1;
    }

    public static double calcGPM(int dist1, double gals1) {
        return gals1 / dist1;
    }

    public static double totalCost(double pricePerGallon1, double gals) {
        return pricePerGallon1 * gals;
    }

    public static void main(String[]Args) {
        CarV5 car1 = new CarV5();

        dist1 = car1.calcDistance(sMiles1, eMiles1);
        MPG1 = car1.calcMPG(dist1, gals1);
        GPM1 = car1.calcGPM(dist1, gals1);
        totalCost1 = car1.totalCost(pricePerGallon1, gals1);

        System.out.printf("%70s%n%s%16s%14s%13s%12s%10s%9s%14s%13s%n%s%n", "Gas Mileage Calculations", "Type of Car", "Start Miles", "End Miles", "Distance", "Gallons", "Price", "Cost", "Miles/Gal", "Gal/Mile", "================================================================================================================");
        System.out.printf("%s%15d%14d%13d%12.1f%10.2f%9.2f%14.1f%13.3f", carType1, sMiles1, eMiles1, dist1, gals1, pricePerGallon1, totalCost1, MPG1, GPM1);
    }
}

虽然这段代码有效,但我几乎可以肯定这不是说明要求我做的。老实说,我不明白指示要我做什么。您在那里看到的程序确实打印了预期的结果,即所有汽车数据。但是,在说明中,我被告知要使用某种带有值的构造函数。如果您在第二个版本中注意到我有这个“CarV5() {}”,我不明白它的作用。

如前所述,在我的第二个版本的程序 CarV5 中预期的输出是正确的。一张表格打印出距离、加仑等的计算值。

我的方法有什么问题。 很抱歉这篇超长的帖子,但我也参加这门课程的朋友认为我已经过了被教导的地步,如果我说实话,也许我也是。我真的很想弄清楚如何做到这一点。

【问题讨论】:

标签: java oop object constructor


【解决方案1】:

对象是与任何其他数据类型一样的数据类型(例如 int、double、String 等)。这意味着您可以创建多个具有不同名称和与之关联的属性的对象。字段变量 (最常见的形式是
private int field_name; ) 只是此对象值的一部分。你必须从字面上看:你直接将每加仑英里的属性与你读到的任何汽车相关联,所以它的工作方式相同。静态变量是一种简单的方法,通过让不属于类的对象访问您不希望特定于汽车的变量(当然只属于特定类),从而使值扩展类的小范围(例如,一个年份变量,对于所有对象都是相同的(不相关,但一个似是而非的例子))。希望这可以解决问题!

【讨论】:

    【解决方案2】:

    好的,我不确定分配是什么,因为它不可访问,但我认为他们希望您创建一个类,其中包含将在对象创建期间在构造函数中设置的字段。让我举一个简单的例子:

    package tst;
    
    public class CarTest {
    
        public static void main(String[] args) {
            // creating new instance of the Car and at once setting all its fields in constructor
            // now as I see the assignment, this line corresponds to 
            // CarV5 car1 = new CarV5(carType1, endMiles1, startMiles1, gallonsUsed1, pricePerGallon1);
            Car car = new Car(43266, 43422, 7.8);
    
            //calculating and printing the result w/o providing values but using instance fields
            System.out.println(car.calcDistance());
    
        }
    
    
    
    
        // the Car class itself, you can put it in different file
        public static class Car {
    
            // note that fields are NOT static, i.e. they belong to an INSTANCE of the class, not the class itself
            private int sMiles1;
            private int eMiles1;
            private double gals1;
    
            //
            public Car(int sMiles1, int eMiles1, double gals1) {
                this.sMiles1 = sMiles1;
                this.eMiles1 = eMiles1;
                this.gals1 = gals1;
            }
    
            // example of your methods
            public int calcDistance(){
                return this.eMiles1 - this.sMiles1;
            }
    
            // other methods you need
    
        }
    
    }
    

    如果有帮助,请告诉我:) PS:我认为让您感到困惑的是 static 与 instance 方法,请尝试刷新这部分课程或使用其他来源(=google :))

    【讨论】:

    • 感谢您的回复。我们实际上不应该使用多个类。我也不明白“这个”。因为我们还没有学会。我更新了说明链接,使其应该可以访问。我认为您的方法非常接近我必须做的。谢谢!
    • 是的,我故意将它分成两个类,以便您可以更好地查看类本身(对象)和使用该类的代码。您可以将main 方法移动到类 Car,它的工作方式相同。关于this - 你可以威胁到作为类实例的指针,就像这里this.sMiles1 = sMiles1; 我们将构造函数参数 sMiles1 设置为private int sMiles1; 中声明的实例字段。
    【解决方案3】:

    首先为什么一切都是静态的? 其次,由于您的链接不起作用,我真的看不出被要求做什么,所以我只能假设,根据您所说的,您想用构造函数创建一个对象,是不是?

    public class CarV5 {
    
    private int sMiles1;
    private int eMiles1;
    
    public CarV5(int sMiles1Constructor,int eMiles1Constructor){
       this.sMiles1 = sMiles1Constructor;
       this.eMiles1 = eMiles1Constructor;
    }
    
     your methods...
    
    }
    
    public static void main(String[]Args) {
        CarV5 car1 = new CarV5(43266,42422);
    
         //Call your methods here...
    }
    

    基本上,您只需将变量作为参数传递给构造函数。

    不要放弃,一旦你学会了这些基础知识,随着时间的推移它会变得更容易,别担心。

    【讨论】:

    • 我认为这是一个很好的建议。我的朋友试图建议我在对象的声明中输入数字。我想我明白你在做什么,但我不确定“这个”是什么。做。我们还没有在课程中学到这一点。但是,查看您的代码,我将其解释为我需要通过构造函数将值提供给实际整数?我也不明白sMiles1Constructor。那只是我要在那里写的东西的占位符吗?非常感谢您的回复。另外,您确定更新后的 Google Drive 链接不起作用吗?我把它设为“每个人都可以查看”。再次感谢
    • sMiles1Constructor 只是我写的,这样你就会明白这是在构造函数中传递的变量(通常它们具有相同的名称),即“this”。正在引用在构造函数上方创建的变量,以便您稍后可以使用 getter 和 setter 访问这些值(如果需要,请查看),基本上我在上面创建的变量将具有在构造函数中传递的值(“ this." 意思是“这个类”) 。是的,谷歌驱动器链接已经工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2011-06-30
    • 2014-09-10
    相关资源
    最近更新 更多