本日練習:
import java.util.Scanner;
public class cla15_1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double b = sc.nextDouble();
int n = sc.nextInt();
double a = power(b, n);
System.out.println(a);
sc.close();
}
public static double power(double b, int n){
if (n == 0)
return 1;
else
return b * power(b, n - 1);
}
}
public class cla15_2 {
public static void main(String[] args){
System.out.println(max(8,2));
System.out.println(max(1,5,9));
}
public static int max(int a, int b){
if (a > b)
return a;
else
return b;
}
public static int max(int a, int b, int c){
if (a > b && a > c)
return a;
else if (b > a && b > c)
return b;
else
return c;
}
}
本日學習:
在類別中,引數的傳遞與傳回值:
範例:
class Circle{
double radius;
double pi = 3.14;
void setRadius(double r){
radius = r;
}
void showArea(){
System.out.println("Radius = " + radius);
System.out.println("Area = " + pi * radius * radius);
}
}
public class cla15_3 {
public static void main(String[] args){
Circle c1 = new Circle();
c1.setRadius(5);
c1.showArea();
}
}
*函數成員的多載:*在一個類別裡設定同一個函數成員,但是引數不同
公有成員與私有成員( public、private ):