类方法(Function)

(1)枚举类型enum

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * Java 创建枚举类型要使用 enum 关键字,隐含了所创建的类型都是 java.lang.Enum 类的子类
 5  */
 6 
 7 enum Car {
 8     lamborghini, fiveLight, audi, fiat, honda
 9 }
10 public class EnumAndSwitch {
11     public static void main(String[] args){
12         Car c;
13         c = Car.fiveLight;
14         switch (c){
15             case lamborghini:
16                 System.out.println("你选择了 lamborghini");
17                 break;
18             case fiveLight:
19                 System.out.println("你选择了 fiveLight");
20                 break;
21             case audi:
22                 System.out.println("你选择了 audi");
23                 break;
24             case fiat:
25                 System.out.println("你选择了 fiat");
26                 break;
27             case honda:
28                 System.out.println("你选择了 honda");
29                 break;
30                 default:
31                     System.out.println("我不知道你的车型...");
32                     break;
33         }
34     }
35 }
View Code

(2)枚举类型构造函数的使用

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * Enum(枚举)构造函数及方法的使用
 5  */
 6 
 7 enum NewCar{
 8     lamborghini(900), tata(2), audi(50), fiat(15), honda(12);
 9     private int price;
10     NewCar(int p){
11         price = p;
12     }
13     int getPrice(){return price;}
14 }
15 public class EnumFunction {
16     public static void main(String[] args){
17      System.out.println("所有汽车价格:");
18      for (NewCar c:NewCar.values())
19          System.out.println(c + "需要" + c.getPrice() + "千美元");
20     }
21 }
View Code

(3)阶乘计算

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 阶乘
 5  */
 6 public class Factorial {
 7     public static void main(String[] args){
 8         for (int counter = 0; counter <= 10; counter++){
 9             System.out.printf("%d! = %d\n", counter, factorial(counter));
10         }
11     }
12     public static long factorial(long number){
13         if (number <= 1) return 1;
14         else return number * factorial(number - 1);
15     }
16 }
View Code

(4)斐波那契数列

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 斐波那契数列:
 5  * 第0项是0,第1项是第一个1
 6  * 这个数列从第三项开始,每一项都等于前两项之和
 7  */
 8 public class Fibonacci {
 9     public static void main(String[] args){
10         for (int counter = 0; counter <= 10; counter++){
11             System.out.printf("Fibonacci of %d is : %d\n", counter, fibonacci(counter));
12         }
13     }
14 
15     public static long fibonacci(long number) {
16         if ((number == 0 ) || (number == 1)){
17             return number;
18         }else {
19             return fibonacci(number - 1) + fibonacci(number - 2);
20         }
21     }
22 }
View Code

(5)foreach循环遍历

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * foreach语句是java5的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便
 5  */
 6 public class ForAndForeach {
 7     public static void main(String[] args){
 8      int[] intary = {1, 2, 3, 4};
 9      forDisplay(intary);
10      foreachDisplay(intary);
11     }
12     public static void forDisplay(int[] a){
13         System.out.println("使用 for 循环数组: ");
14         for (int i = 0; i < a.length; i++){
15             System.out.print(a[i] + " ");
16         }
17         System.out.println();
18     }
19     public static void foreachDisplay(int[] data){
20         System.out.println("使用 foreach 循环数组: ");
21         for (int a:data)
22             System.out.print(a + " ");
23     }
24 }
View Code

(6)方法覆盖

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 方法重载与方法覆盖区别如下:
 5  * 方法重载(Overloading):
 6  *      如果有两个方法的方法名相同,但参数不一致,哪么可以说一个方法是另一个方法的重载
 7  * 方法覆盖(Overriding):
 8  *      如果在子类中定义一个方法,其名称、返回类型及参数签名正好与父类中某个方法的名称、返回类型及参数签名相匹配,
 9  *      那么可以说,子类的方法覆盖了父类的方法
10  */
11 public class FunctionCover {
12     public static void main(String[] args){
13         Figure f = new Figure(10, 10);
14         Rectangle r = new Rectangle(9, 5);
15         Figure figure;
16         figure = f;
17         System.out.println("Area is : " + figure.area());
18         figure = r;
19         System.out.println("Area is : " + figure.area());
20     }
21 }
22 
23 class Figure{
24     double dim1;
25     double dim2;
26     Figure(double a, double b){
27         dim1 = a;
28         dim2 = b;
29     }
30     Double area(){
31         System.out.println("Inside area for figure.");
32         return (dim2 * dim1);
33     }
34 }
35 
36 class Rectangle extends Figure{
37     Rectangle(double a, double b){
38         super(a, b);
39     }
40     Double area(){
41         System.out.println("Inside area for rectangle.");
42         return (dim1 * dim2);
43     }
44 }
View Code

(7)方法重载

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 方法重载(Overloading)定义:
 5  * 如果有两个方法的方法名相同,但参数不一致,哪么可以说一个方法是另一个方法的重载
 6  *
 7  * 原则:
 8  * 1 方法名相同
 9  * 2 方法的参数类型,个数顺序至少有一项不同
10  * 3 方法的返回类型可以不相同
11  * 4 方法的修饰符可以不相同
12  * 5 main方法也可以被重载
13  */
14 public class FunctionLoad {
15     public static void main(String[] args) {
16         MyClass t = new MyClass(3);
17         t.info();
18         t.info("重载方法\n");
19         //重载构造函数
20         new MyClass();
21     }
22 }
23 
24 
25 class MyClass{
26     int height;
27     MyClass(){
28         System.out.println("无参构造函数");
29         height = 4;
30     }
31     MyClass(int i){
32         System.out.println("房子高度为:" + i + "米");
33         height = i;
34     }
35     void info(){
36         System.out.println("房子高度为 " + height + "米");
37     }
38     void info(String s){
39         System.out.println(s + "房子高度为" + height + "米");
40     }
41 }
View Code

(8)汉诺塔

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 汉诺塔游戏,玩法如下:
 5  * 1 有三根杆子A,B,C
 6  * 2 A杆上有若干碟子
 7  * 3 每次移动一块碟子,小的只能叠在大的上面
 8  * 4 把所有碟子从A杆全部移到 C 杆上
 9  */
10 public class HanNuoTower {
11     public static void main(String[] args) {
12         int nDisks = 3;
13         doTowers(nDisks, 'A', 'B', 'C');
14     }
15 
16     public static void doTowers(int topN, char from, char inter, char to) {
17         if (topN == 1) {
18             System.out.println("Disk 1 from " + from + " to " + to);
19         } else {
20             doTowers(topN - 1, from, to, inter);
21             System.out.println("Disk " + topN + " from " + from + " to " + to);
22             doTowers(topN - 1, inter, from, to);
23         }
24     }
25 }
View Code

(9)标签Label

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * Java 中的标签是为循环设计的,是为了在多重循环中方便的使用break 和 continue
 5  */
 6 public class Label {
 7     public static void main(String[] args){
 8         String strSearch = "This is the string in which you have to search for a substring.";
 9         String subString = "substring";
10         boolean found = false;
11         int max = strSearch.length() - subString.length();
12         testLabel:
13             for (int i = 0; i <= max; i++){
14                 int length = subString.length();
15                 int j = i;
16                 int k = 0;
17                 while (length-- != 0){
18                     if (strSearch.charAt(j++) != subString.charAt(k++)){
19                         continue testLabel;
20                     }
21                 }
22                 found = true;
23                 break testLabel;
24             }
25             if(found){
26                 System.out.println("发现子字符串...");
27             }else {
28                 System.out.println("字符串中未发现子字符串...");
29             }
30     }
31 }
View Code

(10)通过重载方法输出不同类型数组

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 通过重载 OutputArray 类的 printArray 方法输出不同类型(整型, 双精度及字符型)的数组
 5  */
 6 public class OutputArray {
 7     public static void printArray(Integer[] inputArray){
 8         for (Integer element:inputArray){
 9             System.out.printf("%s", element);
10             System.out.println();
11         }
12     }
13     public static void printArray(Double[] inputArray){
14         for (Double element:inputArray){
15             System.out.printf("%s", element);
16             System.out.println();
17         }
18     }
19     public static void printArray(Character[] inputArray){
20         for (Character element:inputArray){
21             System.out.printf("%s", element);
22             System.out.println();
23         }
24     }
25     public static void main(String[] args){
26         Integer[] interArray = {1, 2, 3, 4, 5};
27         Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
28         Character[] charArray = {'J', 'A', 'V', 'A'};
29         System.out.println("输出整形数组 : ");
30         printArray(interArray);
31         System.out.println("\n输出双精度数组 : ");
32         printArray(doubleArray);
33         System.out.println("\n输出字符数组 : ");
34         printArray(charArray);
35     }
36 
37 }
View Code

(11)可变参数

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 在重载方法中使用可变参数
 5  */
 6 public class OvreloadingOfVarargs {
 7     static void vaTest(int... no) {
 8         System.out.println("vaTest(int...):" + "参数个数:" + no.length + "内容:");
 9         for (int n : no)
10             System.out.print(n + " ");
11         System.out.println();
12     }
13 
14     static void vaTest(boolean... bl) {
15         System.out.println("vaTest(boolean...):" + "参数个数:" + bl.length + "内容:");
16         for (boolean n : bl)
17             System.out.print(n + " ");
18         System.out.println();
19     }
20 
21     static void vaTest(String msg, int... no) {
22         System.out.println("vaTest(String, int...):" + msg + "参数个数:" + no.length + "内容:");
23         for (int n : no)
24             System.out.print(n + " ");
25         System.out.println();
26     }
27 
28     public static void main(String[] args) {
29         vaTest(1, 2, 3);
30         vaTest("测试", 10, 20);
31         vaTest(true, false, false);
32     }
33 }
View Code

(12)实参个数可变

 1 package JavaEE.JavaBaseExampleTest.Functions;
 2 
 3 /**
 4  * 定义实参个数可变的方法:只要在一个形参的"类型"与"参数名"之间加上三个连续的"."
 5  * (即"...",英文里的句中省略号),就可以让它和不确定个实参相匹配
 6  */
 7 public class Varargs {
 8     static int sumVarargs(int... intArrays){
 9         int sum,i;
10         sum = 0;
11         for (i = 0; i < intArrays.length; i++){
12             sum += intArrays[i];
13         }
14         return (sum);
15     }
16     public static void main(String[] args){
17         int sum = 0;
18         sum = sumVarargs(new int[]{10, 12, 33, 45});
19         System.out.println("数字相加之和 :" + sum);
20     }
21 }
View Code

猜你喜欢

转载自www.cnblogs.com/skygrass0531/p/12299329.html