无法应用java方法和构造函数

时间:2017-10-29 02:40:33

标签: arrays inner-classes getter-setter

我打算制作一个程序,询问用户他们想要在车库中存储多少辆车,然后让用户将车辆的信息存入阵列,这是代码:

public class Main {
    static class veh {
        int roda;
        String warna;
        String merk;

        public veh(int roda, String warna, String merk){
            this.roda = roda;
            this.warna = warna;
            this.merk = merk;
        }
        private void setRoda(){
        }
        private void setWarna(){
        }
        private void setMerk(){
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Pemilik Garasi\t: ");
        String nama = in.nextLine();
        System.out.print("NIM\t\t\t\t: ");
        String nim = in.next();
        System.out.print("Kelas\t\t\t: ");
        String kls = in.next();
        System.out.print("Masukkan Luas Garasi\t:");
        int luas = in.nextInt();
        veh[] data = new veh[luas];
        System.out.println("============================");

        for (int i = 0; i < luas; i++) {
            veh vehicle = new veh();
            System.out.println("Masukkan Data Kendaraan Ke " + (i + 1));
            System.out.print("Jumlah Roda\t: ");
            vehicle.setRoda(in.nextInt());
            System.out.print("Warna\t\t: ");
            vehicle.setWarna(in.next());
            System.out.print("Merk\t\t: ");
            vehicle.setMerk(in.next());
            data[i] = vehicle;
        }

        System.out.println("====== GARASI PRIBADI ======");
        System.out.println("Milik \t: " + nama);
        System.out.println("NIM \t: " + nim);
        System.out.println("Kelas \t: " + kls);
        System.out.println("============================");
        for (int i = 0; i < luas; i++) {
            System.out.println("Kendaraan Ke " + (i + 1));
            System.out.println("Jumlah Roda\t : " + data[i].roda);
            System.out.println("Warna\t\t : " + data[i].warna);
            System.out.println("Merk\t\t : " + data[i].merk);
            System.out.println("--------------------------");
        }
    }
}

当我编译它时,我在这一行中出错:

veh vehicle = new veh();
vehicle.setRoda(in.nextInt());
vehicle.setWarna(in.next());
vehicle.setMerk(in.next());

我真的不知道getter / setter如何工作,我是Java编程语言的新手,我写错了什么?

1 个答案:

答案 0 :(得分:0)

  1. 您正在调用没有参数的构造函数,以便执行此操作 你需要重载它,因此,声明另一个不带参数的构造函数。

  2. 一般来说,setter和getter或封装的想法是阻止直接访问变量本身,并使用函数修改或获取它们的值。你的setter需要接受一个参数并设置你想要的值 并且getter返回值。

    public class Main {
        static class veh {
            private int roda;
            private String warna;
            private String merk;
    
            public veh (){}
    
            public veh(int roda, String warna, String merk){
               this.roda = roda;
               this.warna = warna;
               this.merk = merk;
            }
            public void setRoda(int newRoda){
               roda = newRoda;
            }
    
             public void setWarna(String newWarna){
               warna  = newWarna;
            }
            public void setMerk(String newMerk){
               merk = newMerk;
            }
            public void getRoda(){
               return roda;
            }
    
            public void getWarna(){
               retun warna;
            }
            public void getMerk(){
               return merk;
            }
    
       }