配套视频教程

本文B站配套视频教程

为什么要用带参数的办法


定义带参数的办法

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

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

调用带参数的办法

/*调用zhazhi办法*/ZhazhiJi myZhazhiji = new ZhazhiJi();String myFruit = "苹果";String myJuice = myZhazhi.zhazhi(myFruit);System.out.println(myJuice);

调用办法,传递的参数要与参数列表一一对应

定义带参数的办法

<拜访修饰符> 返回类型 <办法名>(<形式参数列表>) {

      //办法的主体

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

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);    }}

数组作为参数的办法

有5位学员加入了Java常识比赛的决赛,输入决赛的均匀问题和最高问题

将5位学员的决赛问题保留在数组中
设计求均匀问题、最高问题的办法,并把数组作为参数

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;    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 void addStudent(String name, int no,int age, int score,int height)//    {////    }}
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();    }}

为什么须要包

Windows树形文件系统
文档分门别类,易于查找和治理
应用目录解决文件同名抵触问题

如何寄存两个同名的类而不抵触?

 //申明包,作为Java源代码第一条语句,//用package申明包,以分号结尾//com.company.model是包名package com.company.model;            public class School {    //……    public String toString() {       //……    }}

包命名标准

包名由小写字母组成,不能以圆点结尾或结尾
包名之前最好加上惟一的前缀,通常应用组织倒置的网络域名
package net.javagroup.mypackage;
包名后续局部依不同机构外部的标准不同而不同

用intelij创立包和类

包与目录的关系

创立好的包和Java源文件是如何存储的?
创立包cn.company.classandobject ,
即创立了目录构造:cncompanyclassandobject

如何导入包

为了应用不在同一包中的类,须要在Java程序中应用import关键字导入这个类

import java.util.*;      //导入java.util包中所有类import cn.company.classandobject.School;    //导入指定包中指定类

本节练习

模仿银行账户业务

创立包bank.com,
编写Account类,增加带参
办法实现贷款和取款业务,
贷款时帐户初始金额为0元,
取款时如果余额有余给出提醒

package com.bank;/** * Created by ttc on 2017/12/26. *///账户public class Account {    public int money;//账户余额    //存钱    public void saveMoney(int value)    {        money += value;        System.out.println("贷款胜利");    }    //取钱    public void getMoney(int value)    {        if(money < value)        {            System.out.println("余额有余");            return;        }        money -= value;        System.out.println("取款胜利");    }    //显示以后余额    public void showMoney()    {        System.out.println("以后余额为:" + money);    }}
public class Main {    public static void main(String[] args) {    // write your code here        Scanner scanner = new Scanner(System.in);        int command = 0;//命令        //初始化账户对象        Account account = new Account();        while (true)        {            System.out.println("1 贷款 2 取款 0 退出");            System.out.println("请抉择要办理的业务");            command = scanner.nextInt();            if(command == 1)//贷款            {                System.out.println("请输出金额");                //获取用户输出的金额                int value = scanner.nextInt();                account.saveMoney(value);                account.showMoney();            }            else if(command == 2)            {                System.out.println("请输出金额");                //获取用户输出的金额                int value = scanner.nextInt();                account.getMoney(value);                account.showMoney();            }            else if(command == 0)            {                break;            }            else            {                System.out.println("谬误的命令");            }        }        System.out.println("程序退出");    }}