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()下面出错了,是怎么回事
hello3.setNum1(55);这个方法是有返回值的,因此你必须将它附给一个新值,
比如 int a =hello3.setNum1(55); System.out.println(a); 这样修改是正确的;
或者直接System.out.println(hello3.setNum1(55));不需要前面的那句赋值的语句
没有赋值啊