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

时间:2016-11-25 00:38:14

标签: java jfreechart personality-insights

我试图在饼图中显示百分比值,因为我在Personality类中有百分比值,Chart类是我需要显示它的地方。我的代码结构如下:

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(不确定这个类是否必要,但现在是这样)

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;
    }

}

CHART 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;
}

所以总体程序是 - 在使用java值生成图形后,将正常段落转换为json然后解析为java。因此,在这种情况下,要在obj1中显示百分比值Chart class

希望你们明白这个问题并且感谢你的时间:)

2 个答案:

答案 0 :(得分:1)

您必须从Children1类中看到Personality对象。目前看起来obj1只是一个局部变量,其范围仅对该局部方法可见

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

假设您的Personality课程类似于

public Personailty {
   ....
}

然后你将添加,以便它看起来像

public Personailty {
   ....
   private Children1 obj1;

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

答案 1 :(得分:0)

如果您在Personality类中具有名为obj1的属性,则可以使用下面的解决方案。

人格等级应该有:

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);

在你的图表类

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