简体   繁体   English

在C#中传递类值?

[英]Class value passing in c#?

I'm trying to pass values stored in a class to a program, but I am not sure how! 我正在尝试将存储在类中的值传递给程序,但是我不确定如何传递! I felt I was so close to completing this program but when running it it only displays what it is written in the actual program and it doesn't display options to store values on the class variable. 我觉得我已经接近完成该程序了,但是在运行它时,它仅显示实际程序中编写的内容,并且不显示用于将值存储在类变量中的选项。 If anyone could help me guide on what am I doing wrong it would be greatly appreciated. 如果有人可以帮助我指导我在做什么错,将不胜感激。

Here is the class I have written. 这是我写的课。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Pet
    {
        public string Name;
        public string Type;
        public double Age;

        public string setName()
        {
            Console.WriteLine("Please write your pet's name");
            Name = Console.ReadLine();
            return Name;
        }

        public string setType()
        {
            Console.WriteLine("What type of animal is your pet?");
            Type = Console.ReadLine();
            return Type; 
        }

        public double getAge()
        {
            Console.WriteLine("Input your pet's age"); 
            while(!double.TryParse(Console.ReadLine(), out Age))
                Console.WriteLine("Please enter a valid number");
            return Age;
        }
    }
}

And here is the actual program: 这是实际的程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Pet mypet = new Pet();
            mypet.Name = "";
            mypet.Type = "";
            mypet.Age = 0;

            Console.WriteLine("The name of your pet is:" + mypet.Name);

            Console.WriteLine("The type of animal your pet is:" + mypet.Type);

            Console.WriteLine("The age of your pet is:" + mypet.Age);
        }
    }
}

I think that, following the way your code is organised, your program should look like this: 我认为,按照代码的组织方式,程序应如下所示:

class Program
{
    static void Main(string[] args)
    {
        Pet mypet = new Pet();
        mypet.setName()
        mypet.setType();
        mypet.getAge();

        Console.WriteLine("The name of your pet is:" + mypet.Name);
        Console.WriteLine("The type of animal your pet is:" + mypet.Type);
        Console.WriteLine("The age of your pet is:" + mypet.Age);
    }
}

By the way, the it could be more consistent naming your methods the following way: 顺便说一下,通过以下方式命名方法可能会更加一致:

PromptName();
PromptType();
PromptAge();

As that is what they actually do. 因为那是他们实际上所做的。 Notice the UpperCamelCase notation ( http://en.wikipedia.org/wiki/CamelCase ), which is the standard when writing C# code. 请注意UpperCamelCase表示法( http://en.wikipedia.org/wiki/CamelCase ),这是编写C#代码时的标准。

You are not requesting for input, simply displaying the output. 您不需要输入,只需显示输出即可。

Change your code to something like 将您的代码更改为

    static void Main(string[] args)
    {
        Pet mypet = new Pet();

        mypet.setName();
        mypet.setType();
        mypet.getAge();

        Console.WriteLine("The name of your pet is:" + mypet.Name);
        Console.WriteLine("The type of animal your pet is:" + mypet.Type);
        Console.WriteLine("The age of your pet is:" + mypet.Age);

    }

Your class definition should probably look like this: 您的类定义应该看起来像这样:

class Pet
{
    public string Name { get; set; }
    public string Type { get; set; }
    public DateTime DateOfBirth { get; set; }
}

You want to use auto-implemented properties instead of public fields. 您想使用自动实现的属性而不是公共字段。 You also do not want to put input/output logic into your data class! 您也不想将输入/输出逻辑放入数据类! That's bad object oriented design. 那是糟糕的面向对象设计。

You should also not store age as the number of years. 您也不应将年龄存储为年数。 Age should be represented by date of birth so that you can always calculate the age. 年龄应以出生日期表示,以便您始终可以计算出年龄。 That way if you store your pet's information in a file and load it one year later your program will know your pet is one year older. 这样,如果您将宠物的信息存储在文件中,并在一年后加载,则程序将知道您的宠物已经大一岁了。

var myPet = new Pet();

Console.WriteLine("Please write your pet's name");
myPet.Name = Console.ReadLine();

Console.WriteLine("The type of animal your pet is:");
myPet.Type = Console.ReadLine();

DateTime dateOfBirth;
string line;

do
{
    Console.WriteLine("The date of birth of your pet:");
    line = Console.ReadLine();
} while(DateTime.TryParse(line, out dateOfBirth);

myPet.DateOfBirth = dateOfBirth;

Console.WriteLine("The name of your pet is:", myPet.Name);
Console.WriteLine("The type of animal your pet is: ", myPet.Type);
Console.WriteLine("The age of your pet is:", GetAge(myPet.DateOfBirth));

You could implement the GetAge function as follows: 您可以按以下方式实现GetAge函数:

public static int GetAge(DateTime birthDate)
{
    DateTime n = DateTime.Now; // To avoid a race condition around midnight
    int age = n.Year - birthDate.Year;

    if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
    age--;

    return age;
}

在控制台写入之前,您没有对mypet.setName(),mypet.setType()或mypet.getAge()的调用。

You haven't implemented properties correctly. 您尚未正确实现属性。 See http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx 请参阅http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

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

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