方法

动态 功能
写在类中
非静态:对象.方法()
静态 :类名.static方法()

同一类中: class A{ a() ; b() } ;
public void a()
{
b();
}

  1. public class MethodReturn {
  2. public void a(){
  3. System.out.println("zs");
  4. }
  5. public void b(){
  6. a();
  7. }
  8. public static void main(String[] args) {
  9. MethodReturn method = new MethodReturn();
  10. method.a();
  11. }
  12. }

返回值

无范围值

返回值为void

有返回值

public 返回值类型 方法名()
{
return 类型对应的数据
}

public int a()
{
return 1;
}

  1. public class MethodReturn {
  2. public void a(){
  3. System.out.println("zs");
  4. }
  5. public String a1(){
  6. String name = "zs";
  7. //System.out.println(name);
  8. return name;
  9. }
  10. public void b(){
  11. //a();
  12. String name = a1(); //2.调a1,返回a1中的name给b中的name
  13. System.out.println("b:"+name);
  14. }
  15. public static void main(String[] args) {
  16. MethodReturn method = new MethodReturn();
  17. method.b(); //1.调b
  18. }
  19. }

在调用有返回值的方法时,需要接收 String xx = a1( ) ;

面向对象编程

类和对象、将功能独立写在方法中

从键盘接收:计算三门课的平均成绩、总成绩

  1. import java.util.Scanner;
  2. public class OOPScore {
  3. //属性(静态行为)、全局变量
  4. int javaScore ;
  5. int cScore ;
  6. int sqlScore ;
  7. public void inputScore(){
  8. Scanner input = new Scanner(System.in);
  9. System.out.println("请输入JAVA成绩");
  10. javaScore = input.nextInt();
  11. System.out.println("请输入C成绩");
  12. cScore = input.nextInt();
  13. System.out.println("请输入SQL成绩");
  14. sqlScore = input.nextInt();
  15. }
  16. //计算平均分并返回
  17. public double calcAvg(){
  18. double av =(javaScore+cScore+sqlScore)/3.0;
  19. return av;
  20. }
  21. //计算总分返回
  22. public int calcSum(){
  23. int sum = javaScore+cScore+sqlScore;
  24. return sum;
  25. }
  26. public static void main(String[] args) {
  27. OOPScore score = new OOPScore();
  28. score.inputScore();
  29. double avg = score.calcAvg();
  30. int sum = score.calcSum();
  31. System.out.println(avg +"---" +sum);
  32. }
  33. }

参数

个性化,谁在睡觉
sleep1(String name)

  1. public class People {
  2. public void sleep(){ //无参:()里没东西
  3. System.out.println("睡觉...");
  4. }
  5. public void sleep1(String name){ //有参 String name 个性化 形参:name
  6. System.out.println(name+"睡觉...");
  7. }
  8. public static void main(String[] args) {
  9. People zs = new People();
  10. zs.sleep1("张三"); //实参:张三
  11. People ls = new People();
  12. ls.sleep1("李四");
  13. }
  14. }
  15. 输出:
  16. 张三睡觉...
  17. 李四睡觉...

更加个性化 :谁在哪里睡觉
sleep1(String name,String place)

  1. public class People {
  2. public void sleep(){
  3. System.out.println("睡觉...");
  4. }
  5. public void sleep1(String name,String place){
  6. System.out.println(name+"在"+place+"睡觉...");
  7. }
  8. public static void main(String[] args) {
  9. People zs = new People();
  10. zs.sleep1("张三","家里");
  11. People ls = new People();
  12. ls.sleep1("李四","宾馆");
  13. }
  14. }
  15. 输出:
  16. 张三在家里睡觉...
  17. 李四在宾馆睡觉...

参数越多越个性化,但太多就很麻烦,可用数组简化

  1. lic class People {
  2. public void sleep(){
  3. System.out.println("睡觉...");
  4. }
  5. public void sleep1(String name,String place,String time){
  6. System.out.println(name+"在"+place+"睡觉,时间"+time);
  7. }
  8. //数组infos = {name,place,time}
  9. public void sleep2(String[] infos){
  10. System.out.println(infos[0]+"在"+infos[1]+"睡觉,时间"+infos[2]);
  11. }
  12. public static void main(String[] args) {
  13. People zs = new People();
  14. //数组infos = {name,place,time}
  15. String[] zsinfos = {"张三","家里","20:22"};
  16. zs.sleep2(zsinfos);
  17. People ls = new People();
  18. String[] lsinfos = {"李四","宾馆","21:22"};
  19. ls.sleep2(lsinfos);
  20. }
  21. 输出:
  22. 张三在家里睡觉,时间20:22
  23. 李四在宾馆睡觉,时间21:22