Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 从另一个对象更改对象变量';s法_Java_Object_Methods - Fatal编程技术网

Java 从另一个对象更改对象变量';s法

Java 从另一个对象更改对象变量';s法,java,object,methods,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() {

这可能是非常新手,但我已经尝试了一段时间来寻找答案,但我不能

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不能解析为变量(显然)


如何从另一个方法操作“cat”?

您已经在
start
方法中定义了对象
cat
,并且正在方法
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);
}


}

我会这样做:

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
,让每个四条腿跟随另一个四条腿。也许这更面向对象。

您试图访问其作用域之外的变量,变量车仅存在于方法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");
    }

}
公共类四条腿{
弦乐室;
公共无效移动(字符串i){
this.room=i;
//有点不必要:)
this.room=this.room;
}
}
公共类存储{
private List fourlegs=new ArrayList();
公开作废开始(){
四条腿猫=新的四条腿();
四条腿。添加(猫);
cat.room=“办公室”;
四条腿的狗=新的四条腿();
四条腿。添加(狗);
dog.room=“办公室”;
//狗走到停车场,猫跟着狗
狗。移动(“停车场”);
}
}

我认为这不是一个需要在一节课上解决的任务。从面向对象的角度来看(在Java编程时应该考虑),您至少需要3个类,它们是
Location
FourLeggedAnimal
,以及一个主类,比方说
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;
    }

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

package fourlegs;

public class FourLegsMain {

    public static void main(String[] args) {
        Location office = new Location("office");
        Location carpark = new Location("carpark");

        FourLeggedAnimal cat = new FourLeggedAnimal("cat", office);
        FourLeggedAnimal dog = new FourLeggedAnimal("dog", office);

        System.out.println("The cat is at the " + cat.getCurrentLocation());
        System.out.println("The dog is at the " + dog.getCurrentLocation());

        dog.moveTo(carpark);

        System.out.println("The dog went to the " + dog.getCurrentLocation());
        System.out.println("The cat is still at the " + cat.getCurrentLocation());

        cat.follow(dog);

        System.out.println("The cat followed the dog and is at the "
                + cat.getCurrentLocation()
                + " now");
    }
}
执行它将提供以下输出:

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


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

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

cat
在方法
start()
中声明,这意味着它仅在该方法的范围内可用。将其声明为类属性(如
room
),您可以在类的所有方法中使用它。为什么在
dog
上调用
move
也会影响
cat
?可能了解一些Java中的对象引用会有所帮助。下面是一篇关于StackOverflow的帖子,我已经解释过了:。这会像
公共对象cat?在您的情况下,它应该是
Fourlegs cat=newfourlegs()。如果愿意,您可以将其定义为
public
,但我建议将其设置为私有的create-a-public-getter方法,以访问值
cat
,因为
FourLegs
中的字段从设计角度看没有多大意义(这将导致堆栈溢出,因为现在您正在递归地创建
FourLegs
)的实例。您的编辑已修复了堆栈溢出,但它作为设计仍然没有意义(为什么
FourLegs
会有另一个
FourLegs
…).The
move
不需要访问
cat
,它只需要修改
FourLegs
房间
字段。我同意。这只是试图解决OP面临的问题。这如何接近于回答手头的问题?您的解决方案很好。谢谢!不过,我对fi更感兴趣因为,如果我有另一个“四条腿”:“老虎”?例如,您必须考虑对象的一种存储方式,如数组或列表。也许您可以更改类,使该类仅代表一个四条腿。然后您可以在任何四条腿实例上调用方法move并传递所需字符串,并且您在处调用方法的对象将执行methodAddition:然后您将需要另一个类来管理和维护您的Fourleg对象,因此您将拥有一个FourlegStorage类,在该类中创建Fourleg对象并调用方法them@JosephKwong因此,您可以将tiger的四条腿引用传递给此方法。此解决方案是正确的。@JosephKwong检查deHaar的答案,以获得详细的示例和示例对象中的拆分。如果您只想了解作用域和类,我的解决方案非常适合您。