一个方法创建一个对象,我从另一个对象调用该方法

时间:2021-09-10 21:32:34

If a method creates an object and I call the method from an other object, will the last object have access to the first object's properties and methods?

如果方法创建了一个对象并且我从另一个对象调用该方法,那么最后一个对象是否可以访问第一个对象的属性和方法?

5 个解决方案

#1


There's some extraneous information there that may be confusing you.

那里有一些无关的信息可能让你感到困惑。

The method and the object (in this case) are disconnected from one another. So the question becomes, are you storing the created object in a scope that the second object has access to?

该方法和对象(在这种情况下)彼此断开连接。那么问题就变成了,您是否将创建的对象存储在第二个对象可以访问的范围内?

#2


In Router, in a method, in an included file is the controller code. In the model and in the view, I need access to the Controller's properties and methods. In Router, in an other method, I want to return a controller object to the model and the view. What now??

在路由器中,在一个方法中,在包含的文件中是控制器代码。在模型和视图中,我需要访问Controller的属性和方法。在Router中,在另一种方法中,我想将控制器对象返回到模型和视图。现在怎么办??

If I understand the question properly, you're a bit confused about MVC. Router class is a cake internal class and should never never ever never never absolutely never ever be changed. And those "never ever" are not even copy-pasted, they are really typed.

如果我理解这个问题,你对MVC有点困惑。路由器类是一个蛋糕内部类,绝不应该永远不会永远不会被改变。那些“永远不会”甚至没有复制粘贴,它们都是真正打字的。

Second, model classes don't even know anything called controller. Controller uses models, not the other way around. If your model needs something from a controller, pass it on as a parameter. Anything beyond that is just bad design.

其次,模型类甚至不知道任何称为控制器的东西。控制器使用模型,而不是相反。如果您的模型需要控制器中的某些内容,请将其作为参数传递。除此之外的任何事情都只是糟糕的设计。

Also, calling controller actions from a view is possible, but strongly discouraged. Controller is the one to prepare all the data for a view, therefore view has no need to access the controller (there are exceptions to this, out of scope of this question).

此外,可以从视图中调用控制器操作,但强烈建议不要这样做。控制器是为视图准备所有数据的控制器,因此视图不需要访问控制器(除此之外,超出了本问题的范围)。

I recommend you read a bit about MVC, cake's typical request, and at least go through the basic blog tutorial.

我建议你阅读一些关于MVC,蛋糕的典型请求,至少要阅读基本的博客教程。

#3


If it is returned/stored somewhere, public fields and methods will be accessible.

如果将其返回/存储在某处,则可以访问公共字段和方法。

#4


Only if the method keeps a reference to the object that it creates.

仅当方法保持对其创建的对象的引用时。

#5


Edit: In light of the change of tags, this answer is no longer relevant. I've left it to preserve the comments...

编辑:根据标签的变化,这个答案不再相关。我留下它来保留评论......

Original Answer:

Like this?:

public MyObject CreateObject()
{
   return new MyObject() { FirstProperty = "Hello World" };
}

public Main()
{
    MyObject n = CreateObject();
    Console.WriteLine(n.FirstProperty);
}

Or this?:

class Program
{
    MyObject _myObject;

    public void CreateObject()
    {
        _myObject = new MyObject() { FirstProperty = "Hello World" };
    }

    public Main()
    {
        Console.WriteLine(_myObject.FirstProperty);
    }
}

In either of these two cases, sure you can access properties of your object. If this is not what you meant, I'm not sure exactly how to answer your question and you'll need to clarify.

在这两种情况中的任何一种情况下,确保您可以访问对象的属性。如果这不是你的意思,我不确定如何回答你的问题,你需要澄清。

#1


There's some extraneous information there that may be confusing you.

那里有一些无关的信息可能让你感到困惑。

The method and the object (in this case) are disconnected from one another. So the question becomes, are you storing the created object in a scope that the second object has access to?

该方法和对象(在这种情况下)彼此断开连接。那么问题就变成了,您是否将创建的对象存储在第二个对象可以访问的范围内?

#2


In Router, in a method, in an included file is the controller code. In the model and in the view, I need access to the Controller's properties and methods. In Router, in an other method, I want to return a controller object to the model and the view. What now??

在路由器中,在一个方法中,在包含的文件中是控制器代码。在模型和视图中,我需要访问Controller的属性和方法。在Router中,在另一种方法中,我想将控制器对象返回到模型和视图。现在怎么办??

If I understand the question properly, you're a bit confused about MVC. Router class is a cake internal class and should never never ever never never absolutely never ever be changed. And those "never ever" are not even copy-pasted, they are really typed.

如果我理解这个问题,你对MVC有点困惑。路由器类是一个蛋糕内部类,绝不应该永远不会永远不会被改变。那些“永远不会”甚至没有复制粘贴,它们都是真正打字的。

Second, model classes don't even know anything called controller. Controller uses models, not the other way around. If your model needs something from a controller, pass it on as a parameter. Anything beyond that is just bad design.

其次,模型类甚至不知道任何称为控制器的东西。控制器使用模型,而不是相反。如果您的模型需要控制器中的某些内容,请将其作为参数传递。除此之外的任何事情都只是糟糕的设计。

Also, calling controller actions from a view is possible, but strongly discouraged. Controller is the one to prepare all the data for a view, therefore view has no need to access the controller (there are exceptions to this, out of scope of this question).

此外,可以从视图中调用控制器操作,但强烈建议不要这样做。控制器是为视图准备所有数据的控制器,因此视图不需要访问控制器(除此之外,超出了本问题的范围)。

I recommend you read a bit about MVC, cake's typical request, and at least go through the basic blog tutorial.

我建议你阅读一些关于MVC,蛋糕的典型请求,至少要阅读基本的博客教程。

#3


If it is returned/stored somewhere, public fields and methods will be accessible.

如果将其返回/存储在某处,则可以访问公共字段和方法。

#4


Only if the method keeps a reference to the object that it creates.

仅当方法保持对其创建的对象的引用时。

#5


Edit: In light of the change of tags, this answer is no longer relevant. I've left it to preserve the comments...

编辑:根据标签的变化,这个答案不再相关。我留下它来保留评论......

Original Answer:

Like this?:

public MyObject CreateObject()
{
   return new MyObject() { FirstProperty = "Hello World" };
}

public Main()
{
    MyObject n = CreateObject();
    Console.WriteLine(n.FirstProperty);
}

Or this?:

class Program
{
    MyObject _myObject;

    public void CreateObject()
    {
        _myObject = new MyObject() { FirstProperty = "Hello World" };
    }

    public Main()
    {
        Console.WriteLine(_myObject.FirstProperty);
    }
}

In either of these two cases, sure you can access properties of your object. If this is not what you meant, I'm not sure exactly how to answer your question and you'll need to clarify.

在这两种情况中的任何一种情况下,确保您可以访问对象的属性。如果这不是你的意思,我不确定如何回答你的问题,你需要澄清。