C# 需要一些关于合并两个类的帮助吗

C# 需要一些关于合并两个类的帮助吗,c#,wpf,C#,Wpf,在我开始之前,我只想说我仍处于C的学习阶段,请宽恕我的灵魂 我现在正忙着用visualstudio代码(WPF)编写一个过于简单的程序 这是让我头疼的代码: using System; public Window3() { InitializeComponent(); // INIT FIRST ANIMAL. string name = "Spotty"; int age = 6; flo

在我开始之前,我只想说我仍处于
C
的学习阶段,请宽恕我的灵魂

我现在正忙着用visualstudio代码(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

我真的很感激任何帮助

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

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

 public static string AnimalList(string _name, int _age, float _happiness, string _animal_type)
*这是因为没有实例,因为您使用的字段都是静态的

另一方面,您当前的方法中有很多地方可以清理,以使您的工作更加轻松

您可以使用一种方法:

 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>();
 // 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();
 }
  • 创建一个新类以保存动物属性
  • 使用
    列表
    保存您添加/创建的动物
  • 动物类:

     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>();
    
     // 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();
     }
    
    添加一个新字段来保存这些动物的:

     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>();
    
     // 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();
     }
    
    打印例程:

     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>();
    
     // 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();
    

    “一个简单的程序”-这里的许多东西实际上是不必要的complicated@ASh我知道是的。我主要是用这个程序来做实验和学习。就像我说的,我对C#lol还是新手。也许格式化和修复上面的代码是个好主意。它不编译。少了一些东西。你不能做
    Count++就在类中的任何位置,并且类作用域过早完成。非常感谢Codexer!我刚刚实施了你的方法,效果更好。你绝对是个传奇和天才。非常感谢你的帮助,我真的很感激!