简体   繁体   English

以下 c# 代码中属性成人的堆栈溢出异常

[英]stack overflow exception for the property adult in the below c# code

The code is for taking the input from user such as their name and date of birth and return details as name and their age and whether they are child or not该代码用于获取用户的输入,例如他们的姓名和出生日期,并返回详细信息作为姓名和年龄以及他们是否是孩子

Apart from the Adult property the rest of the code works fine除了成人属性之外,代码的 rest 工作正常

using System;
using System.IO;

public class Person
{
    //Fill code here
    private string firstName;
    private string lastName;
    private DateTime dob;
    //private string adult;

    public string FirstName
    {
        set { firstName = value; }
        get { return firstName; }
    }

    public string LastName
    {
        set { lastName = value; }
        get { return lastName; }
    }

    public DateTime Dob
    {
        set { dob = value; }
        get { return dob; }
    }
}

In the below property it has to check age and return "Adult" if the age is above or equal to 18 else "Child".在下面的属性中,它必须检查年龄,如果年龄大于或等于 18 岁,则返回“成人”,否则返回“儿童”。

And according to the question i cannot declare the field for this.根据问题,我不能为此声明该字段。

public string Adult
{
    get
    {
        return Adult;
        throw (stackoverflow)
    }
    set
    {
        if (GetAge(dob) >= 18)
        {
            Adult = "Adult";
        }
        else
        {
            Adult = "Child";
        }
    }
}

Help me with the corrections needed for the above property so that it does not throw any exceptions and the reason why it throws the exception帮助我进行上述属性所需的更正,使其不会引发任何异常以及引发异常的原因

public void DisplayDetails()
{
    Console.WriteLine("First Name: {0}", firstName);
    Console.WriteLine("Last Name: {0}", lastName);
    int age = GetAge(dob);
    Console.WriteLine("Age: {0}",age);
    Console.WriteLine(Adult);
    }
    
    public int GetAge(DateTime dob)
    {
        int age = 0;
        age = DateTime.Now.Year - dob.Year;
        return age;    
    }
}

public class Program
{
    public static void Main(String[] args)
    {
        //Fill code here
        Person p = new Person();
        Console.WriteLine("Enter first name");
        p.FirstName = Console.ReadLine();
        Console.WriteLine("Enter last name");
        p.LastName = Console.ReadLine();
        Console.WriteLine("Enter date of birth in yyyy/mm/dd/ format");
        p.Dob = Convert.ToDateTime(Console.ReadLine());
        
        p.DisplayDetails();
    }
}

You have an Adult property, and inside that property you manually defined a getter and setter, but they both use exactly the same property instead of using a field like you do in your other properties.您有一个 Adult 属性,并且在该属性中您手动定义了一个 getter 和 setter,但它们都使用完全相同的属性,而不是像您在其他属性中那样使用字段。

Note that the fields used in your other properties have lowercase names, while the properties start with an Upper case letter (which means that they are separate entities).请注意,您的其他属性中使用的字段具有小写名称,而属性以大写字母开头(这意味着它们是单独的实体)。

So you did declare adult like this but have commented it out:因此,您确实像这样声明了成人,但已将其注释掉:

//private string adult;

So you need to first uncomment it, then use it in your getter & setter.所以你需要先取消注释,然后在你的 getter & setter 中使用它。

public string Adult
    {
        get
        {
            return adult;
        }  
        set
        {
            if (GetAge(dob) >= 18)
            {
                adult = "Adult";
            }
            else
            {
                adult = "Child";
            }
        }
    }

But this also doesn't make sense, as your setter is not using the value which you pass into it, So while this might work.但这也没有意义,因为您的设置器没有使用您传递给它的值,所以虽然这可能有效。 it is not really the best way to do it.这并不是最好的方法。 Instead just use the Getter to return if it is an adult or child each time.相反,如果每次都是成人或儿童,只需使用 Getter 即可返回。

 public string Adult
  {
        get
        {
            if (GetAge(dob) >= 18)
                     return "Adult";
            else
                return "Child";
        }
}

(You should also include handling in case dob value is not valid (eg default value) (您还应该包括处理 dob 值无效的情况(例如默认值)

BTW: It might be better to use an Enum rather than a string, or just a boolean indicating if it is an adult (true) or child (false).顺便说一句:使用枚举而不是字符串可能会更好,或者只使用 boolean 来指示它是成人(真)还是儿童(假)。

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

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