java如何调用这个类的public数据

public class DrawListener extends MouseAdapter {

private Graphics2D graphics2d;// 绘图区域的Graphic2D对象,用于绘制给定的图形
private ToolsPanel toolsPanel;// 图形选择工具条
private DrawListener get;
private int x1, x2, y1, y2;//记录坐标
public int id=1;//记录每个节点的ID
public int NumOfEdge=0 ;//记录边的数量
public int [][] edge = new int[20][20];//记录节点之间是否连通
boolean temp1=false,temp2=false;//判断直线起始点是否在圆内
private int temp3=0,temp4=0;
public String[] value= new String[20];
public  int[] x = new int[20];   //储存节点坐标
public  int[] y = new int[20]; //储存节点坐标
public DrawListener(Graphics2D graphics2d, ToolsPanel toolsPanel
                    ) {
    this.graphics2d = graphics2d;
    this.toolsPanel = toolsPanel;
            }


            我想再另外的类里调用这里的id啊。edge啊。改在另外的类里。求示范代码

DrawListener dl = new DrawListener();
dl.id = xxx;

或者把这些变量定义成static的(所有的对象实例共享)
那么直接 DrawListener.id = xxx;

你可以在另一个类中实例化这个对象,通过对象名.id 对象名.eg

或者你把这二个值设为静态的statis 通过类名. id

一个类的public类型的成员变量,可以通过该类的实例.属性名称直接被访问的。所以需要创建一个这个类的实例。

先要声明这个类的实例化对象,在通过这个实例化对象调用id。
DrawListener dl = new DrawListener();
dl.id = xxx;

一般我们不提倡类里面的属性采用public修饰,这样违背了面向对象的封装特性,正确的做法是用private修饰,提供id的getter/setter方法供外部调用。

public class DrawListener extends MouseAdapter 这个变成public static class DrawListener extends MouseAdapter ,用的时候直接DrawListener.id就行了

1、通过创建类对象引用
DrawListener dl = new DrawListener();
dl.id = xxx;

2、成员变量变成类成员变量,把这些变量定义成static的(所有的对象实例共享)
那么直接 DrawListener.id = xxx;
3、创建get方法,通过get方法获取