【问题标题】:Constructor function idea :D构造函数思想:D
【发布时间】:2015-12-26 10:43:24
【问题描述】:

我不知道这是可能的还是最糟糕的想法,哈哈; 但是我正在尝试为我的模型类编写构造函数,它使用字符串数组并自动从它绑定参数:) 感谢您在我可怜的问题上浪费您的黄金时间:D 这是示例: 编辑:代码,我的意思是,我创建这个类的对象只是将 String[] 给构造函数.... :(

//Like ---->>>>
public class MemberEducation{
String name;
String surname;
String address;
//empty constructor
public MemberEducation(){
}
//it's for parameters
public MemberEducation(String[] a){
int i=0;       
for(String val:a){
my_parameters[i]=a;// my parameters mean surname, name, and address
i++; 
}
}
// it's my solution now i'm using, create new object with empty constructor         
then set  all parameter with String array
public  void setAll(String[] a){
this.surname=a[0];
this.name=a[1];
this.address=a[2];
}

【问题讨论】:

  • 问题是什么?..

标签: java arrays constructor minify


【解决方案1】:

问题

您将 String Array 分配到它期望 String 的位置,这就是它出错的地方。我想您正在寻找这个

解决方案

public class MemberEducation{
    String[] arr = new String[10];// just taking size 10 for test

    public MemberEducation(String[] a) {
            int i = 0;
            for (String val : a) {
                arr[i++] = val;// arr is an string array
            }
        }

    public static void main(String args[]) {
    new MemberEducation(args);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多