简体   繁体   English

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

[英]How to transfer object from one class to another class - JAVA

I am trying to display the value of percentage in a pie chart, for that I have the percentage value in the Personality class and the Chart Class is where I need to show it. 我试图在饼图中显示百分比值,因为我在“ Personality类中具有百分比值,而“ Chart类”是我需要显示它的位置。 My code structure is below: 我的代码结构如下:

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 儿童1.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(Not sure if this class is necessary but here it is) 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;
}

So overall program is - get normal paragraph convert to json then to parse to java after using java values produce graph. 所以总体程序是-使用Java值生成图形后,将普通段转换为json,然后解析为java。 So in this case get percentage value obj1 to show in Chart class . 因此,在这种情况下,将百分比值obj1显示在Chart class

Hope you guys understood the question and thankyou for your time :) 希望你们能理解这个问题,并感谢您的时间:)

You have to make the Children1 object visible from the Personality class. 您必须使Personality类中的Children1对象可见。 At the moment it looks like obj1 is just a local variable and its scope is only visible to that local method 目前看来obj1只是一个局部变量,其作用域仅对该局部方法可见

If in Personality, you have a field Children1 obj1 then this could be set by the local method or constructor. 如果在“个性”中,您有一个Children1 obj1字段,则可以通过本地方法或构造函数进行设置。 If the scope of Children1 obj1 was public OR preferably it had a getter method, then you could read it from the Chart class assuming that the Children1 obj1 is created in the constructor ie Personality m = new Personality(); 如果Children1 obj1的范围是公共的,或者最好具有getter方法,则可以从Chart class读取它,并假设在构造函数中创建了Children1 obj1 ,即Personality m = new Personality();

Assuming that your Personality class looks something like 假设您的Personality类看起来像

public Personailty {
   ....
}

then you will to add, so that it looks like 然后您将添加,使其看起来像

public Personailty {
   ....
   private Children1 obj1;

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

Possible solution below if you have attribute called obj1 in Personality class. 如果您在Personality类中具有名为obj1的属性,则可能的解决方法如下。

Personality Class should have: 个性班应具备:

private Children1 obj1;

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

.

Change this your Code 更改您的代码

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

to

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

And on your Chart class 在您的Chart课上

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

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

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