Java调用方法的值

时间:2014-05-09 10:06:25

标签: java class methods

首先抱歉我的英语并不完美。 我在java中遇到了一个小问题(对我来说是个大问题)。

package test;

import java.util.Scanner;

public class adress {

    String adress;
    String city;
    int postcode;
    String ergebnis;



    public void setadress(String adress)
    {
        this.adress = adress;
    }

    public String getadress()
    {
        return adress;
    }


    public void setcity(String city)
    {
        this.city = city;
    }

    public String getcity()
    {
        return city;
    }

    public void setpostcode(int postcode)
    {
        this.postcode = postcode;
    }

    public int getpostcode()
    {
        return postcode;
    }

    public void output (String adress, String city, int postcode) {

        Scanner a = new Scanner (System.in);
        System.out.println("How much values?");
        int b = a.nextInt();
        int [] c  = new int [b];

        for (int i=0; i<c.length; i++)  {



            Scanner input = new Scanner (System.in);
            System.out.println("Adress?");
            String temp = input.nextLine();
            setadresse(temp);

            Scanner input3 = new Scanner (System.in);
            System.out.println("City?");
            String temp2 = input3.nextLine();
            setcity(temp2);

            Scanner input4 = new Scanner (System.in);
            System.out.println("Postcode?");
            int temp3 = input4.nextInt();
            setpostcode(temp3);

            this.adress = adress;
            this.city = city;
            this.postcode = postcode;


            System.out.println("Adress: "+adress+"City"+city+"postcode"+postcode);

        }


    }

}

现在,我想将值保存在数组

中的新类中
package test;

public class save {

    adress [] saver = new adress[10];

    public adressenpool (String adress, String city, int postcode){



        for(int i =0; i<10;i++)
            saver[i] = ????? ;   //i have tried several things here, but it will not work. i know it is      just a little problem but i can't get it the mistake

    }


}

}

如何从地址类中获取值并将其复制为保护程序类中的数组?

3 个答案:

答案 0 :(得分:0)

看起来你试图将10个类地址对象放在类save的对象中,而不是仅仅是地址中的信息。这通常是一个好主意,所以我鼓励你继续。

要在方法adressenpool中创建地址,您需要使用其构造函数。目前,类address只有一个默认构造函数,它创建一个有效的空地址。我会添加一个完全创建对象的新构造函数

public class adress {

     String adress;
     String city;
     int postcode;
     String ergebnis;

     public adress(String adress, String city, int postcode, String ergebnis){
          this.adress=address;
          this.city=city;
          this.postcode=postcode;
          this.ergebnis=ergebnis;

     }

     //you can have several constructors so you can keep the empty constructor if you want to set the elements piece by piece
     public adress(){
     }

     ......
     other methods as before
}

添加构造函数后,您现在可以轻松地创建地址

  public adressenpool (String adress, String city, int postcode,String ergebnis){

        saver[0] = new adress(adress, city, postcode,ergebnis);
  }

但是,您的方法adressenpool仅包含足够的信息来创建1个地址。您可能希望设置将其保存在哪个索引处。或者您可能希望从数组更改为arraylist,这样您就可以随时添加新的adress

  public adressenpool (String adress, String city, int postcode,String ergebnis, int index){

        saver[index] = new adress(adress, city, postcode,ergebnis);
  }

注释

  • 类始终以大写字母开头。对象 小写。所以应该是课程Save和课程Address
  • 没有大括号的for循环(和if语句)被认为是危险的事情。即使它包含单个语句,也始终在您的循环中包含{}。所以

     for(int i =0; i<10;i++){
        saver[i] new Adress(adress, city, postcode,ergebnis);
     }
    

答案 1 :(得分:0)

我认为你应该更具体地说明你想做什么。 您的第一个班级有4个成员(3个String和1个int)并且您想将地址类中的值保存为保护程序类中的数组?你最后一个是什么意思?

我猜你需要通过调用适当的setter来填充你定义的数组中的每个地址实例(saver)。 (顺便说一下,你没有定义setadresse()。例如,这可以在循环中完成。

这也不是很直接://i have tried several things here, but it will not work.你尝试了什么,但没有用?

当然,您还需要一个main()功能来运行您的程序。

我希望这有点帮助...

答案 2 :(得分:0)

这将解决问题

package temp;

import java.util.Scanner;

public class adress {

     String adress;
     String city;
     int postcode;
     String ergebnis;



   public void setadress(String adress) 
    {
        this.adress = adress;
    }

    public String getadress() 
    {
        return adress;
    }


     public void setcity(String city) 
    {
        this.city = city;
    }

    public String getcity() 
    {
        return city;
    }

    public void setpostcode(int postcode) 
    {
        this.postcode = postcode;
    }

    public int getpostcode() 
    {
        return postcode;
    }

    public void setAddress () {


        Scanner input = new Scanner (System.in);
        System.out.println("Adress?");
        String temp = input.nextLine();
        setadress(temp);

        Scanner input3 = new Scanner (System.in);
        System.out.println("City?");
        String temp2 = input3.nextLine();
        setcity(temp2);

        Scanner input4 = new Scanner (System.in);
        System.out.println("Postcode?");
        int temp3 = input4.nextInt();
        setpostcode(temp3);



    }



    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Adress: "+adress+"City"+city+"postcode"+postcode;
    }



}

第二节课

package temp;

import java.util.Scanner;

public class save {

    adress [] saver;


    public save(){
        saver = new adress[10];
    }

    public void adressenpool(){
        Scanner a = new Scanner (System.in);
        System.out.println("How much values?");
        int b = a.nextInt();
        adress address1 = null;
        for (int i=0; i<b; i++)  {    

            address1 = new adress();
            address1.setAddress();


            this.saver[i] = address1;
        }
    }


    public static void main(String[] args) {

        save saveTemp = new save();
        saveTemp.adressenpool();
        for(int i=0; i<2; i++){

            System.out.println(saveTemp.saver[i].toString());
        }
    }

}