小白求问getter和setter问题
package com.imooc;
public class Demo1 {
private int num1;
public int getNum1(){
return num1;
}
public void setNum1(int newNum1){
num1=newNum1;
}
以上是对的程序,通过下面程序调用
package com.imooc;
public class Demo2 {
public static void main(String[] args) {
Demo1 hello3=new Demo1();
hello3.setNum1(55);
System.out.println(hello3.getNum1());
}
}
再看下面
public int setNum1(int newNum1){
num1=newNum1;
return num1;
}
这种写法没提示出错,但是调用不了是为什么,下面是上面的调用程序
package com.imooc;
public class Demo2 {
public static void main(String[] args) {
Demo1 hello3=new Demo1();
hello3.setNum1(55);
System.out.println(hello3.setNum1());
}
}在System.out.println(hello3.setNum1());的setNum1()下面出错了,是怎么回事