通过另一个类传递对象 [英] passing object through another class

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

问题描述

假设我声明了一个类.然后创建该类的对象.现在我创建另一个类.现在我想将第一个类对象传递给第二个类,以从第二个类访问ist类的所有属性..
所以该怎么办..

Suppose I declare a class. and I create a object of that class. now I create a another class. now I want to pass 1st class object to the 2nd class for accessing the all properties of the ist class from 2nd class..
so how do I do..

推荐答案

Class1 obj1 = new Class1();
Class2 obj2 = new Class2();
obj2.process(obj1);


obj2现在可以访问obj1的所有公共方法和属性.
注意:我不确定这是您要的内容,因此您可能需要在问题中添加更多详细信息.


obj2 now has access to all public methods and properties of obj1.

Note: I am not sure that this is what you are asking, so you may need to add some more detail to your question.


您可以将第一个类的所有属性声明为public ,或在第二个类中使用反射来访问第一个类的任何非公共属性.
You can either declare all properties of your first class as public, or use reflection in your second class to access any non-public properties of your first class.


您可以通过使用构造函数来使用它.
首先为该对象创建构造函数.

Class1 obj1 =新的Class1();
Class2 obj2 =新的Class2(obj1);
You can use it by using the constructor.
First create the constructor to this object.

Class1 obj1 = new Class1();
Class2 obj2 = new Class2(obj1);


这篇关于通过另一个类传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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