我觉得我越来越接近使用调试器,但我仍然无法解决这个问题。
我正在浏览以下代码
namespace Taxes
{
public class Rates
{
//A class constructor that assigns default values
public Rates()
{
incLimit = 30000;
lowTaxRate = .15;
highTaxRate = .28;
}
//A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
public Rates(int lim, double low, double high)
{
incLimit = lim;
lowTaxRate = low;
highTaxRate = high;
}
// A CalculateTax method that takes an income parameter and computes the tax as follows:
public int CalculateTax(int income)
{
//determine if the income is above or below the limit and calculate the tax owed based on the correct rate
int taxOwed;
if (income < incLimit)
taxOwed = Convert.ToInt32(income * lowTaxRate);
else
taxOwed = Convert.ToInt32(income * highTaxRate);
return taxOwed;
}
}
// The Taxpayer class is a comparable class
public class Taxpayer : IComparable
{
//Use get and set accessors.
private int taxOwed;
string SSN
{ set; get; }
int grossIncome
{ set; get; }
int TaxOwed {
get
{
return taxOwed;
}
}
int IComparable.CompareTo(Object o)
{
int returnVal;
Taxpayer temp = (Taxpayer)o;
if (this.taxOwed > temp.taxOwed)
returnVal = 1;
else if (this.taxOwed < temp.taxOwed)
returnVal = -1;
else returnVal = 0;
return returnVal;
}
public static Rates GetRates()
{
// Local method data members for income limit, low rate and high rate.
int incLimit;
double lowRate;
double highRate;
string userInput;
//Rates myRates = new Rates(incLimit, lowRate, highRate);
//Rates rates = new Rates();
// Prompt the user to enter a selection for either default settings or user input of settings.
Console.Write("Would you like the default values (D) or would you like to enter the values (E)?: ");
// if they want the default values or enter their own
userInput = (Console.ReadLine());
if (userInput == "D" || userInput == "d")
{
Rates myRates = new Rates();
return myRates;
//Rates.Rates();
//rates.CalculateTax(incLimit);
}
else if (userInput == "E" || userInput == "e")
{
Console.Write("Please enter the income limit: ");
incLimit = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the low rate: ");
lowRate = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter the high rate: ");
highRate = Convert.ToDouble(Console.ReadLine());
Rates myRates = new Rates(incLimit, lowRate, highRate);
return myRates;
//rates.CalculateTax(incLimit);
}
else return null;
}
static void Main(string[] args)
{
Taxpayer[] taxArray = new Taxpayer[5];
//Rates taxRates = new Rates();
// Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
for (int x = 0; x < taxArray.Length; ++x)
{
taxArray[x] = new Taxpayer();
Console.Write("Please enter the Social Security Number for taxpayer {0}: ", x + 1);
taxArray[x].SSN = Console.ReadLine();
Console.Write("Please enter the gross income for taxpayer {0}: ", x + 1);
taxArray[x].grossIncome = Convert.ToInt32(Console.ReadLine());
//taxArray[x].taxOwed = taxRates.CalculateTax(taxArray[x].grossIncome);
}
Rates myRate = Taxpayer.GetRates();
//Taxpayer.GetRates();
// Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
for (int i = 0; i < taxArray.Length; ++i)
{
Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, myRate.CalculateTax(taxArray[i].grossIncome));//taxArray[i].taxOwed);
}
// Implement a for-loop that will sort the five objects in order by the amount of tax owed
Array.Sort(taxArray);
Console.WriteLine("Sorted by tax owed");
for (int i = 0; i < taxArray.Length; ++i)
{
//double taxes = myTax.CalculateTax(taxArray[i].grossIncome);
Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, myRate.CalculateTax(taxArray[i].grossIncome));
}
}
}
}
我已经解决了所有问题,但现在由于某种原因,排序没有按照欠税额进行排序。
答案 0 :(得分:2)
关于Taxpayer.GetRates()
的旁白:纳税人类不应负责确定税率。如果纳税人可以确定税率,税率很可能为零。将其移入Rates
类可能更有意义。
这个问题的答案显示了一个例子,可以帮助您了解出错的地方。如果您想要与您的代码相关的特定建议,请发布一个完整的编译程序。您发布的示例代码无法编译(错误:“当前上下文中不存在名称'taxRates'”。)
回答你的问题:
如何实例化一个类,以便我可以在不调用默认构造函数的情况下使用它的方法?
正如其他人所说,你需要保留对新实例化对象的引用,这样你就可以在从字段中读取值时使用该实例。
考虑:
void SetRates()
{
Rates rates = new Rates(10000, 0.3, 0.4);
}
void UseRates(Taxpayer taxpayer)
{
taxpayer.Tax = new Rates().CalculateTax(taxpayer.Income);
}
void Main()
{
SetRates(); // creates an object and throws it away
Taxpayer taxpayer = new Taxpayer(20000);
UseRates(taxpayer); // creates a new object with default values
Console.WriteLine(taxpayer.Tax);
}
相反,返回您在SetRates()中创建的对象(并将其称为GetRates())。然后将其传递给UseRates方法:
Rates GetRates()
{
return new Rates(10000, 0.3, 0.4);
}
void UseRates(Taxpayer taxpayer, Rates rates)
{
taxpayer.Tax = rates.CalculateTax(taxpayer.Income);
}
void Main()
{
Rates rates = GetRates();
Taxpayer taxpayer = new Taxpayer(20000);
UseRates(taxpayer, rates);
Console.WriteLine(taxpayer.Tax);
}
关于您编辑的代码,您有
Rates myTax = new Rates();
Taxpayer.GetRates();
现在Taxpayer.GetRates()
会为一个Rates实例分配一些值,但它与使用语句Rates myTax = new Rates()
创建的Rate实例不同。语句Rates myTax = new Rates()
< em>调用默认的Rates构造函数,这是您稍后在计算税的方法中使用的实例!这解释了为什么您的税总是使用默认值计算。
GetRates方法在不同的Rates类实例上运行。这个不同的实例是由GetRates方法体中的new Rates(...
个表达式之一创建的。该实例具有您想要使用的速率,但它基本上被困在方法中。它为什么被困?因为您只将实例分配给局部变量,并且在声明它们的方法之外无法访问局部变量。
有点像这样:我们Rates
代替Car
,而不是GetRates
我们有FillFuelTank
。您的代码正在执行以下操作:
Get a new car //Rates myTax = new Rates();
Go to the gas station //Taxpayer.GetRates();
现在,GetRates()方法......我的意思是,FillFuelTank方法......这样做:
Get a new car with fuel in it //Rates myRates = new Rates(incLimit, lowRate, highRate)
你看到你做了什么?你已经开车去了新车的加油站,买了一辆装有燃料的第二辆车,然后你回到了第一辆车然后开走了 - 没有任何燃料。
其中一个解决方案是将myTax
作为参数传递给GetRates()
方法;更好的解决方案是从GetRates()
方法返回一个Rates实例。
您写道:
我需要率myTax = new Rates();在main中,我可以调用calculateTax方法来做到这一点。如果有另一种方法在不调用默认构造函数的情况下调用该方法,我无论是耳朵还是手指。
表达式new Rates()
调用默认构造函数。因此,在不调用默认构造函数的情况下调用calculateTax
的方法是调用参数化构造函数:
Rates rates = new Rates(limit, lowRate, highRate);
double tax = rates.CalculateTax(taxpayer.Income);
你可能会说,“但我确实在GetRates方法中调用了参数化构造函数!”而且,再次出现了用燃料填充错误汽车的问题,因为GetRates方法中的Rates对象是一个不同的对象。您在不使用的对象上调用参数化构造函数,并在您使用的对象上调用默认构造函数。
答案 1 :(得分:0)
当到达花括号时,myRates对象超出范围并且不再存在。你的价值观来自其他地方。
Rates myRates = new Rates(incLimit, lowRate, highRate);
//rates.CalculateTax(incLimit);
}
答案 2 :(得分:0)
我认为你的问题就在这一行:
Taxpayer.GetRates();
这是一个静态方法,这意味着它不会设置任何类成员变量。我希望GetRates()返回一个Rates对象。