7,学习有参方法和包

定义带参数的方法

参数列表:
(数据类型 参数1,数据类型 参数2…)

public class ZhazhiJi {
    public String zhazhi ( String fruit ) {
          String juice = fruit + "汁";
          return juice; 
     } 
}

调用带参数的方法
对象名.方法名(参数1, 参数2,……,参数n)

/*调用zhazhi方法*/
ZhazhiJi myZhazhiji = new ZhazhiJi();
String myFruit = "苹果";
String myJuice = myZhazhi.zhazhi(myFruit);
System.out.println(myJuice);
public class StudentsBiz {
    String[] names = {"zhangsan","lisi","wangwu","liudehua"};

    public boolean searchName(String name,int start, int end)
    {
        if(end > names.length)
        {
            end = names.length;
        }
        if(start < 0)
        {
            start = 0;
        }
        for(int i = start; i < end; i++)
        {
            if(names[i].equals(name))
            {
                return true;
            }
        }
        return false;
    }
}
注意参数name的传入

数组作为参数的方法

public class ScoreCalc {

    public int getTotalScore(int[] scores)
    {
        int totalScore = 0;
        for(int i = 0; i < scores.length; i++)
        {
            totalScore += scores[i];
        }
        return totalScore;
    }

    public double getAvgScore(int[] scores)
    {
        int totalScore = getTotalScore(scores);
        return (double) totalScore/scores.length;
    }

    public int getMaxScore(int[] scores)
    {
        int max = scores[0];//假设数组的第一个元素是最大
        for(int i = 1; i < scores.length; i++)
        {
            if(max < scores[i])
            {
                max = scores[i];
            }
        }

        return max;
    }
}


public class Main {

    public static void main(String[] args) {
    // write your code here
        ScoreCalc scoreCalc = new ScoreCalc();
        int[] arrays = {67,76,88,86,99};
        int total = scoreCalc.getTotalScore(arrays);
        System.out.println(total);
        double avg = scoreCalc.getAvgScore(arrays);
        System.out.println(avg);

        int max = scoreCalc.getMaxScore(arrays);
        System.out.println(max);
    }
}

对象作为参数的方法

将多个相关的信息封装成对象,作为参数传递,避免方法有太多的参数!

public class Student {
    int no;//学号
    int age;
    String name;
    int score;

    //toString()方法才能在后面打印出来字符串,否则打印地址
    public String toString()
    {
        String info =  "学号" + no + "年龄" + age + "姓名" + name;
        return info;
    }
}
public class School {
    Student[] students = new Student[30];
    int index = 0;//当前数组中有多少个学生,也就是数组下一个要插入的下标位置
    public void addStudent(Student student)
    {
        students[index] = student;
        index++;
    }

    public void showStudents()
    {
        for(int i = 0; i < index; i++)
        {
            System.out.println(students[i]);
        }
    }
}
public class Main {

    public static void main(String[] args) {
    // write your code here
        School school = new School();
        Student student = new Student();
        student.name = "zhangsan";
        student.no = 10;
        student.age = 23;
        student.score = 90;
        school.addStudent(student);
        school.showStudents();
    }
}

包命名规范

包名之前最好加上唯一的前缀,通常使用组织倒置的网络域名。


image.png

作者:豆约翰
链接:https://www.jianshu.com/p/3bacbcb9bdec
來源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(7,学习有参方法和包)