構造方法三個條件:
①、方法名和類名相同
②、在方法名前面沒有返回值類型的聲明
③、在方法中不能使用return語句java
方法重載三個條件: 函數
①、方法名相同,參數類型不一樣
②、參數個數不一樣,參數順序不一樣
③、重載跟方法的返回值類型無關,只跟方法的參數有關this
方法重寫條件:code
①、重寫方法必須和被重寫的方法具備相同的方法名稱,參數列表和返回值字符串
②、重寫方法不能使用比被重寫方法更嚴格的訪問權限class
③、父類中的私有方法不能被重寫,在子類重寫的方法中繼續調用父類被重寫的方法能夠經過調用super.函數名獲取。權限
package package1; public class Demo6 { public static void main(String[] args) { Printer aa=new Printer(1000); aa.print(100); aa.print("hahahahah"); aa.print("hahahaha", 88); } } class Printer{ private String brand="聯想"; private double price; //構造方法 public Printer(double price) { this.price=price; } public Printer(double price,String brand) { this.price=price; this.brand=brand; } //方法的重載 public void print(String content) { System.out.println("字符串"+content); } public void print(int content) { System.out.println("整形"+content); } public void print(String str,int content) { System.out.println(str+"---"+content); } /* * 錯誤的重載方式 * public int print(int content) { return content; }*/ //重載跟方法的返回值類型無關,只跟方法的參數有關 public int print(int content,double d) { return content; } }