访问上层/包装类的变量

时间:2017-06-17 06:00:52

标签: java android

我是Java的初学者,所以我不知道我想要访问的是上层/包装类。基本上,我有三个班,A,B和C.

CLASS A.java

public class A{

private String aName;

private B objectB;

}

CLASS B.java

public class B{

    private String bName;

    private C objectC;

    }

CLASS C.java

public class C{

        private String cName;


        }

基本上,我有一个A类,它有一个B类对象,后者又有一个C类对象。 我有一个C类对象的实例。如何从对象C的这个instane中访问变量bName和cName?

2 个答案:

答案 0 :(得分:1)

你为什么不使用getter&用于从c的实例访问变量bName的setter方法。你不能直接访问它们,因为它们是私有的。

“CLASS B.java”

public static void generateTooltip(final TableCursor tableCursor,final Table table) {
    tableCursor.addTraverseListener(new TraverseListener() {
        @Override
        public void keyTraversed(TraverseEvent e) {
            switch (e.detail)
            {
            case SWT.TRAVERSE_ARROW_NEXT:
                if (tip == null)
                    break;
                tip.dispose();
                tip = null;
                break;

            case SWT.TRAVERSE_ARROW_PREVIOUS:
                if (tip == null)
                    break;
                tip.dispose();
                tip = null;
                break;  


            }
        }
    });
    tableCursor.addKeyListener(new KeyAdapter() {
          public void keyPressed (KeyEvent e)
          {
              if(((e.stateMask & SWT.ALT) != 0) && e.keyCode == SWT.TAB)
              {                  
                  System.out.println("Key down  ALT +TAB Detact!!");
                  if (tip != null && !tip.isDisposed()) {
                            tip.close();
                            tip.dispose();
                    }
              }
          }

          public void keyReleased(KeyEvent e)
          {
              if(((e.stateMask & SWT.ALT) != 0) && e.keyCode == SWT.TAB)
              {                  
                  System.out.println("Key down  ALT +TAB Detact!!");
                  if (tip != null && !tip.isDisposed()) {
                            tip.close();
                            tip.dispose();
                    }
              }
          }
    });
    final Listener labelListener = new Listener() {
        public void handleEvent(Event event) {
            Text label = (Text) event.widget;
            tip = label.getShell();
            switch (event.type)
            {
                case SWT.MouseDown:
                    label.setFocus();
                    break;
                case SWT.MouseExit:
                     break;
                case SWT.MouseHover:
                {
                    label.setFocus();
                    break;
                }
                case SWT.MouseEnter:
                {
                    label.setFocus();
                    break;
                }
            }

        }
    };

    Listener tableListener = new Listener() {
         Text label = null;
        public void handleEvent(Event event) {
            if (event.keyCode == SWT.F3) 
            {   
                if (tip != null && !tip.isDisposed()) {
                    tip.setFocus();
                    label.setFocus();
                }
            }               

            switch (event.type)
            { 

                case SWT.Dispose:
                {
                    if (tip == null)
                        break;
                    tip.dispose();
                    tip = null;
                    label = null;
                    break;
                }

            case SWT.MouseExit: 
            {               
                System.out.println("Mouse Exit");
                if (tip != null && !tip.isDisposed()) {
                        tip.dispose();
                        break;
                }

            }

            case SWT.MouseHover: {
                // for F1 pressed key focus.                
                TableItem item = tableCursor.getRow();                  
                if(item==null)
                    return;
                    if (tip != null && !tip.isDisposed()) {
                        if (!tip.isFocusControl()) {
                            tip.close();
                            tip.dispose();
                        }
                    }

                    if (tableCursor.getRow().getText(tableCursor.getColumn()).equals("")) {
                        return;
                    }

                    tip = new Shell(tableCursor.getShell(), SWT.ON_TOP| SWT.TOOL  | SWT.RESIZE );
                    tip.addTraverseListener( new TraverseListener() {
                        @Override
                        public void keyTraversed( TraverseEvent event ) {
                            event.doit = true;
                        }
                    } );
                    tip.setLayout(new GridLayout(1, false));
                    tip.layout(true, true);
                    tip.setBackground(SWTResourceManager.getColor(255, 250,205));
                    ScrolledComposite scroll = new ScrolledComposite(tip,SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
                    GridData gd_scrolledComposite = new GridData(SWT.FILL,SWT.FILL, true, true, 1, 1);
                    scroll.setLayoutData(gd_scrolledComposite);
                    scroll.setExpandHorizontal(true);
                    scroll.setExpandVertical(true);
                    scroll.setMinSize(item.getBounds().width,item.getBounds().height);
                    Composite compo = new Composite(scroll, SWT.RESIZE);
                    compo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,true, true, 1, 1));
                    compo.setLayout(new GridLayout(1, false));
                    compo.layout(true);
                    scroll.setContent(compo);
                    label = new Text(compo, SWT.RESIZE | SWT.V_SCROLL | SWT.WRAP | SWT.BORDER | SWT.READ_ONLY);

                    GridData gd_lblNewLabel_2 = new GridData(SWT.FILL,SWT.FILL, true, true, 1, 1);
                    gd_lblNewLabel_2.widthHint = 251;
                    gd_lblNewLabel_2.heightHint = 75;
                    label.setLayoutData(gd_lblNewLabel_2);
                    label.setText(tableCursor.getRow().getText(tableCursor.getColumn()));

                    Label lblNewLabel_1 = new Label(tip, SWT.NONE);
                    GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT,SWT.RIGHT, true, true, 1, 1);
                    lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
                    lblNewLabel_1.setText("Press F3 to focus");
                    lblNewLabel_1.setBackground(SWTResourceManager.getColor(255, 250, 205));

                    Label saveTextLabel = new Label(tip, SWT.NONE);
                    GridData gd_savetext = new GridData(SWT.LEFT,SWT.RIGHT, true, true, 1, 1);
                    saveTextLabel.setLayoutData(gd_savetext);
                    saveTextLabel.setText("Press F3 to Save");
                    saveTextLabel.setVisible(false);
                    saveTextLabel.setBackground(SWTResourceManager.getColor(255, 250, 205));

                    // / Label lisner called
                    label.addListener(SWT.MouseHover, labelListener);
                    label.addListener(SWT.MouseEnter, labelListener);
                    label.addListener(SWT.KeyDown, labelListener);
                    label.addListener(SWT.MouseMove, labelListener);
                    label.setBackground(SWTResourceManager.getColor(255,250, 205));
                    Rectangle rect = item.getBounds();
                    Point pt = tableCursor.toDisplay(rect.x, rect.y);
                    tip.setBounds(pt.x - rect.x, pt.y - rect.y+ tableCursor.getSize().y, rect.width,rect.height);
                    tip.setVisible(true);
                    tip.pack(true);
                    break;
            }
            case SWT.Close:

            case SWT.MouseMove: 
            {               
                if (tip != null && !tip.isDisposed()) {
                tip.dispose();
                }
            }
            }//End switch
                    }
    };
    tableCursor.addListener(SWT.MouseHover, tableListener);
    tableCursor.addListener(SWT.KeyDown, tableListener);
    table.getShell().addListener(SWT.MouseExit, tableListener);         
    table.addListener(SWT.MouseMove, tableListener);
}

您可以直接访问“cName”变量,因为此变量属于您用于获取bName的c的同一个obejct。

答案 1 :(得分:1)

C类访问 B类实例的最简单方法是在C中添加对B的引用:

C类

public class C {

    private String cName;
    private B bObj;

}

然后在B组中使用getter和setter:

B类

public class B {

    private String bName;
    private C cObj;

    public String getBName() {
        return bName;
    }

    public void setBName(String newName) {
        this.bName = newName;
    }
}

然而,请记住,这会导致循环依赖,这通常是代码气味(但在这种情况下可能没问题,具体取决于整体类层次结构)。

更好的选择是在类之间实现Observer模式:

C类

public class C {

    private String cName;
    private List<Observers> observers; //if you only need one instance, then 
                                       //switch out for a single interface reference

    public void requestWrapperName () {
        List<String> names = new ArrayList<>();
        for(observer: observers) {
            names.add(observer.requestName());
        }
        // code to do what you want with wrapper name
        ...
    }

    public void addObserver(Observer observer) {
        observers.add(obsersver);
    }

}

观察者界面

public interface Observer {
    String requestName();
}

B类

public class B implements Observer {

    private String bName;
    private C cObj;

    // Observer method
    public String requestName() { 
        return bName;
    }
}

现在,当您在B类中拥有C的实例时,您可以使用cObj.addObserver(this);将B作为观察者注入C,然后使用方法requestWrapperName();请求B的名称。因此避免双向关联:)