首先封装一段代码 定义在类中方法外
public class Test01 {
public static int getsum() {
int a = 10;
int b = 20;
int c = a+b;
return c;
}
在主方法中调用getsum方法
public static void main(String[] args) {
System.out.println(Test01.getsum());
}

//封装了一个公共的静态的int类型的一个做加法的方法
public static int getsum(int a,int b) {
int c = a+b;
return c;
}
public static void main(String[] args) {
// 有返回值可使用输出调用
System.out.println(getsum(9,3));
// 有返回值可使用赋值调用
int d = getsum(35,222);
System.out.println(d);
}
