如何访问调用者对象';在JAVA中通过被调用的方法调用成员

如何访问调用者对象';在JAVA中通过被调用的方法调用成员,java,Java,这是我的密码 class Base { public void fillRandomData() { //code for accessing members for sub classes //here object "b" is calling this method //I want to access members of caller object //As it is Derived class how can I ac

这是我的密码

class Base
{
  public void fillRandomData()
  {
    //code for accessing members for sub classes
        //here object "b" is calling this method
        //I want to access members of caller object
        //As it is Derived class how can I access its member 
  }
}

class Derived extends Base
{
  Map<String, List<Integer>> fields = new HashMap<String,List<Integer>>();
  List<Integer> attribute = new ArrayList<Integer>();

    public Derived() {
        attribute.add(1);
        fields.put("textbox",attribute);
    }
}

class Main
{
     public static void main(String[] argv) {
         Base b = new Base();
       **b.fillRandomData();**
     }
}
类基
{
公共数据()
{
//用于访问子类成员的代码
//这里,对象“b”正在调用此方法
//我想访问调用方对象的成员
//由于它是派生类,如何访问其成员
}
}
类派生的扩展基
{
映射字段=新的HashMap();
列表属性=新的ArrayList();
公共派生(){
属性。添加(1);
fields.put(“textbox”,属性);
}
}
班长
{
公共静态void main(字符串[]argv){
基b=新基();
**b、 填充随机数据()**
}
}
上面的代码解释了我的问题。 我无法访问调用方对象成员 我认为回顾会有帮助,但对我帮助不大


正如在ruby中一样,有一种方法“instance\u variable\u get(instance\u var)”,它允许访问调用方对象的数据成员。

您必须定义方法并将您的方法调用到中,您不能在类块中调用方法

像这样:

class Main
{
void callMethod(){
  Base b = new Base ();
  b.fillRandomData();
}
}

根据我从你的问题中了解到的情况,你想做如下事情:

class Base
{
  public void fillRandomData()
  {
      // do not directly access members. you can use reflect to do this but do not do this.
      // (unless you have a real reason for it)
      //
      // instead, trust on the subclasses to correctly override fillRandomData to fill in the
  }
}

class Derived extends Base
{
    Map<String, List<Integer>> fields = new HashMap<String,List<Integer>>();
    List<Integer> attribute = new ArrayList<Integer>();

    public Derived() {
        attribute.add(1);
        fields.put("textbox",attribute);
    }

    @Override
    public void fillRandomData() {
         // in Main, the b.fillRandomData will run this method.

         // let the Base class set it's part of the data
         super.fillRandomData();

         // then let this class set it's data
         // do stuff with attributes
         // do stuff with fields.
    }
 }

class Main
{
  // you _must_ instantiate a derived object if you want to have a chance at getting it's instance variables
    public static void main(String[] argv) { 
        Base b = new Derived();
        b.fillRandomData();
    }
}
类基
{
公共数据()
{
//不要直接访问成员。您可以使用“反射”来执行此操作,但不能执行此操作。
//(除非你有真正的理由)
//
//相反,信任子类正确重写fillRandomData以填充
}
}
类派生的扩展基
{
映射字段=新的HashMap();
列表属性=新的ArrayList();
公共派生(){
属性。添加(1);
fields.put(“textbox”,属性);
}
@凌驾
公共数据(){
//基本上,b.fillRandomData将运行此方法。
//让基类设置它是数据的一部分
super.fillRandomData();
//然后让这个类设置它的数据
//用属性做东西
//用字段做一些事情。
}
}
班长
{
//如果希望有机会获得派生对象的实例变量,则必须实例化该对象
公共静态void main(字符串[]argv){
基b=新的派生();
b、 填充随机数据();
}
}

不清楚您对调用方对象成员的含义。上面的代码无法编译。也许你可以更详细地描述一下你实际上在做什么?或者发布您正在编写的实际代码,而不是伪代码?啊,好的,您想从基本部分的代码访问实例派生部分的成员吗?这不是建议的做法。但是有办法解决这个问题。@Joeblade调用者对象的成员表示调用该方法的对象。在本例中,b.fillRandomData()“b”是对象,我希望在此方法中访问“fillRandomData()”中“b”的成员。如果我有n个扩展“基类”的子类,@override将不会有帮助,它将增加LOC以及代码的可维护性,正如我希望的那样“fillRandomData()”方法的定义写在一个地方,我可以访问它的子类的成员。这是个坏习惯。它将在父类中嵌入有关子类(其结构)的信息。这意味着无论何时添加子类,都必须更新父类。不是每个人都可以访问父类(例如,如果它位于另一个jar文件中),所以不是每个人都可以这样做。如果你真的必须这样做,那么反射就是你可以做到的方式,但我相信你最终会把自己画进一个角落。出于这个原因,我不会把它列为一个答案。查看委托以获得真正的解决方案。这基本上是问题中的代码示例-看不出这是一个很好的答案。出于完整性和教育目的,重写的方法应该有一个
@override
注释要点。我是一个糟糕的编码员,几乎从未使用过这些,我的编辑为我解决了这类问题。所以手动编写代码让我忘记了。