简体   繁体   English

从另一个项目中的另一个类获取变量

[英]Get variable from another class within another project

I have 3 project. 我有3个专案。 (and Ignore all Connection to database and Registry to Computer). (并忽略所有与数据库的连接和注册表与计算机的连接)。 Project A = API (java library), Project B = Java Class (Server) , Project C = Java Class (Client) . 项目A = API(Java库),项目B = Java类(服务器),项目C = Java类(客户端)。

in Project AI have 1 java class for entity , lets say (entity.java) Project AI中的entity有1个Java类,可以说(entity.java)

private Boolean data;
private String name;    
public void Data_set(Boolean data){
this.data = data;
}
public void Name_set(String name){
this.name = name
}        
public Boolean Data_get() {
return data;
}
public String Name_get() {
return name;
}

in Project A, I have 1 Interface , lets say (interface.java) 在项目A中,我有1个Interface,可以说(interface.java)

public void method();

in Project B 1 java class which implements interface.java, lets give it a name (server.java) which import all java & interface class on project A. 在项目B中,一个实现interface.java的java类,让我们给它起一个名字(server.java),该名称将导入项目A上的所有java&接口类。

public void method() {
entity Entity = new entity();
Entity.Data_set(true);
Entity.Name_set("Someone");
}

in Project C 1 java main class, lets give it a name (main.java) which import all java & interface class on project A. 在Project C 1 Java主类中,为其命名(main.java),该名称将导入项目A上的所有Java和接口类。

server Server = new server();
Server.method();
entity Entity = new entity();
System.out.print("Boolean = "Entity.Data_is();
System.out.print("Name = "Entity.Name_get();

WHAT I WANT. 我想要的是。 After I run "main.java" It should be below 在我运行“ main.java”之后,它应该在下面

Boolean = true
Name = someone

but it show difference value it show below. 但它显示的差值如下所示。

Boolean = false
Name = null

but if i set entity on "main.java" not in server.java the result there are no problem. 但是,如果我在“ main.java”上而不是在server.java中设置实体,则结果没有问题。

NOTE for connection database and connection between client-server there are no problem. 注意对于连接数据库和客户端-服务器之间的连接没有问题。

Is there any solution for this. 有什么解决办法吗?

Thanks. 谢谢。

It sounds as if you create an Entity in the Server class: 听起来好像您在Server类中创建了Entity

class Server implements Interface {
    private Entity entity;

    public void method() {
        entity = new Entity();
        entity.setName(...);
        entity.setData(...);
    }
}

But rather than print the details of that entity, you create a new one and print the details to that one (which you never assign it any details): 但是,您无需打印该实体的详细信息,而是创建一个新实体并将其打印到该实体(您永远不会为其分配任何详细信息):

class Main {
    public static void main(String[] args) {
        Server server = new Server();
        server.method();

        Entity entity = new Entity();
        //print entity
    }
}

What you should be doing is grabbing the entity from the server: 您应该做的是从服务器获取实体:

  class Main {
    public static void main(String[] args) {
        Server server = new Server();
        server.method();

        Entity entity = server.getEntity();
        //print entity
    }
}

class Server {
    private Entity entity;

    public void method() {
        entity = new Entity();
        entity.setName(...);
        entity.setData(...);
    }

    public Entity getEntity() {
        return entity;
    }
}

As you may have noticed, I used a different style of capitalization than you used. 您可能已经注意到,我使用的大写字母格式与您使用的大写字母格式不同。 The capitalization style I used is the coding convention people typically use when writing Java. 我使用的大写形式是人们在编写Java时通常使用的编码约定。 Language have their own styles, it's best to stay consistent with the language's choice of style . 语言有自己的风格,最好与语言选择的风格保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM