如何将对象从一个 class 转移到另一个 class - JAVA

How to transfer object from one class to another class - JAVA

我想在饼图中显示百分比值,因为我在 Personalityclass 和 Chart Class 中有百分比值是我需要展示它的地方。我的代码结构如下:

Personality.JAVA

   Tree C = gson.fromJson(profile.toString(), Tree.class);  

                Tree.SubTree t = C.getTree(); 
                ArrayList<Children> mc = t.getChildren();
                Children c1 = mc.get(0);

                ArrayList<Children1> c2 = c1.getChildren();
                Children1 obj1 = c2.get(0);

                private Children1 obj1; //ERROR-ILEGAL START OF EXPRESSION 


                public Children1 getObj1(){
                return this.obj1;
               } 

         /**       ArrayList<child2> c3 = obj1.children;
                child2 obj2 = c3.get(0);

                ArrayList<child3> c4 = obj2.children;
                child3 obj3 = c4.get(0);  **/


                System.out.println(obj1.getPercentage()); //ERROR - IDENTIFIER EXPECTED

Children1.JAVA

   public class Children1 {
     private String category;
     private String id;
     private String name;
     private double percentage;
     private double sampling_error;
     private ArrayList<Children2> children;

    public Double getPercentage() {
     return percentage;
   }
/**
* 
* @param percentage
* The percentage
*/
    public void setPercentage(Double percentage) {
    this.percentage = percentage;
    }
}

Tree.JAVA(不确定这个class是否有必要,但这里有)

public class Tree {
    private SubTree tree;  
    @Override
    public String toString() {
        return "ID: " + id + "\n" + "Name: "+ "\n" + tree+ "\n" ;
    }  
    class SubTree{
    private String id;
    private String name;
    private ArrayList<Children> children; //Gets Arraylist from Class - Children


    public ArrayList<Children> getChildren() {
    return children;
    }

    public void setChildren(ArrayList<Children> children) {
    this.children = children;
    }     
    }

   public SubTree getTree() {
     return tree;
   }

    public void setTree() {
    this.tree = tree;
    }

}

图表CLASS

private PieDataset createDataset(){
   Personality m = new Personality();
  // Personality.class = Personality();
    DefaultPieDataset result = new DefaultPieDataset();
    result.setValue("Value1",m.obj1 ); //attempted to get obj1 from Persoanlity Class
    result.setValue("Linux", 10);
    result.setValue("Mac", 25);
    return result;
}

所以整个程序是 - 将普通段落转换为 json,然后在使用 java 值生成图形后解析为 java。所以在这种情况下,获取百分比值 obj1 以显示在 Chart class 中。

希望你们理解这个问题,感谢您抽出宝贵时间:)

您必须使 Children1 对象在 Personality class 中可见。目前看起来 obj1 只是一个局部变量,它的作用域只对该局部方法可见

如果在 Personality 中,您有一个字段 Children1 obj1,那么这可以由本地方法或构造函数设置。如果 Children1 obj1 的范围是 public 或者最好它有一个 getter 方法,那么你可以从 Chart class 中读取它,假设 Children1 obj1 是在构造函数,即 Personality m = new Personality();

假设您的 Personality class 看起来像

public Personailty {
   ....
}

然后你会添加,这样看起来像

public Personailty {
   ....
   private Children1 obj1;

   public Children1 getObj1(){
      return this.obj1;
  } 
}

如果您在 Personality class.

中有名为 obj1 的属性,则可能的解决方案如下

性格Class应该有:

private Children1 obj1;

public Children1 getObj1(){
    return this.obj1;
} 

.

更改此代码

ArrayList<Children1> c2 = c1.getChildren();
Children1 obj1 = c2.get(0);

ArrayList<Children1> c2 = c1.getChildren();
this.obj1 = c2.get(0);

在你的图表上 class

DefaultPieDataset result = new DefaultPieDataset();
result.setValue("Value1",m.getObj1()); //attempted to get obj1 from Persoanlity Class