返回值与块的应用小栗子

引用数据类型做参数,基本数据类型做返回值的案例。

 1 package com.ccp.iu;
 2 public class Test1 {
 3     public static void main(String[] args) {
 4         int se=new Test1().ss("你是最棒的猪才怪!");
 5         System.out.println(se);
 6         int []qq=new Test1().xx("你是最棒的猪才怪!");
 7         for (int i=0; i <qq.length ; i++) {
 8             System.out.println(qq[i]);
 9         }
10     }
11     public int ss(String name){
12         int s1=0;
13         if ("你是最棒的猪才怪!".equals(name)){
14             s1=11;
15         }
16             return s1;
17     }
18     public int[] xx(String name){
19         int[] x1=new int[5];
20         if ("你是最棒的猪才怪!".equals(name)){
21             for (int i=0; i <x1.length ; i++) {
22                 x1[i]=x1[i]+5;
23             }
24         }
25         return x1;
26     }
27 }

使用块来完成实体中的初始值的存储

 1 package com.ccp.iu;
 2 
 3 public class Test2 {
 4 
 5     public static void main(String[] args) {
 6         Test2 test2=new Test2();
 7     }
 8     {
 9         int[] aa=new int[10];
10         for (int i=0; i <2; i++) {
11             aa[i]+=5;
12             System.out.println(aa[i]);
13         }
14     }
15  }

 

posted on 2019-08-02 20:55  年夜饭  阅读(134)  评论(0编辑  收藏  举报

导航