简体   繁体   English

C#程序中的IComparable命名空间问题

[英]IComparable Namespace Issue in C# Program

I'm working on a program that uses IComparable, and I'm following the code in the book, but it's not clear on where to put it exactly. 我正在开发一个使用IComparable的程序,并且正在按照书中的代码进行操作,但是尚不清楚将其确切放置在何处。 I placed it in my Driver program, but in the original program, the main class is underlined, saying 我将其放在驱动程序中,但是在原始程序中,主类带有下划线,

"Error  1   The namespace 'Exercise6B' already contains a definition for 'TaxPayer2'

I tried changing things around and renaming my classes in each file, but then the TaxOwed field turns red. 我尝试更改每个文件中的类并重命名我的类,但是TaxOwed字段变成红色。 Any guidance would be helpful. 任何指导都会有所帮助。 I'm very new to C# and don't quite understand it. 我是C#的新手,对此不太了解。

Here's my code: 这是我的代码:

class TaxPayer2
{
    //Instance Fields
    private string ssn;
    private double grossIncome;
    private double taxOwed;

    private double incomeLimit;
    private double lowTaxRate;
    private double highTaxRate;

    public string Ssn
    {
        get { return ssn; }
        set { ssn = value; }
    }

    public double GrossIncome
    {
        get { return grossIncome; }
        set { grossIncome = value; }
    }

    public double TaxOwed
    {
        get
        {
            return taxOwed;
        }
    }

    public TaxPayer2()
    {
    }

    public TaxPayer2(string social, double income)
    {
        ssn = social;
        grossIncome = income;

        if (income < 30000)
        {
            taxOwed = .15 * income;
        }
        else
        {
            taxOwed = .28 * income;
        }

    }


    public TaxPayer2(double limit, double lowRate, double highRate)
    {
        incomeLimit = limit;
        lowTaxRate = lowRate;
        highTaxRate = highRate;
    }

} //end of TaxPayer main class

DRIVER 驱动器

class TaxPayer2 : IComparable
{
        public string SSN { get; set; }
        public double Income { get; set; }
        //public double TaxOwed { get; }

    int IComparable.CompareTo(Object o)
    {
        int returnVal;
        TaxPayer2 temp = (TaxPayer2)o;
        if (this.taxOwed > temp.taxOwed)
            returnVal = 1;
        else if (this.taxOwed < temp.taxOwed)
                returnVal = -1;
        else returnVal = 0;
        return returnVal;
    }
}


class Driver
{
    static void Main()
    {

        TaxPayer2[] arrayTaxPayers = new TaxPayer2[10];

        for (int x = 0; x < 3; ++x)
        {
            Console.WriteLine("Enter SSN: ");
            string ssn = Console.ReadLine();

            Console.WriteLine("Enter Income: ");
            double income = Convert.ToDouble(Console.ReadLine());

            arrayTaxPayers[x] = new TaxPayer2(ssn, income);
        }

        for (int x = 0; x < 3; ++x)
        {
            Console.WriteLine("SSN {0} earned income of {1} and owes {2} in taxes.",
                arrayTaxPayers[x].Ssn, arrayTaxPayers[x].GrossIncome.ToString("C"), 
                arrayTaxPayers[x].TaxOwed.ToString("C"));

        }

        Array.Sort(arrayTaxPayers);
        Console.WriteLine("Sorted By Tax Owed:");
        for(int x=0; x<3; ++x)
        {
            Console.WriteLine("SSN {0} earned income of {1} and owes {2} in taxes.",
                arrayTaxPayers[x].Ssn, arrayTaxPayers[x].GrossIncome.ToString("C"),
                arrayTaxPayers[x].TaxOwed.ToString("C"));
        }

    }//end of Main
}//end of Driver

除非并且直到它们是局部类,否则在同一名称空间中不能有两个或多个相同类的定义(在您的情况下为TaxPayer2)。

Basic Skeleton should be something like this: 基本骨架应该是这样的:

class TaxPayer2 : IComparable
{
  //properties 

  //methods

  //IComparable implementation
}

class Driver
{
    static void Main()
    {
      //the code you have in Main() here
    }
}

What the book prabably is showing is first the class declaration(without the IComparable implementation) and then they show you how to implement it in the same class,thats why you think the book is showing 2 different classes when there are only 1 and the same.To state the obvious you cant have 2 equal class declarations in the same namespace. 本书大概会显示的是类声明(没有IComparable实现),然后向您展示如何在同一类中实现它,这就是为什么您认为这本书在只有1个且相同的情况下显示了2个不同的类要说清楚,您不能在同一名称空间中有2个相等的类声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM