相似功能的函數,以相同的名稱來命名之;透過不同的引數個數,或是不同的型態,
來執行相對應的功能。
class CCircle{
String color;
double pi=3.14;
double radius;
void setColor(String str){
color=str;
System.out.println("顏色= "+color);
}
void setRadius(double r){
radius=r;
System.out.println("半徑= "+radius);
}
void setAll(String str, double r){
color=str;
radius=r;
}
void show(){
System.out.println("顏色= "+color+", 半徑= "+radius);
System.out.println("面積= "+pi*radius*radius);
}
}
public class ch08_4 {
public static void main(String[] args) {
CCircle cake=new CCircle();
cake.setColor("粉紅");
cake.setRadius(5);
cake.setAll("藍色", 2);
cake.show();
}
}
void setColor(String str)
void setRadius(double r)
void setAll(String str, double r)
void setCircle(String str)
void setCircle(double r)
void setCircle(String str, double r)
cake.setCircle("粉紅");
cake.setCircle(5);
cake.setCircle("藍色", 2);
class CCircle2{
String color;
double pi=3.14;
double radius;
void setCircle(String str){
color=str;
System.out.println("顏色= "+color);
}
void setCircle(double r){
radius=r;
System.out.println("半徑= "+radius);
}
void setCircle(String str, double r){
color=str;
radius=r;
}
void show(){
System.out.println("顏色= "+color+", 半徑= "+radius);
System.out.println("面積= "+pi*radius*radius);
}
}
public class ch08_4_2 {
public static void main(String[] args) {
CCircle2 cake=new CCircle2();
cake.setCircle("粉紅");
cake.setCircle(5);
cake.setCircle("藍色", 2);
cake.show();
}
}
ex.
void setCircle(double r)
int setCircle(double r)