问答详情
源自:7-1 什么是 Java 中的封装

为什么有错误

public class Demo {

public String name;

public String brithday;

public int number;

public void setName(String name){

this.name=name;

}

public void setBrithday(String brithday){

this.brithday=brithday;

}

public void setNumber(int number){

this.number=number;

}

public String getName(){

return this.name;

}

 

public String getBrithday(){

return this.brithday;

}

 

public int getNumber(){

return this.number;

}

 

public static void main(String[]args){

Demo student=new Demo();

student.setName("杨");

student.setBrithday("1996-11-11");

student.setNumber("1408441063"); //这里报错

 

System.out.println(student.getName());

System.out.println(student.getBrithday());

System.out.println(student.getNumber());

}

}


提问者:qq_星尘_8 2016-12-20 21:31

个回答

  • qq_走下去不回头_0
    2016-12-20 21:36:46
    已采纳

    你的setNumber(int number)传入的参数是需要int类型的,student.setNumber("1408441063");传入的类型是字符串所以报错,要保证传入的类型和要和参数的类型一致