C#如何在2个类中引用/使用/设置对象/实例的值

时间:2017-03-18 16:39:26

标签: c# class object properties instance

我正在开展一个简短的项目,其目标是创建一个计算净工资的计划。用户将输入他们的姓名,工作时间和工资率。我必须使用Properties而不是构造函数来接收数据。应该有两个阶段; Main类,以及一个名为Pay的类,它将执行计算并使用覆盖ToString()方法来显示结果(使用string.format命令)。

我遇到的问题是程序能够显示人员姓名,工作时间和工资率(所有这些都是用户输入的)。但是,许多其他变量显示为“0”。我也无法弄清楚如何设置这些变量,以便它们是我在Main类中创建的对象“aPay”的实例。

我应该将这些变量设置为Main类或Pay类中的aPay对象的实例吗?如果是这样,我该怎么做呢?我在过去一小时左右尝试了很多事情,试图解决这个问题,甚至引用了我上周作为一项任务的类似程序,但我似乎无法理解这个概念及其正确性实现。

我将在下面发布我的代码,真诚地感谢任何帮助和解释。

import random
import time

choice=0

def introDisplay():
    print('This is the pre-game story.')
    time.sleep(1)
    print('It lasts for 5 lines.')
    time.sleep(1)
    print('When you can be arsed, fix this.')
    time.sleep(1)
    print('Thanks,')
    time.sleep(1)
    print('You, from 18/3/17')
    print()
    firstChoice()

def firstChoice():
    time.sleep(2)
    print('You come across a path, it splits at the end.')
    time.sleep(1)
    choice=raw_input('Which path do you take, the left path (1) or the right path (2)? \n')
    checkChoice(choice)

def checkChoice(choice):
    correct=False
    if choice=='1' or choice=='2':
        correct_choice=random.choice(['1','2'])
        if choice==correct_choice:
            correct=True
            print('You chose the correct path')
        else:
            print('You dont chose the correct path')
    else:
        print('You decide to not take a path, and you die due to random circumstances.')
        time.sleep(1)
        print('Take a path next time, or at least take it correctly.')
        failScreen()




def failScreen():
    restart=True
    print('You have failed.')
    print('Do you want to retry?')
    restart1=input('Y or y = Yes. N or n = No. \n')
    if restart1=='y' or restart1=='Y':
        restart=True
    if restart1=='n' or restart1=='N':
        restart=False
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y':
        failScreen()
    if restart==True:
        introDisplay()
    if restart==False:
        exit()

introDisplay()

那是Main课程,我将在下面发布Pay课程:

class MainClass
{
    public static void Main(string[] args)
    {


        Header();
        Directions();

        Pay aPay = new Pay();
        Write("**********************************\n");
        Write("Enter name:  ");
        aPay.WorkerName = ReadLine();

        Write("Enter hours: ");
        aPay.HoursWorked = double.Parse(ReadLine());

        Write("Enter rate:  ");
        aPay.RateOfPay = double.Parse(ReadLine());
        Write("**********************************\n");



        WriteLine(aPay.ToString());




    }

    private static void Header()
    {
        WriteLine("*************************************************************");
        WriteLine("\t Pay");
        WriteLine("\t Calculate Net Pay");
        WriteLine("\t Matt Craig");
        WriteLine("\t " + DateTime.Today);
        WriteLine("*************************************************************");
    }

    private static void Directions()
    {
        WriteLine("This program will determine pay.");
        WriteLine(" ");
        WriteLine("You will be asked to enter hours worked" 
                  + "\n and rate of pay");
        WriteLine(" ");
        WriteLine("*************************************************************");
    }
}

1 个答案:

答案 0 :(得分:1)

这里的问题是你没有在ToString内调用getter,你正在调用字段,除非你通过属性的get方法,否则它们没有值。

public override string ToString()
{
    string stats;
    stats = string.Format("Name\t\t  {0} \n", workerName);
    stats += string.Format("Gross Pay\t  {0} \n", GrossPay);
    stats += string.Format("FICA tax\t  {0} \n", FicaTax);
    stats += string.Format("Federal tax\t  {0} \n", FedTax);
    stats += string.Format("State tax\t  {0} \n", StateTax);
    stats += string.Format("Health Insurance  {0} \n", HealthIns);
    stats += string.Format("Net pay\t\t  {0} \n", NetPay);
    return stats;
}

看到我将名称更改为以大写字母开头,以便它们引用属性。