2012-02-18 154 views
0

我不太确定为什么,但是当我放入中断点并跨过我的代码时,每个属性都返回null或0当他们应该成为我放在主要的地方,我疯狂的类的实例。我的代码没有错误,但应该显示的输出没有显示

这是我的主

namespace DemoJobs 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string desc; 
      decimal rate, time, total; 

      Job job1 = new Job("Pour Driveway", 8m, 50.00m); 
      Job job2 = new Job("Instal New Windows", 18m, 120m); 
      Job job3 = job1 + job2; 

      Console.WriteLine("The {0} job will take {1} hours, costing {2} per hour with a total of {3}", 
       job1.Description, job1.Time, job1.Rate.ToString("C"), job1.Total.ToString("C")); 

      Console.WriteLine("The {0} job will take {1} hours, costing {2} per hour with a total of {3}", 
       job2.Description, job2.Time, job2.Rate.ToString("C"), job2.Total.ToString("C")); 

      Console.WriteLine("The {0} job will take {1} hours, costing {2} per hour with a total of {3}", 
       job3.Description, job3.Time, job3.Rate.ToString("C"), job3.Total.ToString("C")); 

      Console.ReadLine(); 
     } 
    } 
} 

这是我的类

namespace DemoJobs 
{ 
    public class Job 
    { 
     // variables 
     private string _description; 
     private decimal _time, _rate, _total; 

     // property for the job description 
     public string Description 
     { 
      get { return _description; } 
      set { _description = value; } 
     } 

     // property for the total job time 
     public decimal Time 
     { 
      get { return _time; } 
      set { _time = value; CalcTotal(); } 
     } 

     // property for the jobs hourly rate 
     public decimal Rate 
     { 
      get { return _rate; } 
      set { _rate = value; CalcTotal(); } 
     } 

     // read-only property for the job total 
     public decimal Total 
     { 
      get { return _total; } 
     } 

     // method to calculate the total for the job 
     public decimal CalcTotal() 
     { 
      _total = _rate * _time; 
      return _total; 
     } 

     // constructor 
     public Job(string description, decimal time, decimal rate) 
     { 
      Description = _description; 

      Time = _time; 

      Rate = _rate; 
     } 

     public static Job operator +(Job job1, Job job2) 
     { 
      string newDescription = job1.Description + " and " + job2.Description; 

      decimal newTime = job1.Time + job2.Time; 

      decimal newRate = (job1.Rate + job2.Rate)/2; 

      decimal newTotalFee = newRate * newTime; 

      return (new Job(newDescription, newTime, newRate)); 
     } 
    } 
} 
+1

Hm no error?如果你有两份工作时间和费率不同,那么合并工作的比例不仅仅是两份工作比率的平均值。 – 2012-02-18 22:34:37

+0

问题有两个部分,第二部分涉及对工作进行加权平均,以使总费用更准确。 – 2012-02-19 21:30:55

回答

1

在你的构造有点你正在使用错误的变量引用简化你的类_description ... _description是私人会员。

5

只需改变(一个例子)

Description = _description; 

Time = _time; 

Rate = _rate; 

Description = description; 

Time = time; 

Rate = rate; 

在您当前的代码中,您使用实例字段而不是实际的ctor参数。 修改你的ctor超载,关注这方面并完成。

0

看看你的CTOR。 您正在分配错误的值。而不是: public Job(字符串描述,十进制时间,十进制速率) { Description = _description;

 Time = _time; 

     Rate = _rate; 
    } 

你应该写: 公开招聘(字符串描述,小数时,小数率) { 说明=描述;

 Time = time; 

     Rate = rate; 
    } 
0

Job构造函数有一个bug:您正在使用description作为参数,但分配_description ....没有提供这一个,是null,所以你Descriptionnull

public Job(string description, decimal time, decimal rate) 
    { 
     Description = description; 

     Time = time; 

     Rate = rate; 
    } 
+0

我改变了我的构造函数中的每个部分,程序显示了它的设想,现在我必须调整格式,我将全部设置。谢谢您的帮助。 – 2012-02-18 22:39:18

0
public Job(string description, decimal time, decimal rate) 
{ 
     Description = _description; 

     Time = _time; 

     Rate = _rate; 
} 

你正在传递的描述并不:

您也可以通过使用自动属性

public String Description {get; set;} 
0

看起来也许像你的构造函数是倒退...

public Job(string description, decimal time, decimal rate) 
    { 
     Description = _description; 

     Time = _time; 

     Rate = _rate; 
    } 

应该 公开招聘(字符串描述,小数时,小数率) { _description =说明;

 _time = time; 

     _rate = rate; 
    } 
相关问题