面向對象----方法的重載


方法的重載

方法(函數)

返回值類型 函數名(參數類型 形式參數1,參數類型 形式參數2,….)

  程序代碼
  return 返回值;

其中:
參數類型:就是該形式參數的數據類型。
返回值:方法在執行完畢后返還給調用它的程序的數據。
返回值類型:函數要返回的結果的數據類型。
實參調用函數時實際傳給函數形式參數的數據
形式參數在方法被調用時用於接收外部傳入的數據的變量

 

方法的調用

 


方法的重載

方法的重載就是在同一個類中允許同時存在一個以上的同名方法,只要它們的參數個數或類型不同即可。 如:

 1 public class Test {
 2     public static void main(String [] args) {
 3         int isum;
 4         double fsum;
 5         isum=add(3,5);
 6         isum=add(3,5,6);
 7         fsum=add(3.2,6.5); 
 8     }
 9     public static int add(int x,int y) {
10         reutrn x+y;
11     }
12     public static int add(int x,int y,int z) {
13         return x+y+z;
14     }
15     public static double add(double x,double y){
16         return x+y;
17     }
18 }


重載方法的參數列表必須不同
重載方法的返回值類型可以相同,也可以不同
調用時根據方法的參數類型來區別。


練習
1.編寫程序,定義三個重載方法並調用。方法名為mOL。
三個方法分別接收一個int參數、兩個int參數、一個字符串參數。分別執行平方運算並輸出結果,相乘並輸出結果,輸出字符CallMOL串信息。
在主類()的main方法中分別用參數區別調用三個方法。

 1 public class MethodOverload {
 2 
 3     /**
 4      * 定義三個重載方法並調用。方法名為mOL。
 5      * 三個方法分別接收一個int參數、
 6      * 兩個int參數、
 7      * 一個字符串參數。
 8      * 分別執行平方運算並輸出結果,相乘並輸出結果,輸出字符CallMOL串信息。
 9      */
10     
11     public void mOL(String str){ 
12         System.out.println("CallMOL:" + str);
13     }
14     
15     public  void mOL(int a, int b){
16         System.out.println(a * b);
17     }
18     
19     public void mOL(int a){
20         System.out.println(a * a);
21     }
22     
23 }
 1 public class TestMethodOverload {
 2     public static void main(String[] args) {
 3         
 4         mo.mOL(5);
 5         mo.mOL("abc");
 6         mo.mOL(3, 4);
 7         
 8      }    9 }  


2.定義三個重載方法max,第一個方法求兩個int值中的最大值,第二個方法求兩個double值中的最大值,第三個方法求三個double值中的最大值,並分別調用三個方法。

    /**
    * 定義三個重載方法max,
    * 第一個方法求兩個int值中的最大值,
    * 第二個方法求兩個double值中的最大值,
    * 第三個方法求三個double值中的最大值,並分別調用三個方法。
    */
    public int max(int a, int b) {
        return a > b ? a : b;
    }
    
    public double max(double a, double b) {
        return a > b ? a : b;
    }
    
    public double max(double a, double b, double c) {
        return this.max(max(a, b), c);
    }

 

public class TestMethodOverload {
    public static void main(String[] args) {
            MethodOverload mo = new MethodOverload();

            System.out.println(mo.max(1.1, 3.1));
            System.out.println(mo.max(1, 4, 3.1));
            System.out.println(mo.max(3, 5));    
    }  
}

 

構造方法的重載

構造方法一般用來創建對象的同時初始化對象。如

 

class Person{
  String name;
  int age;
  public Person(String n , int a){

 

     name=n; age=a;

 

  }
}

 

構造方法重載使得對象的創建更加靈活,方便創建各種不同的對象。
構造方法重載舉例:
public class Person{
  public Person(String name, int age, Date d) {this(name,age);}
  public Person(String name, int age) {…}
  public Person(String name, Date d) {…}
  public Person(){…}
}
構造方法重載,參數列表必須不同

構造方法重載舉例

 1 public class Person {
 2   private String name;
 3   private int age;
 4   private Date birthDate;
 5   public Person(String name, int age, Date d) {
 6     this.name = name;
 7     this.age = age;
 8     this.birthDate = d;
 9   }
10       public Person(String name, int age) {
11           this(name, age, null); //this.name=name; this.age=age; 
12         //this.birthDate=null;
13       }
14      public Person(String name, Date d) {
15         this(name, 30, d);    //this.name=name; 
16         this.age=30;this.birthDate=d;
17     }
18     public Person(String name) {
19         this(name, 30);    //this.name=name; this.age=30;
20     }
21 }


練習
(1)定義Person類,有4個屬性:String name; int age; String school; String major,
(2)定義Person類的3個構造方法:
第一個構造方法Person(String n, int a)設置類的name和age屬性;
第二個構造方法Person(String n, int a, String s)設置類的name, age 和school屬性;
第三個構造方法Person(String n, int a, String s, String m)設置類的name, age ,school和major屬性;

 1 public class Person {
 2 
 3     private String name;
 4     private int age;
 5     private String school;
 6     private String major;
 7     
 8     public Person(String n, int a, String s, String m){
 9         this(n, a, s);
10         major = m;
11     }
12     
13     public Person(String n, int a, String s){
14         this(n, a);
15         school = s;
16     }
17     
18     public Person(String n, int a){
19         name = n;
20         age = a;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public int getAge() {
28         return age;
29     }
30 
31     public String getSchool() {
32         return school;
33     }
34 
35     public String getMajor() {
36         return major;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public void setAge(int age) {
44         this.age = age;
45     }
46 
47     public void setSchool(String school) {
48         this.school = school;
49     }
50 
51     public void setMajor(String major) {
52         this.major = major;
53     }    
54 }

 

(3)在main方法中分別調用不同的構造方法創建的對象,並輸出其屬性值。

    Person person = new Person("xiaoye", 1, "xiaoye", "Java");
   
    System.out.println(person.getName());
    System.out.println(person.getAge());
    System.out.println(person.getSchool());
    System.out.println(person.getMajor());

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM