不起作用的其他方法

本文关键字:方法 其他 不起作用 | 更新日期: 2023-09-27 18:27:48

我意识到我可以将数据放在构造函数正在调用的方法中,并且它可以工作。然而,我只是打乱了一些代码,我正在尝试使用其他方法进行计算。这是一本书中的问题,由于我有几个星期没有导师可以窃听,我想我会在这里问。

有什么意见吗?请并感谢

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace ConsoleApplication305
{
class Program
{
    static void Main(string[] args)
    {
        Trip a = new Trip();
        Console.WriteLine(a);
    }
    class Trip
    {
        int distanceTraveled;
        double costGas;
        int numberGallon;
        int mpg;
        double cpm;
        public int DistanceTraveled
        {
            get
            {
                return distanceTraveled;
            }
        }
        public double CostGas
        {
            get
            {
                return costGas;
            }
        }
        public int NumberGallon
        {
            get
            {
                return numberGallon;
            }
        }
        public Trip()
        {
            distanceTraveled = 312;
            costGas = 3.46;
            numberGallon = 10;
        }
        public void milesPerGal()
        {
            mpg = distanceTraveled / numberGallon;
        }
        public void costPerMile()
        {
            cpm = costGas * mpg;
        }
        public override string ToString()
        {
            return String.Format("The distance traveled is...{0} 'nThe cost per gallon of gasoline is... {1} 'nThe amount of gallons were... {2} 'nThe miles per gallon attained were... {3} 'nThe cost per MPG were... {4}", distanceTraveled, costGas, numberGallon, mpg, cpm);
        }
    }
}

}

不起作用的其他方法

如果我正确理解你的问题,这应该对你有效:

class Trip
{
    int distanceTraveled;
    double costGas;
    int numberGallon;
    int mpg;
    double cpm;
    public int DistanceTraveled
    {
        get
        {
            return distanceTraveled;
        }
    }
    public double CostGas
    {
        get
        {
            return costGas;
        }
    }
    public int NumberGallon
    {
        get
        {
            return numberGallon;
        }
    }
    public Trip()
    {
        distanceTraveled = 312;
        costGas = 3.46;
        numberGallon = 10;
        mpg = milesPerGal();
        cpm = costPerMile();
    }
    public int milesPerGal()
    {
        return distanceTraveled / numberGallon;
    }
    public double costPerMile()
    {
        return costGas * mpg;
    }
    public override string ToString()
    {
        return String.Format("The distance traveled is...{0} 'nThe cost per gallon of gasoline is... {1} 'nThe amount of gallons were... {2} 'nThe miles per gallon attained were... {3} 'nThe cost per MPG were... {4}", distanceTraveled, costGas, numberGallon, mpg, cpm);
    }
}

老实说,这里一切都很好。由于您从未调用方法milesPerGal或costPerMile,所以私有变量mpg和cpm的值仍然为0,也将输出。

你可能想要的是这个构造函数:

public Trip()
{
    distanceTraveled = 312;
    costGas = 3.46;
    numberGallon = 10;
    milesPerGal();
    costPerMile();
}

为了使此代码如您所期望的那样工作,我建议您使用适当的getter和setter,如下所示:

class Trip
{
    public int DistanceTraveled { get; set; }
    public double CostGas { get; set; }
    public int NumberGallon { get; set; }
    public int MilesPerGallon { get { return (NumberGallon != 0) ? DistanceTraveled / NumberGallon : 0; } }
    public double CostPerMile { get { return CostGas * MilesPerGallon; } }
    public Trip()
    {
        DistanceTraveled = 312;
        CostGas = 3.46;
        NumberGallon = 10;
    }
    public override string ToString()
    {
        return String.Format("The distance traveled is...{0} 'nThe cost per gallon of gasoline is... {1} 'nThe amount of gallons were... {2} 'nThe miles per gallon attained were... {3} 'nThe cost per MPG were... {4}", DistanceTraveled, CostGas, NumberGallon, MilesPerGallon, CostPerMile);
    }
}

请注意,使用这种方法,无论何时更新具有setter的任何字段(DistanceTraveledCostGasNumberGallons),相应的派生值(MilesPerGallonCostPerMile)都将自动更正,而无需任何额外的方法调用,因为当有人访问这些字段时,它们是动态计算的。