Java:如何将数据添加到对象中?

时间:2016-10-15 10:42:33

标签: java string object constructor add

我是Java的新手。如何将String数据添加到Object" myData"并在main中打印出它的内容?

public class myData {
  static String[] myArray = new String[] { "Mimi Rudolph", "minirudolph" };

  public static String[] cutName(String string) {
    return string.split(" ");
  }

  String[] fullName = cutName(myArray[0]);
  String skype = myArray[1];
  String github = null;
  Object myData = new Object();

  public myData(String[] fullName, String skype, String github) {
    this.fullName = fullName;
    this.skype = skype;
    this.github = github;
  }

  public static void main(String[] args) {

    Object myData = new Object();
  }
}

4 个答案:

答案 0 :(得分:1)

您只需创建一个类myData的实例作为下一个:

public static void main(String[] args) {
    myData myData = new myData(myArray, "skype", "github");
    ...
}

要打印其内容,您可以覆盖方法toString(),例如:

@Override
public String toString() {
    return "myData{" +
        "fullName=" + Arrays.toString(fullName) +
        ", skype='" + skype + '\'' +
        ", github='" + github + '\'' +
        ", myData=" + myData +
        '}';
}

然后,您就可以使用System.out.println(myData)打印其内容。

所以最终的代码看起来像这样:

public class myData {
    ...
    @Override
    public String toString() {
        ...
    }

    public static void main(String[] args) {
        myData myData = ...
        System.out.println(myData);
    }
}

答案 1 :(得分:1)

我认为你正在寻找这样的东西:

public class MyData {

private static final String[] myArray = new String[]{"Mimi Rudolph", "minirudolph"};

String[] fullName = cutName(myArray[0]);
String skype = myArray[1];
String github = null;
Object myData = new Object();

private static String[] cutName(String string) {
    return string.split(" ");
}

public MyData(String[] fullName, String skype, String github) {
    this.fullName = fullName;
    this.skype = skype;
    this.github = github;
}

public static void main(String[] args) {
    MyData myData = new MyData(myArray, "Skype string goes here", "githun string goes here");
    System.out.println(myData.fullName);
    System.out.println(myData.github);
    System.out.println(myData.skype);
}
}

答案 2 :(得分:1)

@Tai,我认为你错过了代码中的一些概念。 一旦Java是面向对象的编程语言,当你想要一个新的类实例时,你应该避免在你的方法中使用static

要使用static调用方法,您不需要对象的新实例(例如,可以将其称为MyData.cutName

另一方面,当您创建对象的新实例时,将访问构造函数。

我相信你可以摆脱数组,但我把它保存在你的cutName方法中。 你可以有这样的东西。

public class MyData {
    private String fullname;
    private String skype;
    private String github;

    public MyData(String fullname, String skype, String github) {
        this.fullname = fullname;
        this.skype = skype;
        this.github = github;
    }

    public String getFullname() {
        return this.fullname;
    }

    public String getSkype() {
        return this.skype;
    }

    public String getGithub() {
        return this.github;
    }

    public String[] cutName(String string) {
        return string.split(" ");
    }

    @Override
    public String toString() {
        return "Fullname: " + this.fullname + "; Skype: " + this.skype + "; Github: " + this.github;
    }

    public static void main(String[] args) {
        MyData myData = new MyData("Mimi Rudolph", "minirudolph_skype", "minirudolph_githnub");
        System.out.println("First name: " + myData.cutName(myData.getFullname())[0]);
        System.out.println("Last name: " + myData.cutName(myData.getFullname())[1]);
        System.out.println(myData);
    }
}

输出结果为:

First name: Mimi
Last name: Rudolph
Fullname: Mimi Rudolph; Skype: minirudolph_skype; Github: minirudolph_githnub

拥有类中的属性并从新实例设置它将帮助您获得可重用性。

希望它有所帮助。

答案 3 :(得分:1)

通常可以通过以下简单示例将字符串添加到对象:

 String a = "abc";
 Object b = a;
 System.out.println(b);

如果要将完整的String数组分配给myData Object,则需要执行以下操作:

 Object[] myData  = new Object[myArray.length];
        for(int i=0;i<myArray.length;i++){
            myData [i] = myArray[i];
            System.out.println("MyData Object Array holding strings data: "+myData[i]);
        }

在main方法中,首先需要将对象更改为Arrays对象: 来自 - Object myData = new Object(); TO:Object[] myData = new Object[myArray.length];