C# 如何为不同类中的字段赋值?

C# 如何为不同类中的字段赋值?,c#,arrays,class,object,methods,C#,Arrays,Class,Object,Methods,在这个程序中,我有两个类应用程序和客户。我想用用户应该输入的值初始化标准票价。“标准票价”字段位于客户类别中。 我这样做了,但没有显示出预期的结果。当调用计算函数时,标准票价的值变为零 当我在Customer类本身中初始化STANDARD_FARE的值时,程序就会按预期工作。 如何将用户给出的值输入标准票价 应用程序类中的GetAge、GetPassNo等方法也不会返回相同的值 class Application { private static int Nop ; pri

在这个程序中,我有两个类应用程序和客户。我想用用户应该输入的值初始化标准票价。“标准票价”字段位于客户类别中。 我这样做了,但没有显示出预期的结果。当调用计算函数时,标准票价的值变为零

当我在Customer类本身中初始化STANDARD_FARE的值时,程序就会按预期工作。 如何将用户给出的值输入标准票价

应用程序类中的GetAge、GetPassNo等方法也不会返回相同的值

 class Application
  {
    private static int Nop ;
    private static double TotalFare=0;
    Customer cust= new Customer(); 

    static void Main(string[] args)
      {
        Application obj = new Application();                      
        Console.Write("Enter the STANDARD RATE of the tour ");
        obj.cust.StandardFare = int.Parse(Console.ReadLine());

        a:
        Console.Clear();                                             
        Console.WriteLine("Enter the number of passengers");
        Nop = int.Parse(Console.ReadLine());                       

        Application[] app = new Application[Nop];                 

        if (Nop <= 0)                                               
        {
            Console.WriteLine("Please enter a valid number of passengers");
            Console.ReadKey();                                     
            goto a;
        }

        for (int i = 0; i < Nop; i++)
        {
            app[i] = new Application();                           
            app[i].GetInformationFromCust();         
        }

        for (int j = 0; j < Nop; j++)
        {

           app[j].cust.Display();                                  

       }                                             
     }

    public int GetInformationFromCust()
    {      
         b:
        Console.Clear();                                     
        int slen = 0;                                   

        Console.WriteLine("Enter the title of the passenger");
        cust.Customer_Title = Console.ReadLine();
        Console.WriteLine("\r\nEnter passenger's First name :");
        cust.Customer_FName = Console.ReadLine();
        Console.WriteLine("\r\nEnter passenger's Last name :");
        cust.Customer_LName = Console.ReadLine();
        slen = cust.Customer_FName.Length + cust.Customer_LName.Length;    
         if (slen < 5 || slen > 15)                                       
          {
            Console.WriteLine("\r\nName should be between 5 to 15 characters, Please try again ");
            Console.ReadLine();
            goto b;
          }

        c:
        long x = 0, len = 0;
        Console.WriteLine("\r\nEnter the passport number of the passenger ");
        cust.CustomerPassNo = int.Parse(Console.ReadLine());
        x = cust.CustomerPassNo;               

         while (x > 0)
          {
            x = x / 10;                    
            ++len;
          }
        if (len != 8)                    
          {
            Console.WriteLine("\r\nInvalid passport number, passport should be of 8 digits ");
            goto c;
          }

        d:
        Console.WriteLine("\r\nEnter the age of the passenger :");
        cust.Customer_Age = int.Parse(Console.ReadLine());
          if (cust.Customer_Age < 0)                                         
          {
             Console.WriteLine("\r\nInvalid age, please enter a valid age ");
             goto d;
          } 
        cust.CalculatePrice();                                          
        return 0;
    }

    public int GetAge()
    {
        return cust.Customer_Age;
    }

    public double GetAirFare()
    {
        return cust.CustomerTicket ;
    }

    public long GetPassportNo()
    {
        return cust.CustomerPassNo;
    }

    public string GetTitle()
    {
        return cust.Customer_Title;
    }
 }

您正在使用应用程序的实例填充阵列应用程序,这些应用程序的默认标准票价值为0.0,因为您从未在这些实例上设置该值。您只在obj.cust实例上设置它,以后不再使用它。因为STANDARD_FARE是一个实例变量,所以对它的更改不会影响其他实例或将来的实例

相反,所有应用程序都有相同的问题;他们正在获取对象obj.cust的属性,该对象除了StandardFare/STANDARD_FARE之外,从未设置过任何属性

最明显的修复方法是完全取消obj和obj.cust——它们除了混淆之外没有任何用处——并使STANDARD_FARE成为一个静态变量,使其setter StandardFare成为一个静态属性


顺便说一句,你的命名习惯是可怕的和不一致的;如果我是你的评分员,我会因为你使用不明确的变量名称app,nop,以及使用非常量标准票价的所有上限而扣分。我还反对使用私有的自动支持属性CustomerName,它也从来没有被用来代替私有变量,因为它没有在其他地方使用自动支持属性StandardFare作为标准票价的显式编码公共getter和setter,等等。,以及用于将常量值复制到不可设置的实例变量K_折扣到Kid折扣;只需直接使用该常量,或者至少将其设置为静态,并添加一些非私有访问。正如其他人提到的,您当然不应该使用goto来代替循环。我还将提到,通过重复除法检查护照号码的长度容易出错且效率低下,而不是简单地检查它是否小于9999999。理论上,护照号码可能以零开头,解析后看起来小于8位,但如果您愿意,也可以确保它大于10000000。

我强烈建议您不要使用goto。只需使用do while循环即可。您已经在此处设置了标准票价:obj.cust.StandardFare=int.ParseConsole.ReadLine;。您只需确保随后还使用了与您从用户输入中设置值相同的应用程序和客户对象。如果您查看其他客户对象,那么您当然不会找到用户输入的值…为什么要在GetInformationFromCust中创建新的应用程序对象?我使用对象数组来获取多个乘客的数据。如何使用该对象初始化标准票价。谢谢,我让标准票价和它的属性保持不变,现在一切都好了。正如您所提到的,我还删除了类应用程序的obj对象。因为这不是我的最终代码,所以命名有一些问题,我没有纠正。1标准票价都是上限,因为我之前已经定义了它的值。但是现在我想从用户那里获取标准票价的值,所以我现在没有更改它,但我会在以后更改,2 app=object for class Application,Nop=No.of Passengers。3我现在更正的CustomerName事件,都是因为我以前在代码中做了其他事情。4在我的问题中,有人提到我应该使用常量值以及名为KidDiscount和SeniorDiscount的字段。因此,我在那里做了一些不专业的事情来满足问题要求5好的,我将用一些其他的逻辑替换所有的goto。还有护照号码检查逻辑。谢谢你的帮助。荣誉
class Customer 
  {
    const double K_DISCOUNT = 0.10;                            
    const double S_DISCOUNT = 0.20;

    private double STANDARD_FARE;
    private string CustomerName { get; set; }
    private int CustomerAge;
    private string CustomerFName;
    private string CustomerLName;
    private long CustomerPassport;
    private double CustomerPrice; 
    private string CustomerTitle;
    private double KidDiscount;
    private double SeniorDiscount;

    public Customer()                                         
    {
        this.KidDiscount = K_DISCOUNT;
        this.SeniorDiscount = S_DISCOUNT;
    }

    public double StandardFare
    {
        get { return STANDARD_FARE; }
        set { STANDARD_FARE = value; }
    }

    public int Customer_Age
    {
        get { return CustomerAge; }
        set { CustomerAge = value; }
    }

    public string Customer_Title
    {
        get { return CustomerTitle; }
        set { CustomerTitle = value; }
    }

    public string Customer_FName
    {
        get { return CustomerFName; }
        set { CustomerFName = value; }
    }

    public string Customer_LName
    {
        get { return CustomerLName; }
        set { CustomerLName = value; }
    }

    public long CustomerPassNo
    {
        get { return CustomerPassport; }
        set { CustomerPassport = value; }
    }

    public double CustomerTicket
    {
        get { return CustomerPrice; }
        set { CustomerPrice = value; }
    }


    public int CalculatePrice()
    {
        if (CustomerAge < 3)
        {
            CustomerPrice = 0;
        }
        else if (CustomerAge >= 3 && CustomerAge < 18)
        {
            CustomerPrice = STANDARD_FARE - (STANDARD_FARE * KidDiscount);
        }
        else if (CustomerAge > 65)
        {
            CustomerPrice = STANDARD_FARE - (STANDARD_FARE * SeniorDiscount);
        }
        else
        {
            CustomerPrice = STANDARD_FARE;
        }
        return 0;
    }

    public void Display()
    {
      //some code here
    }
}