封装(用户输入)

时间:2016-11-13 16:32:15

标签: java encapsulation

//这是我的主要课程

import java.io.*;
import java.util.*;

public class TheInnovator{      
    private String name;
    private String age;
    private String designation;
    private String course;
    private String yrlvl;

    public TheInnovator(String name, String age, String designation, String course, String yrlvl){

        this.name = name;
        this.age = age;
        this.designation = designation;
        this.course = course;
        this.yrlvl = yrlvl;

    }

    public void setName(String name){
        this.name = name;
    }

    public void setAge(String age){
        this.age = age;
    }

    public void setDesignation (String designation){
        this.designation = designation;
    }

    public void setCourse(String couse){
        this.course = course;
    }

    public void setYrlvl (String yrlvl){
        this.yrlvl = yrlvl;
    }

    public String getName(){
        return name;
    }

    public String getAge(){
        return age;
    }

    public String getDesignation(){
        return designation;
    }

    public String getCourse(){
        return course;
    }

    public String getYrlvl(){
        return yrlvl;
    }

}

//这是我的主要驱动程序

import java.util.*;
public class MainDriver{

    public static void main(String args[]){

        TheInnovator theinnov = new TheInnovator();
        Scanner input = new Scanner(System.in);

        theinnov.setName = (input.nextLine());
        theinnov.setAge = (input.nextLine());
        theinnov.setDesignation = (input.nextLine());
        theinnov.setCourse = (input.nextLine());
        theinnov.setYrlvl = (input.nextLine());



        System.out.println("Name: " + theinnov.getName());
        System.out.println("\nAge: " + theinnov.getAge());
        System.out.println("\nDesignation: " + theinnov.getDesignation());
        System.out.println("\nCourse: " + theinnov.getCourse());
        System.out.println("\nYear Level: " + theinnov.getYrlvl());     

    }


}

So my problem is everytime I run the MainDriver.java, it cannot find my setter variables. what's wrong or what's missing in my code? thank you for a quik response! anyway I'm using notepad++ on this because it's a requirement.

2 个答案:

答案 0 :(得分:0)

您需要删除=标志。它使编译器认为setName是一个字段而不是方法。

theinnov.setName (input.nextLine());

答案 1 :(得分:0)

 class Quick {

 public static void main(String[]args) {
 Don op=new Don();
 Scanner sc=new Scanner(System.in);
 System.out.println("enter");
 op.setName(sc.nextLine());
 System.out.println("Name: " + op.getName());

 }
}

 class Don
{

 private String name;

 public void setName(String name)
 {
 this.name = name;
 }
 public String getName(){
        return name;
    }

 }

输出:名称:忠诚