C# - 访问另一个类方法 [英] C# - Access another class methods

查看:134
本文介绍了C# - 访问另一个类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

public class Map
{
    class DataHandler
    {
        private int posX, posY;
        // Other attributes

        public int GetX() { return posX; }
        public int GetY() { return posY; }
        // Other functions for handling data (get/set)
    }

    class FileHandler
    {
        // Functions for reading/writing setting files
    }

    DataHandler Data;
    FileHandler Files;

    public Map()
    {
        Data = new DataHandler();
        Files = new FileHandler();
    }
}



现在,当我写一个设置文件时,我需要读取 DataHandler 类的一些变量(非静态)。
例如,假设我想要获取 posX posY 的值,使用 GetX GetY 方法,以便将它们写入文件。如何从文件实例访问地图中的数据实例?
我知道我可以作为参数传递给写入文件的函数,DataHandler类的实例,但它不像我想的那样。在我的想法,它应该读取数据( posX posY 在这种情况下)相同 Map 类。是否有干净的方法来做到这一点?

Now, when I write a setting file I need to read some variables (non-static) of the DataHandler class. For instance, let's suppose I want to get the value of posX and posY, using the GetX and GetY methods, in order to write them in the file. How can I access the 'Data' instance in Map from the 'Files' instance? I know that I could pass as parameter, to the function that writes the file, the instance of the DataHandler class, but it's not like I thought it. In my idea, it should read data (posX and posY in this case) by itself, since it is inside the same Map class. Is there a clean way to do this?

编辑:

我可以传递< c $ c> Map 类添加到 FileHandler 构造函数中,因此它具有访问其父类并因此访问数据的方法,但我理解它不是一个很好的方法来解决问题。
所以,没有问题,改变我的结构,我明白有一些问题,否则我不会有这个问题,但我不知道如何改变它。如果你有任何建议,随时写它。

I could pass the instance of the Map class to the FileHandler constructor, so it has a way to access its parent class and therefore Data, but I understand it's not a good way to solve the problem. So, there is no problem to change my structure, I understand that there is something wrong in it, otherwise I won't have this problem, but I don't know how to change it. If you have any suggestion feel free to write it.

只是为了更好地解释上下文。我有一个地图,它是由各种数据(x,y,高度,宽度,名称,...),一些图像描述它(例如,一个RGB图像的地形高度,我已经计划类来处理图像)和一些设置文件。在其中一个文件中必须写入一些地图数据。
所以,我的想法是有一个父Map类,用三个类处理数据,图像和文件里面,但这个问题出现,所以我认为这不是一个好的结构。

Just to explain better the context. I have a map, which is composed of various data (x, y, height, width, name, ...), some images which describe it (for example, a RGB image for the terrain height, I've already planned the class to handle images) and some setting files. In one of these files there must be written some of the map data. So, my idea was to have a parent Map class, with the three classes for handling data, images and files inside, but this problem came up so I think it's not a good structure.

推荐答案


在我的想法中,它应该读取数据(在这种情况下为posX和posY)地图类。有没有干净的方法来做到这一点?

In my idea, it should read data (posX and posY in this case) by itself, since it is inside the same Map class. Is there a clean way to do this?

不。您将必须在 Map 中的 DataHandler FileHandler / code>类,它们不会自动获知对方的存在。

No. You will have to pass data back and forth between the DataHandler and FileHandler in your Map class, they do not automagically get informed of each other's presence.

如果你可以解释一下应该做什么,因为你的类名似乎有点太一般了。

It would really help if you could explain a bit what everything is supposed to do, as your class names seem to be a bit too generic.

这篇关于C# - 访问另一个类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆