類的成員包括屬性和方法,也稱作成員變量和成員方法。
1.成員方法
1.1.方法重載
方法重載是OOP中的一個重要概念,而實(shí)際開發(fā)中經(jīng)常用的方法重載。
1.1.1.方法重載的定義
方法重載是指在一個類中定義多個同名的方法,但要求每個方法具有不同的參數(shù)類型或參數(shù)個數(shù)或參數(shù)順序。即:在同一個類中,同名不同參,與返回值無關(guān)。
public class Student{
public void play(){
}
public int play(int time){
return 0;
}
}
1.1.2.方法重載的調(diào)用
方法重載在調(diào)用時,根據(jù)實(shí)參與形參在類型、個數(shù)、順序一一匹配的規(guī)則調(diào)用。即:方法重載根據(jù)參數(shù)匹配原則進(jìn)行調(diào)用。
public class Student{
public void play(){
System.out.println("方法一");
}
public int play(int time){
System.out.println("方法二");
return 0;
}
public static void main(String[] args) {
Student s1 = new Student();
s1.play(); //調(diào)用的是第一個方法
Student s2 = new Student();
s2.play(1); //調(diào)用的是第二個方法
}
}
運(yùn)行結(jié)果:
方法一
方法二
2.什么是構(gòu)造方法
2.1.什么是構(gòu)造方法
構(gòu)造方法也叫構(gòu)造函數(shù),或者叫構(gòu)造器
在java中,當(dāng)類創(chuàng)建一個對象時會自動調(diào)用該類的構(gòu)造方法,構(gòu)造方法分為默認(rèn)構(gòu)造方法和自定義的構(gòu)造方法。
構(gòu)造方法的語法格式:
[訪問修飾符] 方法名([參數(shù)列表]){
//方法體的代碼
}
特點(diǎn):
1.構(gòu)造方法的方法名必須與類名相同
2.構(gòu)造方法沒有返回值,也不寫void
示例:
public class Student {
//構(gòu)造方法
Student(){
}
}
注意:下面這個方法是構(gòu)造方法嗎?
public class Student {
void Student(){//普通方法,可有對象調(diào)用
}
void play(){
}
}
答:不是,第2行的Student()方法前面添加了void,因此不是構(gòu)造方法,此時它是一個普通的方法。與play方法一樣??梢允褂脤?shí)例化后對象調(diào)用Student()方法。
不推薦把普通方法名稱定義為與類名相同。
2.2.構(gòu)造方法的作用
構(gòu)造方法的作用為成員變量初始化(為對象的屬性初始化)。
2.2.1.默認(rèn)構(gòu)造函數(shù)
一個類如果沒有顯示的定義構(gòu)造函數(shù),那么這個類默認(rèn)具有無參的構(gòu)造函數(shù)。
默認(rèn)構(gòu)造函數(shù)為對象的屬性賦默認(rèn)值。
class Student {
String name;
int score;
String no;
public void play(){
System.out.printf("我的名字是%s,我的成績是%d,我的學(xué)號是%s",this.name,this.score,this.no);
}
}
public class StudentTest {
public static void main(String[] args) {
Student s1 =new Student();
s1.play();
}
}
運(yùn)行結(jié)果:
我的名字是null,我的成績是0,我的學(xué)號是null
為什么輸出的是null,0,null?
分析:因?yàn)楸纠袥]有定義構(gòu)造函數(shù),系統(tǒng)會自動添加一個默認(rèn)構(gòu)造函數(shù),默認(rèn)構(gòu)造函數(shù)是無參的,會為所有的屬性賦默認(rèn)值,因此name是null,score是0,no是null。
2.2.2.自定義的構(gòu)造函數(shù)
如果顯示的定義了構(gòu)造函數(shù),那么默認(rèn)構(gòu)造函數(shù)就沒有啦。
class Student {
String name;
int score;
String no;
//顯示定義構(gòu)造函數(shù),此時默認(rèn)構(gòu)造函數(shù)就沒有了
Student(){
}
}
2.3.構(gòu)造方法的調(diào)用
構(gòu)造方法是在實(shí)例化對象時調(diào)用的。并且實(shí)例化時傳遞的參數(shù)必須有構(gòu)造方法的參數(shù)一致。
class Student {
String name;
int score;
String no;
/**
* 有兩個參數(shù)的構(gòu)造方法
* @param name1
* @param score1
*/
Student(String name1,int score1){
name= name1;
score= score1;
}
}
public class StudentTest {
public static void main(String[] args) {
Student s1 =new Student("haha",76);//調(diào)用student類的構(gòu)造方法,為name和score屬性初始化,no為默認(rèn)值null
}
}
構(gòu)造方法不允許通過對象名調(diào)用。例如下面的調(diào)用是錯誤的:
public static void main(String[] args) {
Student s1 =new Student("haha",76);
s1.Student("haha",88);//錯誤的,對象名不允許調(diào)用構(gòu)造函數(shù)
}
2.4.構(gòu)造方法的重載
class Student {
String name;
int score;
String no;
//第1種構(gòu)造方法的重載
Student(String name1,int score1){
name= name1;
score= score1;
}
//第2種構(gòu)造方法的重載
Student(String name1, int score1, String no1) {
name = name1;
score = score1;
no = no1;
}
}
public class StudentTest {
public static void main(String[] args) {
Student s1 =new Student("haha",76);//調(diào)用第1種構(gòu)造方法的重載
Student s2 =new Student("yaya",67,"111");//調(diào)用第2種構(gòu)造方法的重載
}
}
采坑:
這個實(shí)例化為什么報錯?
class Student {
String name;
int score;
String no;
}
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("haha",76); //報錯,為什么?
}
}
分析:因?yàn)閚ew 類時必須調(diào)用構(gòu)造方法,而第8行有兩個參數(shù)haha和76,因此會調(diào)用有兩個參數(shù)的構(gòu)造,但是類中沒有定義有兩個參數(shù)的構(gòu)造,因此報錯。
3.this關(guān)鍵字
this關(guān)鍵字是對一個對象的默認(rèn)引用,即:this代表的是當(dāng)前正在運(yùn)行的對象。
class Student {
String name;
int score;
String no;
Student(String name,int score){
name= name;
score= score;
}
public void sayHello(){
System.out.printf("我是%s,我的成績是%d,我的學(xué)號是%s",name,score,no);
}
}
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("haha",76);
s1.sayHello();
}
}
運(yùn)行結(jié)果:
我是null,我的成績是0,我的學(xué)號是null
分析原因:為什么是null,0,null?
看構(gòu)造函數(shù)
Student(String name,int score){
name= name; //name的值賦給了name,前面的name是誰?后面的name是誰?前后的name都是方法參數(shù)name
score= score; //score的值賦給了score,前面的score是誰?后面的score是誰?前后的score都是方法參數(shù)score
}
原因是
1.前后的name都是方法參數(shù)name
2.前后的score都是方法參數(shù)score
在類中,如果類的屬性名和方法內(nèi)部的局部變量同名時,那么在方法內(nèi)部使用的是局部變量,也就是變量使用遵循就近原則。
解決方法:
String name;
int score;
String no;
Student(String name,int score){
//this代表正在運(yùn)行的對象,當(dāng)執(zhí)行第16行代碼時,調(diào)用了構(gòu)造函數(shù),此時this代表第16行的s1
this.name= name; //this.name指的是對象的屬性,name是指方法的參數(shù)。
this.score= score; //this.score指的是對象的屬性,score是指方法的參數(shù)。
}
public void sayHello(){
System.out.printf("我是%s,我的成績是%d,我的學(xué)號是%s",this.name,this.score,this.no);
}
}
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("haha",76);
s1.sayHello();
}
}
運(yùn)行結(jié)果:
我是haha,我的成績是76,我的學(xué)號是null
3.1.什么時候可以省略this
在非static方法內(nèi)部使用屬性,可以省略。
public void sayHello(){
System.out.printf("我是%s,我的成績是%d,我的學(xué)號是%s",this.name,this.score,this.no);
}
public void sayHello(){
System.out.printf("我是%s,我的成績是%d,我的學(xué)號是%s",name,score,no);
}
兩種結(jié)果相同
4.成員變量
成員變量是類的屬性,直接定義在類內(nèi),方法的外部的變量。
例如:
class Student {
String name;
int score;
String no;
}
4.局部變量
局部變量是定義在方法內(nèi)的變量
例如:
public void sayHello(){
int height = 20;//局部變量
}
成員變量和局部變量的區(qū)別
-
作用域不同
- 成員變量作用域:整個類
- 局部變量的作用域:方法內(nèi)
-
初始值不同
- 成員變量由構(gòu)造函數(shù)初始化的
- 局部變量需要手動初始化
在同一個方法中不允許有同名的局部變量,在不同的方法中可以有同名的局部變量。
局部變量可以和成員變量名相同,并且在使用時局部變量有更高的優(yōu)先級。