Java,将字符串从一个类传递到另一个类

3 java

在下面的代码我做错了.对不起,如果这有点基础.如果它只在一个类中,那么我的工作正常,但是当我在下面的代码中打破类时,我的工作正常:

class Apples{
    public static void main(String[] args){

        String bucket = "green"; //instance variable

        Apples appleOne = new Apples(); //create new object (appleOne) from Apples class

        System.out.println("Paint apple one: " + appleOne.paint(bucket));
        System.out.print("bucket still filled with:" + bucket);

        }//end main

    }//end class

class ApplesTestDrive{

    public String paint(String bucket){

        bucket = "blue"; //local variable
        return bucket;

        }//end method

    }//end class
Run Code Online (Sandbox Code Playgroud)

错误信息:

location:class Apples
cannot find symbol
pointing to >> appleOne.paint(bucket)
Run Code Online (Sandbox Code Playgroud)

任何提示?

Bal*_*usC 5

您需要创建一个实例ApplesTestDrive,而不是Apples.方法就在那里.

所以,而不是

Apples appleOne = new Apples();
Run Code Online (Sandbox Code Playgroud)

ApplesTestDrive appleOne = new ApplesTestDrive();
Run Code Online (Sandbox Code Playgroud)

这与通过引用传递无关(所以我从你的问题中删除了标记).这只是程序员错误(实际上所有编译错误都是).