从另一个对象的方法更改对象变量

时间:2018-08-23 11:26:40

标签: java object methods

这可能是一个新手,但是我已经尝试了一段时间了,但我没有找到答案。

package playground.space;

public class Fourlegs {
    String room;

    public static void main(String[] args) {
        Fourlegs program = new Fourlegs();

        program.start();
    }

    public void start() {
        Fourlegs cat = new Fourlegs();
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        dog.room = "office";

        //dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
    }

    public void move(String i) {
        this.room = i;

        //cat cannot be resolved to a variable
        cat.room = this.room; //the cat's room will be the same as the dog's room.
        System.out.println("the cat is in the " + cat.room);
    }
}

我得到一个错误:cat无法解析为变量。(显然)。

如何通过另一种方法操纵“猫”?

5 个答案:

答案 0 :(得分:1)

您正在尝试访问变量范围之外的变量,变量cart仅存在于start方法中。
您必须将要使用的对象传递给此方法:

public void move(String i, Fourlegs fourleg) {
  fourleg.room = this.room
}

现在您可以在Fourlegs的任何实例上调用方法

编辑: 新方法:

public class Fourlegs {
    String room;

    public void move(String i) {
        this.room = i;
        //kind of unnecesary:)
        this.room = this.room;
    }
}


public class FourlegStorage {
 private List<Fourleg> fourlegs = new ArrayList<>();
    public void start() {
        Fourlegs cat = new Fourlegs();
        fourlegs.add(cat);
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        fourlegs.add(dog);
        dog.room = "office";

        //dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
    }

}

答案 1 :(得分:1)

我认为这不是单个课程要解决的任务。从面向对象的角度(使用Java进行编程时应考虑),您至少需要3个类,分别为LocationFourLeggedAnimal和一个主类,例如{{1 }}:

在动物的名字和位置上应该看起来像这样:

FourLeggedMain

该位置只需要一个名称:

package fourlegs;

public class FourLeggedAnimal {

    protected String name;
    protected Location location;

    public FourLeggedAnimal(String name, Location room) {
        this.name = name;
        this.location = room;
    }

    public Location getLocation() {
        return location;
    }

    public void follow(FourLeggedAnimal animal) {
        this.location = animal.getLocation();
    }

    public void moveTo(Location room) {
        this.location = room;
    }

    public String getCurrentLocation() {
        return location.getName();
    }
}

主体执行包括其他对象的逻辑:

package fourlegs;

public class Location {

    private String name;

    public Location(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

执行它将提供以下输出:

  

猫在办公室
  狗在办公室
  那只狗去了停车场
  猫还在办公室
  猫跟着狗走,现在在停车场

答案 2 :(得分:0)

您已经在cat方法中定义了对象start,并且正在方法move中使用它。

在方法内部定义变量时,其范围仅限于该方法。要在同一个类中的其他方法中使用它,则应在类级别定义变量,并且错误应消失。

在类级别定义变量以解决错误

package playground.space;

public class Fourlegs {
String room;
Fourlegs cat; // global variable here

public static void main(String[] args) {
    Fourlegs program = new Fourlegs();

    program.start();
}

public void start() {
    Fourlegs cat = new Fourlegs();
    cat.room = "office";
    Fourlegs dog = new Fourlegs();
    dog.room = "office";
    dog.cat = cat; // global variable set here

    //dog moves to the carpark, and the cat follows the dog
    dog.cat = new Fourlegs();
    dog.move("carpark");
}

public void move(String i) {
    this.room = i;

    cat.room = this.room; //the cat's room will be the same as the dog's room.
    System.out.println("the cat is in the " + cat.room);
}


}

答案 3 :(得分:0)

我会这样:

package playground.space;

public class Fourlegs {

    String room;

    public static void main(String[] args) {
        Fourlegs program = new Fourlegs();

        program.start();
    }

    public void start() {
        Fourlegs cat = new Fourlegs();
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        dog.room = "office";

        //dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
        cat.follow(dog);
        System.out.println("the cat is in the " + cat.room);
    }

    public void follow(Fourlegs other) {
        room = other.room;
    }

    public void move(String newRoom) {
        this.room = newRoom;
    }
}

我添加了一种方法follow,让每个Fourleg都跟随另一个Fourleg。也许这是面向对象的。

答案 4 :(得分:0)

这是我的尝试。我对发布的代码进行了一些更改:

public class Fourlegs {

    String room;
    private String id; // to identify a dog, cat, etc.

    public static void main(String[] args) {
        Fourlegs program = new Fourlegs();
        program.start();
    }

    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return this.id;
    }   

    public void start() {
        Fourlegs cat = new Fourlegs();
        cat.setId("Cat-Fluffy");
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        dog.setId("Dog-Toby");
        dog.room = "office";
        System.out.println(dog.getId() + " is in the " + dog.room);
        System.out.println(cat.getId() + " is in the " + cat.room);
        // dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
        // the cat's follows the dog.
        cat.move(dog.room);
    }

    public void move(String i) {
        this.room = i;
        System.out.println(this.getId() + " moved " + this.room);
    }
}

输出:

Dog-Toby is in the office
Cat-Fluffy is in the office
Dog-Toby moved to carpark
Cat-Fluffy moved to carpark
相关问题