需要有关合并两个课程的帮助

时间:2019-12-16 13:44:35

标签: c# wpf

在开始之前,我只想说我仍处于C#的学习阶段,请对我的灵魂保持怜悯。

任何人,我目前正在用Visual Studio代码(WPF)编写一个简单的程序。

这是让我头疼的代码:

using System;


    public Window3()
    {

        InitializeComponent();

        // INIT FIRST ANIMAL.

        string name = "Spotty";
        int age = 6;
        float happiness = 0.5f;
        string animal_type = "Dog";

        // NUMBER OF ANIMALS INIT VAR.

        int Count = 0;

        Count++;

        // FIRST ANIMAL EXAMPLE.

        Test.Content = "Name: " + name + "\n" + "Age: " + age + "\n" + "Happiness: " + happiness + "\n" + "Animal Type: " + animal_type;

        Test1.Content = AnimalList("idk", 10, 0.5f, "idk");

        Count++;

        //Test2.Content = "";



        // LIST NUMBER OF ANIMALS FUNCTION.

        Test3.Content = "Number of Animals: " + Count;
    }


    public static int Count { get; private set; }
        public static string name { get; private set; }
        public static int age { get; private set; }
        public static float happiness { get; private set; }
        public static string animal_type { get; private set; }

        public static string AnimalList(string _name, int _age, float _happiness, string _animal_type)
    {
        name = _name;
        age =  _age;
        happiness = _happiness;
        animal_type = _animal_type;

        return name + age + happiness + animal_type;
    }

    public void Print() 
    {

        Test1.Content = "Name: " + name + "\n" + "Age: " + age + "\n";

    }

}

现在要详细说明我的问题。

我希望Test1.Content = AnimalList("idk", 10, 0.5f, "idk");

合作
public void Print() 
    {

        Test1.Content = "Name: " + name + "\n" + "Age: " + age + "\n";

    }

我怎样才能做到这一点?因为我希望它在标签内这样显示:

  

名称:idk

     

年龄:10

     

幸福:0.5

     

动物类型:idk

我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

  

我想要Test1.Content = AnimalList(“ idk”,10,0.5f,“ idk”);与

一起工作
public string AnimalList(string _name, int _age, float _happiness, string _animal_type)
{
    name = _name;
    age =  _age;
    happiness = _happiness;
    animal_type = _animal_type;

    return name + age + happiness + animal_type;
}

我可以看到的主要问题是AnimalList不是静态的,签名需要更改为:

 public static string AnimalList(string _name, int _age, float _happiness, string _animal_type)

*之所以这样,是因为没有实例,因为您使用的字段都是静态的。

另一方面,您当前的方法有很多可以清理的地方,使您使用起来更容易。

以下是您可以使用的一种方法:

  1. 创建一个新类来保存动物属性。
  2. 使用List<Animal>来保存您添加/创建的动物。

动物类:

 public class Animal
 {
    #region | Enums |

    public enum AnimalType
    {
        Dog = 0,
        Cat = 1,
    }

    #endregion

    #region | Properties |

    public string Name { get; set; } = string.Empty;

    public int Age { get; set; } = 0;

    public float Happiness { get; set; } = 0f;

    public AnimalType Animaltype { get; set; } = AnimalType.Dog;

    #endregion

    #region | Constructor |

    public Animal(string name, int age, float happiness, AnimalType animalType)
    {
        Name = name;
        Age = age;
        Happiness = happiness;
        Animaltype = animalType;
    }

    #endregion

    #region | Overrides |

    public override string ToString()
    {
        return Name + Environment.NewLine + Age.ToString() + Environment.NewLine + Happiness.ToString() + Environment.NewLine() + Animaltype.ToString();
    }

    #endregion
}

添加一个新字段来保存这些动物:

 public List<Animal> AnimalList = new List<Animal>();

InitializeComponent()之后添加:

 // Add a new animal to the list
 AnimalList.Add(new Animal("Spotty", 6, 0.5f, Animal.AnimalType.Dog));
 // Print the first animal out
 Print();

打印例程:

 public void Print()
 {
     Test1.Content = AnimalList[0].ToString();
 }

如果您要计数动物,现在可以致电:

 AnimalList.Count();