java基礎(chǔ)之方法和方法的重載詳解
一、帶參方法
1.1 帶參方法的定義和調(diào)用
之前定義的方法大部分都是無參方法,但是有些方法的執(zhí)行是需要前提條件的,那么參數(shù)就是將這些前提條件傳送過來
定義帶參數(shù)的方法:
<訪問修飾符> 返回值類型 <方法名稱> (<形式參數(shù)列表>){
//方法的主體
}
調(diào)用帶參數(shù)的方法
對象名.方法名(參數(shù)1,參數(shù)2,參數(shù)3…參數(shù)n);
定義榨汁機的類,輸出詳細信息
package Kind.dh;
//定義榨汁機類
public class MethodWithParameters {
//屬性:顏色 價格
public String color;
public double price;
public void showInfo() {
System.out.println("這是一臺" + color + "的榨汁機,價格為:" + 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)建對象
MethodWithParameters methodWithParameters = new MethodWithParameters();
Scanner input = new Scanner(System.in);
System.out.println("請輸入您的榨汁機的顏色:");
methodWithParameters.color = input.next();
System.out.println("請輸入您的榨汁機的價格:");
methodWithParameters.price = input.nextDouble();
methodWithParameters.showInfo();
System.out.println("您想要榨什么果汁:");
String shuiguo = input.next();
System.out.println("您需要榨幾杯果汁:");
int num = input.nextInt();
//這里是實際參數(shù)(實參)
methodWithParameters.zhazhi(shuiguo, num);
}
}
1.2 帶參方法使用注意事項
方法定義處的參數(shù)叫形式參數(shù),方法調(diào)用處傳的值為實際參數(shù)
帶參方法,參數(shù)個數(shù)可以有一個,也可以有多個,多個參數(shù)之間用逗號進行隔開
帶參方法,參數(shù)的名字可以隨意的取,符合變量命名規(guī)則
形參和實參的名字可以不一樣,但是數(shù)據(jù)類型一定要一致,順序要一樣,個數(shù)要一樣
方法有沒有參數(shù)和方法有沒有返回值沒有聯(lián)系
1.3 帶參方法的應(yīng)用
package Kind.dh;
//定義一個存放學(xué)生姓名的數(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ù)組中某一個元素為null則進行插入
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
names[i] = name;
break;//插入學(xué)生的姓名后退出循環(huán)
}
}
}
//2.在固定的區(qū)間內(nèi),查找某一個學(xué)生
//start:其實查找的位置
//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("請輸入第" + (i + 1) + "個同學(xué)的姓名:");
String name = input.next();
student02.addName(name);
}
student02.showNames();
//查找某一個學(xué)生的信息
System.out.println("請輸入要開始查找的位置:");
int start = input.nextInt();
System.out.println("請輸入要結(jié)束查找的位置:");
int end = input.nextInt();
System.out.println("請輸入您要查找的學(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ù)類型傳參時的區(qū)別
定義學(xué)生類,并實現(xiàn)增1操作
package Kind.dh;
//學(xué)生類
public class Student {
//屬性:姓名 年齡 愛好
public String name;
public int age;
public String love;
//方法:輸出個人信息
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);
}
}
運行代碼發(fā)現(xiàn)結(jié)果是8---19,但是我們想得到的是每一個結(jié)果自增1,應(yīng)該是9---19才對,這是為什么呢?這是由于參數(shù)類型的不同,如果是基本數(shù)據(jù)類型(int char double boolean float),操作傳遞的是變量的值,改變一個變量的值不會影像另一個變量的值。但是參數(shù)如果是引用數(shù)據(jù)類型(自定義數(shù)據(jù)類型 數(shù)組 接口),賦值時是將原對象的引用(也就是內(nèi)存地址)傳遞給另一個引用。
基本數(shù)據(jù)類型傳參:

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

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

3.1 成員變量和局部變量
變量聲明的位置決定變量的作用域,變量的作用域確定可以在程序中按照變量名稱訪問該變量的區(qū)域。
成員變量和局部變量的區(qū)別:
- 作用域不同
局部變量的作用在它的方法中
成員變量(全局變量)作用在整個類中
- 初始值不同
java會給成員變量一個初始值
java不會給局部變量賦初始值
- 注意:
在同一個方法中,不允許有同名稱的局部變量
在不同的方法中,可以有同名稱的局部變量
在同一個類中,成員變量和局部變量同名稱時,局部變量具有更高的優(yōu)先級
package cn.zhz.Test.dh;
public class Var {
//屬性:這里的是成員變量 全局變量
//定義成員變量num和s
//系統(tǒng)默認給成員變量進行賦初始值,如果是int就是0,String就是null,double就是0.0
int num;
String s;
//在同一個類中,局部變量可以和全局變量同名稱,但是同名的時候局部變量的優(yōu)先級會更高一些
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) {
// //可以通過擴大a的變量范圍來解決,此時a的作用域在main方法中
// int a = 0;
// for(;a <= 4;a ++){
// System.out.println("hello");
// }
// System.out.println(a);//系統(tǒng)會找不到變量a
}
}
到此這篇關(guān)于java基礎(chǔ)之方法和方法的重載詳解的文章就介紹到這了,更多相關(guān)java方法和方法的重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過Feign進行調(diào)用@FeignClient?找不到的解決方案
這篇文章主要介紹了通過Feign進行調(diào)用@FeignClient?找不到的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
mybatis-plus動態(tài)表名的實現(xiàn)示例
這篇文章主要介紹了mybatis-plus動態(tài)表名的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
用Maven插件生成Mybatis代碼的實現(xiàn)方法
本文主要介紹 Maven插件生成Mybatis代碼,現(xiàn)在做開發(fā)的朋友有好多用Maven 來管理代碼,這里給大家舉個例子,有需要的同學(xué)可以看下2016-07-07
詳解mybatis foreach collection示例
這篇文章主要介紹了詳解mybatis foreach collection的相關(guān)資料,需要的朋友可以參考下2017-10-10
Linux下用java -jar運行可執(zhí)行jar包的方法教程
這篇文章主要給大家介紹了在Linux下用java -jar運行可執(zhí)行jar包的方法教程,文中介紹的非常詳細,相信對大家的工作或者學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05

