如何从另一个类访问一个类的实例?

时间:2018-09-16 19:06:15

标签: c#

我有以下代码:

public class UnitManager
{
    public string Name { get; set; }
    public string Firstname { get; set; }

    public UnitManager(string name, string firstname)
    {
        this.Name = name;
        this.Firstname = firstname;
    }
}

class Other
{
}

class Program
{
    static void Main(string[] args)
    {
        var player1 = new UnitManager("p1Name", "p1FirstName");

        var player2 = new UnitManager("p2Name", "p2FirstName");
    }
}

因此,我在Program类中创建了 2 个对象。 我想做的是从Other类内部访问这些实例以//做事情。

例如,访问玩家名称,在其名称中添加一个标题,然后评估玩家2并添加另一个标题。

请记住,这只是一个例子,我实际上并没有计划使用它,我只是在试图理解这个概念。

4 个答案:

答案 0 :(得分:0)

我认为您可以在UnitManager类中编写Other属性,设置UnitManager

public class Other
{
    public UnitManager manager1{ get; set; }
}

或通过UnitManager实例作为参数编写方法。

public class Other
{
    public void SetTitle(UnitManager m1) {
        // do your logic
    }
}

答案 1 :(得分:0)

我认为您可能想要这样的东西:

public class UnitManager
{
public string Name { get; set; }
public string Firstname { get; set; }

public UnitManager(string name, string firstname)
{
    this.Name = name;
    this.Firstname = firstname;
}

}


public class Other
{
     public void doSomething(UnitManager myUnit) {
             //do something with each unit manager
     }
}

public class Program
{
    static void Main(string[] args)
{
    //create an object of type UnitManager and place it into variable player1
    var player1 = new UnitManager("p1Name", "p1FirstName");
    //create an object of type UnitManager and place it into variable player2
    var player2 = new UnitManager("p2Name", "p2FirstName");

    //create an instance of the class Other
    Other ot = new Other();

    //call the method within the instantiated class ot (of type Other) and
    //pass it the instance of the object UnitManager with a name
    //of player1
    result1 = ot.doSomething(player1);
    result2 = ot.doSomething(player2);
}
}

答案 2 :(得分:0)

谢谢大家,我终于明白了:)

在您的帮助下,这就是我的工作方式:

public class UnitManager
{
    public string Name { get; set; }
    public string Firstname { get; set; }

    public UnitManager(string name, string firstname)
    {
        this.Name = name;
        this.Firstname = firstname;
    }
}

class Other
{
    public static void AddTitle(UnitManager myUnit)
    {
        var titlePlusFullName = ("The Legendary" + " " + myUnit.Name + " " + myUnit.Firstname);
        myUnit.Name = titlePlusFullName;
    }
}

class Program
{
    static void Main(string[] args)
    {

        var player1 = new UnitManager("john", "smith");

        var player2 = new UnitManager("jen", "doe");

        Other oT = new Other();

        Other.AddTitle(player1);
        Console.WriteLine("Player 1 name: " + player1.Name);
    }
}

显示全名和标题。

答案 3 :(得分:0)

如果您完全是 2个实例(玩家),为什么不在UnitManager创建呢?

// sealed: since we have 2 instances only we don't want to
// inherit (and create) derived classes
public sealed class UnitManager
{
    public string Name { get; set; }
    public string Firstname { get; set; }

    // private: since we have 2 intances only we don't want to expose the constructor
    private UnitManager(string name, string firstname)
    {
        this.Name = name;
        this.Firstname = firstname;
    }

    // Think over renaming these fields: say, Player and Computer
    public static readonly UnitManager Player1 = new UnitManager("p1Name", "p1FirstName");
    public static readonly UnitManager Player2 = new UnitManager("p2Name", "p2FirstName");
}

然后将其命名为UnitManager.Player1UnitManager.Player2),例如

class Program
{
    static void Main(string[] args)
    {
        UnitManager.Player1.Name = "First Name";
        UnitManager.Player2.Name = "Second Name";

        ...
    }
}

甚至(借助静态导入的帮助):

using static MyNamepace.UnitManager;

...

class Other
{
    public void DoSomething() 
    {
        // We don't have now put it as UnitManager.Player1 
        string name1 = Player1.Name;
        ...  
    }
}