简体   繁体   English

C#与另一个类中的一个类的变量进行交互

[英]C# Interact with variable of a class in an other class

I'm learning C# and I asked the best method to interact with variable of a class out of this class. 我正在学习C#,并要求最好的方法与此类中的某个类的变量进行交互。 I thought about this: 我想到了:

public class Character
    {
        private int x, y;

        public Character(int posX, int posY)
        {
            x = posX;
            y = posY;
        }

        public int X
        {
            get
            { 
                return x;
            }
            set
            { 
                x = value;
            }
        }

        public int Y
        {
            get
            { 
                return y;
            }
            set
            { 
                y = value;
            }
        }
    }`

    class MainClass
    {
        public static void Main (string[] args)
        {
            Character hero = new Character (42, 36);
            Console.WriteLine (hero.X);
            Console.WriteLine (hero.Y);
            hero.X = 5;
            Console.WriteLine (hero.X);
        }
    }

I don't know if this method is good or optimized but it works. 我不知道这种方法是好的还是优化的,但是它可以工作。 But, if I want do this for 10 variables, my class will do minimum (more if I want add a test in get/set) 100 lines just for my variables... Do you know an other method to proceed? 但是,如果我想对10个变量执行此操作,则我的班级将为我的变量执行最少100行(如果我要在get / set中添加测试),这将做100行...您知道其他方法可以继续吗? Or how I can compress this method? 或如何压缩此方法? Thank you! 谢谢!

You can use auto-implemented properties : 您可以使用自动实现的属性

public class Character
{
   public Character(int x, int y)
   {
      X = x;
      Y = y;
   }

   public int X { get; set; }
   public int Y { get; set; }
}

Also in real life if you need to update lot of variables of other class consider to redesign your code. 同样在现实生活中,如果您需要更新许多其他类的变量,请考虑重新设计代码。 Probably you have separated data and logic into two different classes. 可能您已将数据和逻辑分为两个不同的类。 Consider to combine two classes into one with data and logic which process that data. 考虑将两个类别与数据和处理该数据的逻辑合并为一个类别。

NOTE: there is big update comming to next versions of C# called Record Types which will allow you just list all class properties after class name: 注意:C#的下一版本有一个重大更新,称为记录类型 ,它将允许您仅在类名称后列出所有类属性:

class Character(int X, int Y);

That is good syntax sugar for Data Transfer Objects. 对于数据传输对象来说,这是很好的语法糖。

this kind of variables is called property, it used when I want the variable to be readable and editable from other objects, so it is best solution to your issue, even if you'll increase the number of properties or add checking in get/set. 这种变量称为属性,当我希望该变量可从其他对象读取和编辑时使用,因此即使您增加属性的数量或在get / set中添加检查,它也是解决问题的最佳解决方案。 go on! 继续!

In addition to auto-properties, you can use Object initializer . 除了自动属性,您还可以使用Object initializer In that case you do not need to declare a constructor explicitly. 在这种情况下,您无需显式声明构造函数。 You can save few more lines of code, using that feature . 使用该功能,您可以再保存几行代码。 Check the code below to see the changes: 检查下面的代码以查看更改:

public class Character
{       
   public int X { get; set; }
   public int Y { get; set; }
}


class MainClass
{
    public static void Main (string[] args)
    {
        var hero = new Character {X = 42, Y = 36};
        Console.WriteLine(hero.X);
        Console.WriteLine(hero.Y);
        hero.X = 5;
        Console.WriteLine (hero.X);
    }
}

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

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