java基礎(chǔ)之方法和方法的重載詳解
一、帶參方法
1.1 帶參方法的定義和調(diào)用
之前定義的方法大部分都是無(wú)參方法,但是有些方法的執(zhí)行是需要前提條件的,那么參數(shù)就是將這些前提條件傳送過(guò)來(lái)
定義帶參數(shù)的方法:
<訪問修飾符> 返回值類型 <方法名稱> (<形式參數(shù)列表>){
//方法的主體
}
調(diào)用帶參數(shù)的方法
對(duì)象名.方法名(參數(shù)1,參數(shù)2,參數(shù)3…參數(shù)n);
定義榨汁機(jī)的類,輸出詳細(xì)信息
package Kind.dh;
//定義榨汁機(jī)類
public class MethodWithParameters {
//屬性:顏色 價(jià)格
public String color;
public double price;
public void showInfo() {
System.out.println("這是一臺(tái)" + color + "的榨汁機(jī),價(jià)格為:" + price + "元");
}
//方法:榨汁-前提:水果 杯數(shù)-形式參數(shù)(形參):參數(shù)類型 參數(shù)名稱
public void zhazhi(String fruit, int num) {
System.out.println(num + "杯" + fruit + "汁");
}
}
package instance.dh;
import Kind.dh.MethodWithParameters;
import java.util.Scanner;
public class MethodWithParametersTest {
public static void main(String[] args) {
//創(chuàng)建對(duì)象
MethodWithParameters methodWithParameters = new MethodWithParameters();
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入您的榨汁機(jī)的顏色:");
methodWithParameters.color = input.next();
System.out.println("請(qǐng)輸入您的榨汁機(jī)的價(jià)格:");
methodWithParameters.price = input.nextDouble();
methodWithParameters.showInfo();
System.out.println("您想要榨什么果汁:");
String shuiguo = input.next();
System.out.println("您需要榨幾杯果汁:");
int num = input.nextInt();
//這里是實(shí)際參數(shù)(實(shí)參)
methodWithParameters.zhazhi(shuiguo, num);
}
}
1.2 帶參方法使用注意事項(xiàng)
方法定義處的參數(shù)叫形式參數(shù),方法調(diào)用處傳的值為實(shí)際參數(shù)
帶參方法,參數(shù)個(gè)數(shù)可以有一個(gè),也可以有多個(gè),多個(gè)參數(shù)之間用逗號(hào)進(jìn)行隔開
帶參方法,參數(shù)的名字可以隨意的取,符合變量命名規(guī)則
形參和實(shí)參的名字可以不一樣,但是數(shù)據(jù)類型一定要一致,順序要一樣,個(gè)數(shù)要一樣
方法有沒有參數(shù)和方法有沒有返回值沒有聯(lián)系
1.3 帶參方法的應(yīng)用
package Kind.dh;
//定義一個(gè)存放學(xué)生姓名的數(shù)組,實(shí)現(xiàn)添加、查找 、和顯示本班的學(xué)生的信息的方法
//學(xué)員信息管理系統(tǒng)
public class Student02 {
//屬性:存放學(xué)生姓名的數(shù)組
//聲明學(xué)生姓名的數(shù)組
String[] names = new String[30];
//1.添加學(xué)生的姓名
public void addName(String name) {
//遍歷學(xué)生姓名的數(shù)組,查詢到數(shù)組中某一個(gè)元素為null則進(jìn)行插入
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
names[i] = name;
break;//插入學(xué)生的姓名后退出循環(huán)
}
}
}
//2.在固定的區(qū)間內(nèi),查找某一個(gè)學(xué)生
//start:其實(shí)查找的位置
//end:結(jié)束查找的位置
//name:查找的學(xué)生姓名
public boolean searchName(int start, int end, String name) {
boolean flag = true;//是否找到了該名學(xué)生,false沒找到,反之找到了
for (int i = start - 1; i < end; i++) {
if (name.equals(names[i])) {
flag = true;
break;
}
}
return flag;
}
//顯示本班的學(xué)生信息
public void showNames() {
System.out.println("本班的學(xué)員列表:");
for (int i = 0; i < names.length; i++) {
if (names[i] != null) {
System.out.println(names[i] + "\t");
break;
}
}
}
}
package instance.dh;
import Kind.dh.Student02;
import java.util.Scanner;
public class Student02Test {
public static void main(String[] args) {
Student02 student02 = new Student02();
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("請(qǐng)輸入第" + (i + 1) + "個(gè)同學(xué)的姓名:");
String name = input.next();
student02.addName(name);
}
student02.showNames();
//查找某一個(gè)學(xué)生的信息
System.out.println("請(qǐng)輸入要開始查找的位置:");
int start = input.nextInt();
System.out.println("請(qǐng)輸入要結(jié)束查找的位置:");
int end = input.nextInt();
System.out.println("請(qǐng)輸入您要查找的學(xué)生的姓名:");
String findName = input.next();
boolean flag = student02.searchName(start, end, findName);
if (flag) {
System.out.println("恭喜您已經(jīng)查詢到了學(xué)生的信息");
} else {
System.out.println("抱歉,沒有查詢到學(xué)生的相關(guān)信息");
}
}
}
1.4 基本數(shù)據(jù)類型和引用數(shù)據(jù)類型傳參時(shí)的區(qū)別
定義學(xué)生類,并實(shí)現(xiàn)增1操作
package Kind.dh;
//學(xué)生類
public class Student {
//屬性:姓名 年齡 愛好
public String name;
public int age;
public String love;
//方法:輸出個(gè)人信息
public void showInfo() {
System.out.println("我叫" + name + "現(xiàn)在" + age + "歲了" + "我的興趣愛好是" + love);
}
}
package Kind.dh;
public class Demo {
public void calc1(int num) {
num = num + 1;
}
public void calc2(Student student) {
student.age = student.age + 1;
}
}
package instance.dh;
import Kind.dh.Student;
import Kind.dh.Demo;
public class DemoTest {
public static void main(String[] args) {
Demo test = new Demo();
int n = 8;
test.calc1(n);
Student student = new Student();
student.age = 18;
test.calc2(student);
System.out.println(n + "---" + student.age);
}
}
運(yùn)行代碼發(fā)現(xiàn)結(jié)果是8---19,但是我們想得到的是每一個(gè)結(jié)果自增1,應(yīng)該是9---19才對(duì),這是為什么呢?這是由于參數(shù)類型的不同,如果是基本數(shù)據(jù)類型(int char double boolean float),操作傳遞的是變量的值,改變一個(gè)變量的值不會(huì)影像另一個(gè)變量的值。但是參數(shù)如果是引用數(shù)據(jù)類型(自定義數(shù)據(jù)類型 數(shù)組 接口),賦值時(shí)是將原對(duì)象的引用(也就是內(nèi)存地址)傳遞給另一個(gè)引用。
基本數(shù)據(jù)類型傳參:

引用數(shù)據(jù)類型傳參:

1.5 方法傳參-對(duì)象數(shù)組
定義學(xué)生類并賦值輸出學(xué)生的成績(jī),定義一個(gè)修改學(xué)生的成績(jī)的類,如果學(xué)生的的成績(jī)小于60分則進(jìn)行加2
package Kind.dh;
//學(xué)生類
public class Student {
//屬性:姓名 年齡 愛好
public String name;
public int age;
public String love;
public int score;
//方法:輸出個(gè)人信息
public void showInfo() {
// System.out.println("我叫" + name + "現(xiàn)在" + age + "歲了" + "我的興趣愛好是" + love);
System.out.println(name+"的成績(jī)是:"+ score);
}
}
package Kind.dh;
//操作學(xué)生的成績(jī)
public class ModifyScore {
//修改小于60分的學(xué)生成績(jī)
public void modifyStuScore(Student[] stus) {
for (int i = 0; i < stus.length; i++) {
if (stus[i].score < 60) {
stus[i].score += 2;
}
}
}
//顯示本組學(xué)生成績(jī)信息
public void showStu(Student[] stus) {
for (Student stu : stus) {
stu.showInfo();
}
}
}
package instance.dh;
import Kind.dh.ModifyScore;
import Kind.dh.Student;
public class ModifyScoreTest {
public static void main(String[] args) {
ModifyScore modifyScore = new ModifyScore();
//定義一個(gè)學(xué)生對(duì)象的數(shù)組
Student student1 = new Student();
student1.name = "張三";
student1.score = 43;
Student student2 = new Student();
student2.name = "李四";
student2.score = 59;
Student student3 = new Student();
student3.name = "王五";
student3.score = 90;
Student[] students = new Student[3];
students[0] = student1;
students[1] = student2;
students[2] = student3;
//顯示學(xué)生的信息、修改學(xué)生的成績(jī)
System.out.println("成績(jī)修改前:");
modifyScore.showStu(students);
modifyScore.modifyStuScore(students);
System.out.println("成績(jī)修改后:");
modifyScore.showStu(students);
}
}
二、構(gòu)造方法
new一個(gè)對(duì)象的時(shí)候要用到構(gòu)造函數(shù),例如Student student1 = new Student();這時(shí)調(diào)用的是Hello的無(wú)參數(shù)構(gòu)造方法
構(gòu)造方法是用來(lái)完成對(duì)象的初始化的,但是通常在代碼中不需要手動(dòng)書寫,這是因?yàn)橄到y(tǒng)提供了默認(rèn)的無(wú)參的構(gòu)造方法。由于構(gòu)造方法也屬于方法的范疇,可見構(gòu)造方法也可以指定參數(shù)。
構(gòu)造方法的格式如下:
訪問修飾符 構(gòu)造方法名 (){
//初始化代碼
}
需要我們值得注意的是構(gòu)造方法沒有返回值類型,并且方法名和類名是相同的。有返回值類型的方法是常用的普通方法
package Kind.dh;
//學(xué)生類
public class Student {
//屬性:姓名 年齡 愛好
public String name;
public int age;
public String love;
public int score;
//系統(tǒng)會(huì)自動(dòng)生成一個(gè)無(wú)參構(gòu)造方法
/*
public Student(){
//對(duì)象初始化代碼
}
*/
//可以在構(gòu)造方法中添加參數(shù)
/*
public Student(String name,int score){
name = name;
score = score;
}
*/
//可以理解為這段代碼
/*
public Student(String n,int s){
name = n;
score = s;
}
*/
public Student(String name, int score) {
this.name = name;
this.score = score;
}
//方法:輸出個(gè)人信息
public void showInfo() {
// System.out.println("我叫" + name + "現(xiàn)在" + age + "歲了" + "我的興趣愛好是" + love);
System.out.println(name + "的成績(jī)是:" + score);
}
}
package Kind.dh;
//操作學(xué)生的成績(jī)
public class ModifyScore {
//修改小于60分的學(xué)生成績(jī)
public void modifyStuScore(Student[] stus) {
for (int i = 0; i < stus.length; i++) {
if (stus[i].score < 60) {
stus[i].score += 2;
}
}
}
//顯示本組學(xué)生成績(jī)信息
public void showStu(Student[] stus) {
for (Student stu : stus) {
stu.showInfo();
}
}
}
package instance.dh;
import Kind.dh.ModifyScore;
import Kind.dh.Student;
public class ModifyScoreTest {
public static void main(String[] args) {
ModifyScore modifyScore = new ModifyScore();
//定義一個(gè)學(xué)生對(duì)象的數(shù)組
//在這里就實(shí)現(xiàn)了對(duì)象的初始化和賦值
Student student1 = new Student("張三", 43);
// student1.name = "張三";
// student1.score = 43;
Student student2 = new Student("李四", 59);
// student2.name = "李四";
// student2.score = 59;
Student student3 = new Student("王五", 90);
// student3.name = "王五";
// student3.score = 90;
Student[] students = new Student[3];
students[0] = student1;
students[1] = student2;
students[2] = student3;
//顯示學(xué)生的信息、修改學(xué)生的成績(jī)
System.out.println("成績(jī)修改前:");
modifyScore.showStu(students);
modifyScore.modifyStuScore(students);
System.out.println("成績(jī)修改后:");
modifyScore.showStu(students);
}
}
代碼中有這樣的一段:
public Student(String name, int score) {
this.name = name;
this.score = score;
}
這里的this關(guān)鍵字是代指當(dāng)前對(duì)象
Student student1 = new Student("張三", 43);
Student student2 = new Student("李四", 59);
Student student3 = new Student("王五", 90);
所謂的當(dāng)前對(duì)象指的是Student類經(jīng)過(guò)實(shí)例化出的student1,student2,student3。程序執(zhí)行創(chuàng)建了student1時(shí),this代指的是student1;創(chuàng)建了student2時(shí),this代指的是student2這個(gè)對(duì)象。
如果代碼中自定義了帶參的構(gòu)造方法后,系統(tǒng)不會(huì)再提供無(wú)參構(gòu)造方法了
2.1 this的其他用法
this可以調(diào)用類中的普通方法和構(gòu)造方法
package Kind.dh;
//學(xué)生類
public class Student {
//屬性:姓名 年齡 愛好
public String name;
public int age;
public String love;
public int score;
//系統(tǒng)會(huì)自動(dòng)生成一個(gè)無(wú)參構(gòu)造方法
/*
public Student(){
//對(duì)象初始化代碼
}
*/
//可以在構(gòu)造方法中添加參數(shù)
/*
public Student(String name,int score){
name = name;
score = score;
}
*/
//可以理解為這段代碼
/*
public Student(String n,int s){
name = n;
score = s;
}
*/
public Student(String name, int score) {
this.name = name;
this.score = score;
}
//方法:輸出個(gè)人信息
public void showInfo() {
// System.out.println("我叫" + name + "現(xiàn)在" + age + "歲了" + "我的興趣愛好是" + love);
System.out.println(name + "的成績(jī)是:" + score);
}
public void method1(){
// showInfo();
//this可以調(diào)用普通方法
this.showInfo();
}
public Student(String name,int score,int age){
/*
this.name = name;
this.score = score;
this.age = age;
*/
//上述代碼等同于
this(name, score);
this.age= age;
//需要注意的是this調(diào)用構(gòu)造方法時(shí)一定要寫在第一句中。
}
}
三、方法重載
方法分為了普通方法和構(gòu)造方法,所以方法重載也相應(yīng)的分為了普通方法重載和構(gòu)造方法重載
- 構(gòu)造方法重載:
方法名相同
參數(shù)項(xiàng)不同
和返回值、訪問修飾符無(wú)關(guān)
- 普通方法重載
需要在同一個(gè)類中
方法名相同
參數(shù)個(gè)數(shù)或者是類型不同
和返回值、訪問修飾符無(wú)關(guān)
- 實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,分別實(shí)現(xiàn)兩個(gè)整數(shù)、兩個(gè)浮點(diǎn)數(shù)、三個(gè)浮點(diǎn)數(shù)進(jìn)行相加的操作
package Kind.dh;
//實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
public class Calc {
//實(shí)現(xiàn)兩個(gè)整數(shù)相加操作
public void add(int num1, int num2) {
int sum = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + sum);
}
//實(shí)現(xiàn)兩個(gè)浮點(diǎn)數(shù)進(jìn)行相加
public void add(double num1, double num2) {
double sum = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + sum);
}
//實(shí)現(xiàn)三個(gè)浮點(diǎn)數(shù)進(jìn)行相加操作
public void add(double num1, double num2, double num3) {
double sum = num1 + num2 + num3;
System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + sum);
}
}
package instance.dh;
import Kind.dh.Calc;
public class CalcTest {
public static void main(String[] args) {
Calc calc = new Calc();
calc.add(2, 8);
calc.add(2.3, 78.9);
calc.add(23.4, 67.8, 90.8);
}
}
其實(shí)System.out.println();就是一個(gè)方法重載。

3.1 成員變量和局部變量
變量聲明的位置決定變量的作用域,變量的作用域確定可以在程序中按照變量名稱訪問該變量的區(qū)域。
成員變量和局部變量的區(qū)別:
- 作用域不同
局部變量的作用在它的方法中
成員變量(全局變量)作用在整個(gè)類中
- 初始值不同
java會(huì)給成員變量一個(gè)初始值
java不會(huì)給局部變量賦初始值
- 注意:
在同一個(gè)方法中,不允許有同名稱的局部變量
在不同的方法中,可以有同名稱的局部變量
在同一個(gè)類中,成員變量和局部變量同名稱時(shí),局部變量具有更高的優(yōu)先級(jí)
package cn.zhz.Test.dh;
public class Var {
//屬性:這里的是成員變量 全局變量
//定義成員變量num和s
//系統(tǒng)默認(rèn)給成員變量進(jìn)行賦初始值,如果是int就是0,String就是null,double就是0.0
int num;
String s;
//在同一個(gè)類中,局部變量可以和全局變量同名稱,但是同名的時(shí)候局部變量的優(yōu)先級(jí)會(huì)更高一些
int var = 9;
//方法:這里的是成員方法
public void m1() {
//這里的a的作用域在m1中
int a = 1;
for (; a <= 5; a++) {
System.out.println("hello");
}
}
public void m2() {
//這里的a的作用域在for循環(huán)中
for (int b = 1; b <= 5; b++) {
System.out.println(b);
}
}
public void m3() {
System.out.println(num);
System.out.println(s);
}
//參數(shù)也是一種變量,它做的是局部變量
public void m4(int num) {
System.out.println("num = " + num);
}
public static void main(String[] args) {
// //可以通過(guò)擴(kuò)大a的變量范圍來(lái)解決,此時(shí)a的作用域在main方法中
// int a = 0;
// for(;a <= 4;a ++){
// System.out.println("hello");
// }
// System.out.println(a);//系統(tǒng)會(huì)找不到變量a
}
}
到此這篇關(guān)于java基礎(chǔ)之方法和方法的重載詳解的文章就介紹到這了,更多相關(guān)java方法和方法的重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring實(shí)現(xiàn)源碼下載編譯及導(dǎo)入IDEA過(guò)程圖解
這篇文章主要介紹了Spring實(shí)現(xiàn)源碼下載編譯及導(dǎo)入IDEA,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java HttpClient-Restful工具各種請(qǐng)求高度封裝提煉及總結(jié)
這篇文章主要介紹了Java HttpClient-Restful工具各種請(qǐng)求高度封裝提煉及總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
elasticsearch集群cluster?discovery可配式模塊示例分析
這篇文章主要為大家介紹了elasticsearch集群cluster?discovery可配式模塊示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
SpringBoot模擬員工數(shù)據(jù)庫(kù)并實(shí)現(xiàn)增刪改查操作
這篇文章主要給大家介紹了關(guān)于SpringBoot模擬員工數(shù)據(jù)庫(kù)并實(shí)現(xiàn)增刪改查操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
Java基于servlet監(jiān)聽器實(shí)現(xiàn)在線人數(shù)監(jiān)控功能的方法
這篇文章主要介紹了Java基于servlet監(jiān)聽器實(shí)現(xiàn)在線人數(shù)監(jiān)控功能的方法,結(jié)合實(shí)例形式分析了ServletContextListener監(jiān)聽功能的相關(guān)使用步驟與操作技巧,需要的朋友可以參考下2018-01-01
java實(shí)現(xiàn)仿windows 字體設(shè)置選項(xiàng)卡實(shí)例
本篇文章介紹了java仿windows 字體設(shè)置選項(xiàng)卡,可實(shí)現(xiàn)類似windows字體設(shè)置效果,需要的朋友可以參考下。2016-10-10
SpringCache常用注解及key中參數(shù)值為null問題解析
這篇文章主要介紹了SpringCache常用注解及key中參數(shù)值為null的問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

