美文网首页
第九章有参方法和包

第九章有参方法和包

作者: Dream_wdl | 来源:发表于2018-05-25 10:51 被阅读0次

有参方法和包

定义参数的方法

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

public class ZhazhiJi {
    public String zhazhi ( String fruit ) {
          String juice = fruit + "汁";
          return juice; 
     } 
}
调用参数的方法
ZhazhiJi myZhazhiji = new ZhazhiJi();
String myFruit = "苹果";
String myJuice = myZhazhi.zhazhi(myFruit);
System.out.println(myJuice);
image.png
定义带参数的方法

访问修饰符 返回类型 方法名( ) {
方法的主体
}
调用带参数的方法
对象名 . 方法名(参数1,参数2)

例:public class StudentsBiz {
    String[] names = new String[30];
    int index = 0;//记录数组中学员的个数,也就是下一个需要插入数组的下标位置
    public void addName(String name)
    {
        names[index] = name;
        index++;
    }
    public void showNames()
    {
        for(int i = 0; i < index; i++)
        {
            System.out.println(names[i]);
        }
    }
}


public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StudentsBiz studentsBiz = new StudentsBiz();
        for(int i = 0; i < 3; i++)
        {
            System.out.println("请输入姓名");
            String name = scanner.next();
            studentsBiz.addName(name);
        }
        studentsBiz.showNames();
    }
}
带多个参数的方法
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;
    }
}
调用方法

public class Main {

    public static void main(String[] args) {
    // write your code here
//         zhangsan,lisi,wangwu,zhaobensan,liudehua
        StudentsBiz studentsBiz = new StudentsBiz();
        boolean b = studentsBiz.searchName("liudehua2",-5,8);
        System.out.println(b);
    }
}


数组作为参数
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);
    }
}


相关文章

  • 有参方法和包

    定义带参数的方法参数列表:(数据类型 参数1,数据类型 参数2…) 调用带参数的方法 调用方法,传递的参数要与参数...

  • 第九章有参方法和包

    有参方法和包 定义参数的方法 参数列表:(数据类型 参数1,数据类型 参数2) 调用参数的方法 定义带参数的方...

  • 有参方法和包的概念

    模拟银行账户业务创建包bank.com,编写Account类,添加带参方法实现存款和取款业务,存款时帐户初始金额为...

  • 7.有参方法和包的概念

    定义带参数的方法 <访问修饰符> 返回类型 <方法名>(<形式参数列表>){//方法的主体} 调用带参数的方法对象...

  • 7.有参方法和包的概念

    为什么要用带参数的方法 定义带参数的方法 参数列表: (数据类型 参数1,数据类型 参数2…) 调用带参数的方...

  • 构造方法、封装、关键字(this、static)和代码块的介绍

    1.构造方法 1.1 构造方法与成员方法的区别 构造方法分为无参构造和有参构造,其中有参构造方法和无参构造方法为方...

  • iOS入门小知识-Swift内外参数名

    形参和实参 以OC为例,在方法的声明中,有不带参的,也有带参的,在调用带参方法的时候,参数可分为形参和实参,形参好...

  • Java有参构造方法和无参构造方法

    1、定义: 编写一个类时没有添加无参构造方法,那么编译器会自动添加无参构造方法;(如果自己添加构造函数,无论有参数...

  • iOS OC Runtime动态调用方法

    调用无参方法 有参有返回值方法

  • Java基础整理(二)

    Java方法的几种类型 无参无返回值的方法 无参有返回值的方法 有参无返回值的方法 有参有返回值的方法 Java ...

网友评论

      本文标题:第九章有参方法和包

      本文链接:https://www.haomeiwen.com/subject/ireejftx.html