C# C语言中的类实例通信#

C# C语言中的类实例通信#,c#,design-patterns,instance,C#,Design Patterns,Instance,我正在做一个游戏,有几个类,每个类都做一些特定的任务 我对OOP完全陌生,我想知道应该怎么做才能使我的类实例在彼此之间进行通信,而不必重复使用静态类、方法和属性,这似乎是一件可怕的事情 我是自学成才的程序员,我意识到我做了很多不好的实践。到目前为止,我设法使这两个类都是静态的,但我想知道我应该做些什么,使我的代码尽可能好 另外,如果您能向我推荐一些资源/书籍/文章,这样我就可以阅读更多关于这个主题的内容(实例之间的交流),那将是一件非常好的事情 这是一段代码,让你明白我在说什么 class Pr

我正在做一个游戏,有几个类,每个类都做一些特定的任务

我对OOP完全陌生,我想知道应该怎么做才能使我的类实例在彼此之间进行通信,而不必重复使用静态类、方法和属性,这似乎是一件可怕的事情

我是自学成才的程序员,我意识到我做了很多不好的实践。到目前为止,我设法使这两个类都是静态的,但我想知道我应该做些什么,使我的代码尽可能好

另外,如果您能向我推荐一些资源/书籍/文章,这样我就可以阅读更多关于这个主题的内容(实例之间的交流),那将是一件非常好的事情

这是一段代码,让你明白我在说什么

class Program
{
    static void Main(string[] args)
    {
        Class1 instance1 = new Class1();
        Class2 instance2 = new Class2();

        // infinite loop
        while (true)
        {
            instance1.UpdateMethod(someValue);
            instance2.UpdateMethod();
        }
    }
}

class Class1
{
    int Property;
    UpdateMethod(int argument)
    {
        Property += argument;
        if(Property == 3000)
        {
            // I should change the state of instance2
        }
    }
}

class Class2
{
    UpdateMethod()
    {
        if(Time.GetTime() == SomeTime)
        {
            // here I want to change the state of instance1
        }
    }
}

对于常见设计模式的概述,我建议

如果在
Class1
Class2
之间存在一种自然关系,那么一个实例引用另一个实例是很常见的。例如,如果您有一个
玩家
类,而该玩家有一个
武器
,请如下定义您的类:

public class Player
{
    public Weapon Weapon { get; set; }
    // Other properties
}
Class2 instance2 = new Class2(instance1);
instance2.UpdateMethod();
Class2 instance2 = new Class2();
instance2.UpdateMethod(instance1);
特别是在您的例子中,看起来您希望从
Class2
的实例更新
Class1
的实例。我建议您在
Class2
上定义一个属性,该属性保存
Class1
的相关实例,就像上面的示例一样

这就是所谓的

在软件工程中,复合模式是一种划分模式 设计模式。复合模式描述了一组 对象的处理方式与对象的单个实例相同 对象合成的目的是将对象“合成”到树中 表示部分-整体层次结构的结构。实施 复合模式允许客户端处理单个对象和 构图均匀

另一个经常用于作用于对象实例的模式是

在面向对象编程中,命令模式是一种设计 一种模式,其中一个对象用于表示和封装所有 以后调用方法所需的信息。这 信息包括方法名称、拥有该方法的对象 和方法参数的值。三个术语总是联系在一起的 命令模式包括客户端、调用者和接收者。客户 实例化命令对象并提供所需的信息 以便稍后调用该方法。调用程序决定 应该调用。receiver是 包含方法的代码。使用命令对象可以更容易地 构造需要委托、排序或 在他们选择的时间执行方法调用,而无需 了解方法或方法参数的所有者


我建议你两个链接,他们的内容是免费阅读的

更多关于细节更好的模式是


最后一个是bob叔叔关于OOD原则的好解释

如果您需要更改另一个类中对象的状态,您需要对它的引用。常用的方法是通过构造函数:

public class Class2
{
    private readonly Class1 instance;

    public Class2(Class1 instance)
    {
        this.instance = instance;
    }

    public void UpdateMethod()
    {
        if(VisualStyleElement.TaskbarClock.Time.GetTime() == SomeTime)
        {
            // here I want to change the state of instance1
            this.instance.SomeProperty = "Some Value";
        }
    }
}
或者通过传递到方法中的参数

public class Class2
{
    public void UpdateMethod(Class1 instance)
    {
        if (VisualStyleElement.TaskbarClock.Time.GetTime() == SomeTime)
        {
            // here I want to change the state of instance1
            instance.SomeProperty = "Some Value";
        }
    }
}
在第一种情况下,您可以这样称呼它:

public class Player
{
    public Weapon Weapon { get; set; }
    // Other properties
}
Class2 instance2 = new Class2(instance1);
instance2.UpdateMethod();
Class2 instance2 = new Class2();
instance2.UpdateMethod(instance1);
在第二种情况下,你可以这样称呼它:

public class Player
{
    public Weapon Weapon { get; set; }
    // Other properties
}
Class2 instance2 = new Class2(instance1);
instance2.UpdateMethod();
Class2 instance2 = new Class2();
instance2.UpdateMethod(instance1);

欢迎来到OOP世界! 需要理解的一件重要事情是继承的概念。继承关系到什么是。孩子是人,母亲是人

请看一下这个型号:

public class Person
    {
        protected string Name;

        public string WhatsYourName()
        {
            return this.Name;
        }
    }

    public class Mother: Person
    {
        public Mother(string personName)
        {
            this.Name = personName;
        }
    }

    public class Child : Person
    {
        public Mother MyMother { get; set; }

        public Child(string personName)
        {
            this.Name = personName;
        }

        public string WhoAreYou()
        {
            return string.Format("My name is {0} and my mom is {1}", this.Name, this.MyMother.WhatsYourName());
        }
    }
现在,对象如何相互通信?实现这一点的方法有很多,但都归结为一个简单的概念:参考。 当您创建一个对象(x=new…)时,您正在创建一个新的实例,并且有它的引用

现在,看看这个:

static void Main(string[] args)
{
    Mother mary = new Mother("Mary");
    Child bobby = new Child("Bobby");

    bobby.MyMother = mary;

    Console.WriteLine(bobby.WhoAreYou());

    Console.ReadLine();
}
看看我们什么时候给鲍比的妈妈做手术?我们正在传递其对象引用

请看一下这个代码,我相信它会有帮助

此外,我强烈建议您阅读设计模式。 也许从这里开始:


希望这有帮助。

这取决于
Class1
Class2
是什么,以及它们是否需要耦合。 如果他们做不相关的事情并且不需要相互了解,您可以使用事件来传达他们之间的更改:

class Program
{
    static void Main(string[] args)
    {
        Class1 instance1 = new Class1();
        Class2 instance2 = new Class2();

        instance1.CriticalValueReached += instance2.DoSomething;
        instance2.TimeoutElapsed += instance1.DoSomething;

        // infinite loop
        while (true)
        {
            instance1.UpdateMethod(someValue);
            instance2.UpdateMethod();
        }
    }
}

class Class1
{
    int Property;

    public event Action CriticalValueReached;

    public UpdateMethod(int argument)
    {
        Property += argument;
        if (Property == 3000)
            RaiseCriticalValueReached();
    }

    public void DoSomething()
    {
        // Whatever...
    }

    private void RaiseCriticalValueReached()
    {
        var handler = CriticalValueReached;
        if (handler != null)
            handler();
    }
}

class Class2
{
    public event Action TimeoutElapsed;

    public UpdateMethod()
    {
        if (Time.GetTime() == SomeTime)
            RaiseTimeoutElapsed();
    }

    public void DoSomething()
    {
        // ...
    }

    private void RaiseTimeoutElapsed()
    {
        var handler = TimeoutElapsed;
        if (handler != null)
            handler();
    }
}

如果对象的数量很小,比如在本例中,你不能简单地将一个实例作为参数传递给另一个的update方法吗?如果我将一个实例传递给一个方法,并且如果我修改了该实例,main()中的实例也会改变吗?在你给出的示例中,是的。将更改作为引用数据类型的类。如果您使用的是结构,结果将不同。@当您传递引用类型对象时,实际上并不是在传递实例;您正在传递对实例的引用。调用方的引用和被调用方的引用表示同一个实例。