在一个类中创建的访问对象到另一个类中 [英] Access object created in one class into another
问题描述
我有一个如下的主类:
public class classB {
public classC getObject (String getstring){
return new classC(getstring);
}
}
classC
有一个结构:
public class classC {
String string;
public classC(String s){
this.string = s;
}
public methodC(int i){
< using'string` variable here>
}
}
现在我有一个 classA
(它当然是 classC $ c>的一个实例)中创建的对象$ c>)。
public classA {
int a = 0.5;
<获取在classB> .methodC(a)中创建的对象;
}
这是需要的,因为变量是在某些操作用户并存储在 classB
中,这将在 classC
的方法中进一步使用。创建一个新对象会将 classB
中的变量设置为不打算使用的null。
假设
品牌
是一个轻量级对象,而运行
是重量级,然后创建一个字段与容器的轻量级对象,并隐藏它是一个好主意。 但 Brand
需要访问它所属的容器,它可以通过映射完成,但我们只需要注入 Run
Brand
,因此它更好地实现 Runable
或用JSR330注释它。并以正常方式通过运行
访问容器。
MainClass {
public static void main(String [] args){
Run r = new Run();
}
}
类运行{
private Container con1 = new Container();
public Run(){
brand cola = new Brand(Coca Cola);
品牌pepsi = new Brand(Pepsi);
//创建容器对象con1并向容器添加品牌。
add(cola);
add(pepsi);
}
public void add(Brand b){
con1.addToList(b);
b.setRun(this);
}
public Container getContainer(){
return con1;
}
}
class Brand {
//在这个类中,我有一个方法需要con1对象
//包含所有品牌和我需要访问的方法
private String name;
private运行;
public Brand(String name){
this.name = name;
}
public void brandMethod(){
if(getRun()。getContainer()。methodExample()){//这里错误。找不到con1。**
System.out.println(方法示例返回true。
}
}
public运行getRun(){
return run;
}
public void setRun(Run run){
this.run = run;
}
}
class Container {
//此类是包含所有品牌品牌的容器列表
private ArrayList< Object> list = new ArrayList< Object>();
public boolean methodExample(){
return false;
}
public void addToList(Object o){
list.add(o);
}
}
I have a primary class as below:
public class classB{
public classC getObject(String getstring){
return new classC(getstring);
}
}
The classC
has a contructor:
public class classC{
String string;
public classC(String s){
this.string = s;
}
public methodC(int i){
<using the `string` variable here>
}
}
Now I've a classA
which will be using the object created in classB
(which is of course, an instance of classC
).
public classA{
int a = 0.5;
<Get the object that was created in classB>.methodC(a);
}
This is needed as a variable is created on some actions from the user and stored in classB
and this would be further used in classC
's methods. Creating a new object will render my variable in classB
set to null which isn't intended.
How can I achieve this?
Assume the Brand
is a lightweight objects and Run
is heavyweight then creating a field with the container for the lightweight objects and hiding it is a good idea.
But the Brand
needs access the container it belongs to it could be done with the mapping but we are simply inject the Run
to the Brand
so it's better implement the Runable
or annotate it with JSR330. And accessing the container through the Run
in the normal way.
class MainClass {
public static void main(String[] args) {
Run r = new Run();
}
}
class Run {
private Container con1 = new Container();
public Run() {
Brand cola = new Brand("Coca Cola");
Brand pepsi = new Brand("Pepsi");
// Creates the container object "con1" and adds brands to container.
add(cola);
add(pepsi);
}
public void add(Brand b){
con1.addToList(b);
b.setRun(this);
}
public Container getContainer() {
return con1;
}
}
class Brand {
// In this class I have a method which needs to accsess the con1 object
// containing all the brands and I need to access the method
private String name;
private Run run;
public Brand(String name){
this.name = name;
}
public void brandMethod() {
if(getRun().getContainer().methodExample()) { // Error here. Can't find "con1".**
System.out.println("Method example returned true.");
}
}
public Run getRun() {
return run;
}
public void setRun(Run run) {
this.run = run;
}
}
class Container {
// This class is a container-list containing all brands brands
private ArrayList<Object> list = new ArrayList<Object>();
public boolean methodExample(){
return false;
}
public void addToList(Object o) {
list.add(o);
}
}
这篇关于在一个类中创建的访问对象到另一个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!