从另一个具有额外信息的类启动Class

时间:2015-02-11 23:38:22

标签: android class

我想询问是否可以从另一个非活动类

启动一个非活动类

即:

public class nubmerOne {
    String data = "send me please";
    public String getInfo(){

        if (/*something happens*/){
          //start Class numberTwo with some data
        }
    return "";
    }
}


public class numberTwo { //this class is started when condition in class One is true
String dataPlus = "";
    public String dosomething(){
       //started from class numberOne and get data

    dataPlus = data + "";  
    return dataPlus; 
    }
}

我知道在BroadcastReceiver,扩展Activity和Service的类之间可以实现这一点,芽可以在两个类中完成吗?

1 个答案:

答案 0 :(得分:0)

我不确定我理解你的问题,但这看起来就像是经典的Java。

如果条件满足,您的第一个类必须创建第二个类的实例,并在第二个类的构造函数中传递数据。

public class nubmerOne {
    String data = "send me please";
    public String getInfo(){

        if (/*something happens*/){
          //start Class numberTwo with some data
          NumberTwo numberTwo = new NumberTwo (data);
          numberTwo.doSomething();
        }
    return "";
    }
}


public class NumberTwo { //this class is started when condition in class One is true
   private String data;

   public NumberTwo (String data){
      this.data = data;
   }
String dataPlus = "";
    public String dosomething(){
       //started from class numberOne and get data

    dataPlus = data + "";  
    return dataPlus; 
    }
}

希望我帮助过。