简体   繁体   English

C#Getter Setter

[英]C# Getter Setter

I am working on a homework related to fractions. 我正在做与分数相关的家庭作业。 I want to prevent the user to input a number smaller than 1 for denominator("nenner") and numerator("zähler). I tried to implement this with set but it doesn't work. If the numerator/denominator is smaller than 1 I want them to be set to 1. Any tips on the mistake in my code would be appreciated. 我想阻止用户为分母(“nenner”)和分子(“zähler”)输入小于1的数字。我试图用set实现它但是它不起作用。如果分子/分母小于1我希望它们设置为1.我的代码中的错误提示将不胜感激。

namespace Aufgabenblatt2
{
class Bruch
{
    private int zähler;
    public int Zähler
    {
        get { return zähler; }
        set 
        {
            if (value < 1)
            {
                //Console.WriteLine("Keine negativen Brüche und Division durch 0 erlaubt! Wert wurde auf 1 gesetzt");
                zähler = 1;
            }
            else
                zähler = value; 
        }
    }

    private int nenner;
    public int Nenner
    {
        get { return nenner; }
        set
        {
            if (value < 1)
            {
                //Console.WriteLine("Keine negativen Brüche und Division durch 0 erlaubt! Wert wurde auf 1 gesetzt");
                nenner = 1;
            }
            else
                nenner = value;
        }
    }


    public Bruch(int zähler, int nenner)
    {
        this.zähler = zähler;
        this.nenner = nenner;
    }

    /// <summary> 
    /// addiert zum Bruch einen weiteren hinzu 
    /// </summary> 
    /// <param name="summand">Summand</param> 
    /// <returns>Summe</returns> 
    public Bruch Addiere(Bruch summand)
    {
        Bruch ergebnis = new Bruch(this.zähler * summand.nenner + summand.zähler * this.nenner, this.nenner * summand.nenner);
        return ergebnis;
    }

    public override string ToString()
    {
        return String.Format("{0}/{1}", this.zähler, this.nenner);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Console.Write("Eingabe erster Zähler:");
        int zähler1 = Convert.ToInt32(Console.ReadLine());       //Read numerator value from console and save to int zähler
        Console.Write("Eingabe erster Nenner:");
        int nenner1 = Convert.ToInt32(Console.ReadLine());       //Read denominator value from console and save to int nenner

        Bruch b1 = new Bruch(zähler1, nenner1);      


        Console.Write("Eingabe zweiter Zähler:");
        int zähler2 = Convert.ToInt32(Console.ReadLine());       //Read second numerator
        Console.Write("Eingabe zweiter Nenner:");
        int nenner2 = Convert.ToInt32(Console.ReadLine());       //Read second denominator

        Bruch b2 = new Bruch(zähler2, nenner2);      

        Console.WriteLine("{0} + {1} = {2}", b1, b2, b1.Addiere(b2));   //Print both fractions, compute and print sum on Console

        Console.ReadLine();
    }
}

} }

You don't use your setters. 你没有使用你的二传手。 At Bruch b1 = new Bruch(zähler1, nenner1); Bruch b1 = new Bruch(zähler1, nenner1); you set your fields in the constructor, not the properties . 您在构造函数中设置字段 ,而不是属性

Change your constructor to set the properties: 更改构造函数以设置属性:

public Bruch(int zähler, int nenner)
{
    Zähler = zähler;
    Nenner = nenner;
}

You could've caught this by putting a breakpoint at the setters and see they're never called, then track back through the code to where the values are set. 你可以通过在setter上设置一个断点并看到它们从未被调用来捕获它,然后通过代码追溯到值设置位置。

Your problem is with your constructor: 你的问题是你的构造函数:

public Bruch(int zähler, int nenner)
{
    this.zähler = zähler;
    this.nenner = nenner;
}

Your constructor is by-passing the setter by directly setting the field. 您的构造函数通过直接设置字段绕过setter。 Change it to: 将其更改为:

public Bruch(int zähler, int nenner)
{
    this.Zähler = zähler;
    this.Nenner = nenner;
}

You setter would have been hit if you did this: 如果你这样做,你的setter会被击中:

 b1.Zähler = 0; 
 Console.Writeline(b1.Zähler.ToString());     // should print 1

But your constructor side-stepped this. 但是你的构造函数支持这一点。 This is one of the problems you get into when your property and your backing field only vary by capitalization. 当您的财产和支持字段仅因大小写而异时,这是您遇到的问题之一。 They are very easy to mix up. 它们很容易混淆。 Which is a good argument for prefixing the backing field with something like _ , although I'm not personally a fan of that style. 尽管我不是那种风格的粉丝,但这对于在后备字段前加上_东西来说是一个很好的论据。

You do not use the setter in the constructor! 你不在构造函数中使用setter!

public Bruch(int zähler, int nenner)
{
    this.Zähler = zähler; // << Use the setter here! 
    this.Nenner = nenner;
}

On a different topic: It is a much better style to not use "Umlaute" for variable names. 在另一个主题上:对于变量名称不使用“Umlaute”是一种更好的方式。 Always use english names. 始终使用英文名称。

that is because you are setting the wrong parameter. 那是因为你设置了错误的参数。 Replace the constructor on Bruch for this one. 替换Bruch上的构造函数。

public Bruch(int zähler, int nenner)
{
    this.Zähler = zähler;
    this.Nenner = nenner;
}

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

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