配套視頻教程
為什么要用帶參數的方法
定義帶參數的方法
參數列表:
(數據類型 參數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 ,
即創建了目錄結構:cn\company\classandobject
如何導入包
為了使用不在同一包中的類,需要在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("程序退出");
}
}