如何将数据/值从一个类传递到另一个类

时间:2013-12-12 14:02:24

标签: java

我是Java的新手,我正在努力将变量/数组从一个类传递到另一个类,其中gui类应该从逻辑类中打印数据。

这是我的代码:

Gui上课:

package testClassesGUI;

public class Gui {

    public static void main(String []args){

        Logic logicObject = new Logic();
        logicObject.message(s);//here is an error

        Logic logicObject2 = new Logic();
        logicObject2.someArray(String[] someArray);//here is an error

        System.out.print(logicObject.message());//here is an error
        System.out.print(logicObject2.someArray());//here is an error

            Logic logicObject3 = new Logic();
        logicObject3.currentPoints((DominoPlayer[] player);

            System.out.print(p.getName() + " has " + p.getPoints() + " Points");

    }
}

Logic上课:

package testClassesGUI;

public class Logic {

    String s = "hello";
    public String message(String s){

        return s;
    }

    String[] someArray = { "Great", "World" };

    public String[] message2(String[] someArray){

        return someArray;

    }

   //imagine i have imported this and I need to pass to the gui class
   public void currentPoints(CardPlayer[] player) {
    for (CardPlayer p : player) {
        p.getName();  //pass this to the gui class
                   p.getPoints();//pass this to the gui class
        }
    }
    //return p.getName() and p.getPoints()
}

4 个答案:

答案 0 :(得分:0)

您在主

中有未定义的变量s

主要代替s,您可以编写常量字符串"I like stackoverflow"

答案 1 :(得分:0)

String s = "hello";
public String message(String s){

    return s;
}

此方法期望您将String传递给它,然后返回相同的String。

尝试:

public String message(){

    return s;
}

答案 2 :(得分:0)

在逻辑类类中编写以下内容:

 public String message(String s){
this.s = s;
    return s;
}

我不确定,但我认为这是你的问题。 (如果这不起作用,只需交换this.s和s)

答案 3 :(得分:0)

普通的getter不接受任何参数

试试这个

String s = "hello";
public String message(){
    return s;
}

String[] someArray = { "Great", "World" };

public String[] message2(){
    return someArray;
}

然后在main

Logic logicObject = new Logic();
System.out.print(logicObject.message());

String[] array = logicObject.message2();
for (String s: array){
    System.out.print(s + " ");
}

由于message2()会返回String[],因此您希望在调用它时将其设置为等于String[] array = logicObject.message2();

当你尝试这样做时

System.out.print(logicObject2.someArray());

您收到错误,因为没有方法someArray()。有一个数组someArray[]所以您可以像logicObject.someArray一样调用它,而不是()


所有错误列表

  • logicObject.message(s); - 声明没有s可以传递
  • logicObject2.someArray(String[] someArray); - 您的Logic类中没有someArray()方法
  • System.out.print(logicObject.message()); - 您message()课程中的Logic会发生争执。
  • System.out.print(logicObject2.someArray()); - someArray()课程中没有Logic方法。

修改:常规数据Accessor(get)Mutator(set)方法

public class Logic {
    private String s = "hello";
    private String[] someArray = { "Great", "World" };

    // getter method
    public String getS(){
        return s;
    }

    // getter method
    public String[] getSomeArray(){
        return someArray;
    }

    // setter method
    public void setS(String s){
        this.s = s;
    }

    // setter method
    public void setSomeArray(String[] someArray){
        this.someArray = someArray;
    }
}