Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从类更改活动中的对象_Java_Android - Fatal编程技术网

Java 从类更改活动中的对象

Java 从类更改活动中的对象,java,android,Java,Android,我有一个活动,它有两个不同类的对象 我有一个CanvasLayout类,需要在特定条件下更改活动的对象 我该怎么做 CurrentlyCountingActivity public class CurrentlyCountingActivity extends AppCompatActivity { CanvasLayout canvasLayout; //I want to call one of the carCommands and //pathCoordin

我有一个
活动
,它有两个不同类的对象

我有一个CanvasLayout类,需要在特定条件下更改活动的对象

我该怎么做

CurrentlyCountingActivity

public class CurrentlyCountingActivity extends AppCompatActivity {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates
}
public class CurrentlyCountingActivity extends AppCompatActivity implements OnCanvasListener  {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates

  @Override
public void doSomething(){

// implement your logic here.
}

}
画布布局类

public class CanvasLayout extends SurfaceView implements Runnable {

    ... initializations here

    @Override
    public void run() {

        prepBrush();

        while(canDraw) {
            ..other irrelevant code

            motionPoint();

            ...more code


            if((pointX == INITIAL_X) && (pointY == INITIAL_Y)) {
                canDraw = false;
                //I would like to have something like 
                //carCommands.setAbsoluteSpeed(0, 0); here
            }
        }
    }


   private void motionPoint() {
       //I want to call pathCoordinates of Activity in CanvasLayout
       pathCoordinates.trackPathCoordinates();
       pointX = pathCoordinates.getX();
       pointY = pathCoordinates.getY();

   }

}
使用

SurfaceView

private OnCanvasListener onCanvasListener;
public class CanvasLayout extends SurfaceView implements Runnable {

  public interface OnCanvasListener{

    public void doSomething();
   }

   public CanvasLayout(Context this){
onCanvasListener = (OnCanvasListener)this;
   }


 @Override
    public void run() {

        prepBrush();

        while(canDraw) {
            ..other irrelevant code

            motionPoint();

            ...more code


            if((pointX == INITIAL_X) && (pointY == INITIAL_Y)) {
                canDraw = false;
                //I would like to have something like 
                //carCommands.setAbsoluteSpeed(0, 0); here
                 onCanvasListener.doSomething(); // invoke interface method

            }
        }
    }

}
在活动中实现接口

public class CurrentlyCountingActivity extends AppCompatActivity {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates
}
public class CurrentlyCountingActivity extends AppCompatActivity implements OnCanvasListener  {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates

  @Override
public void doSomething(){

// implement your logic here.
}

}

对不起,有错。希望这会有所帮助。

有几种方法可以提供对CarCommands对象的访问:

  • 在CanvasLayout中创建一个构造函数,该构造函数接受CarCommands对象

  • 在CanvasLayout中创建一个setter方法,该方法接受CarCommands对象

  • 在CanvasLayout中创建CarCommands对象

  • 向允许访问CarCommands对象的activity类添加方法,并向接受CurrentlyCountingActivity的CanvasLayout添加构造函数

  • 我相信还有其他选择。选择哪一个在很大程度上取决于代码设计中的其他因素。我学的是三。活动真的需要CAR命令吗


    我建议您继续学习面向对象原则。只有经验才能帮助您判断此类设计决策的权衡。

    您可以将对象设置为静态对象,以便您可以直接从类中访问它,并可以更改值,由于它是静态的,因此在应用程序中只有一个内存分配,所以不会有多个实例使用接口是go@IchigoKurosaki静态意味着活动类的每个实例共享同一个对象。在大多数情况下,这是不正确的,因为您只是为了方便访问。但我需要两种情况下的同一组carCommands,因此在CanvasLayout中创建另一个carCommands对象是没有意义的。@Kek我没有说创建另一个carCommands对象。我说的是在画布布局中创建它。活动是否确实需要访问CarCommands对象?如果是,怎么会这样?是的,是的。它实际上负责发出大部分汽车指令,因为它应该是在运行汽车并检测障碍物,而画布布局只是用来绘制汽车的路径,当汽车回到其原始位置时,告诉汽车停车。@Kek这很有意义。那么#1或#4都是很好的解决方案。