静态方法应用-函数库开发
理论:
静态方法应用: *、在工作中,静态方法主要用作函数库。 *、函数库的特征就是一个类的定义中,主要由静态方法组成。 练习1、请使用Math.random()类,请说明为什么这个类可以通过 Math.random()直接调用。 练习2、开发一个MyMath类,提供两个整数的加减乘除余等函数。 函数:即带返回值的方法。 publc class MyMath{ public static int(int a,int b){ return a+b; } //... } 练习3、开发一个MyArray类,请提供以下方法。 2.1、提供打印数组的方法,可以指定分隔符(如,或|) 2.2、传入一个一维整数数组,排序可以传出,可以指定升序或降序。 2.3、传入一个一维整数数组和一个整数,返回这整数在数组中的位置, 如果没有则返回-1。 说明:从现在开始,要养成收集有用的各种方法的习惯,以便后续使用。
main调用:
package com.iit.main; import com.iit.demo.MyMath; public class Start { public static void main(String[] args) { double rnd = Math.random(); System.out.println("随机数="+rnd); int result = MyMath.add(10,20); System.out.println("和="+result); } }
MyMath库:
package com.iit.demo; public class MyMath { public static int add(int a,int b){ return a+b; } //其它方法略。 }
MyArray库:
package com.iit.demo; public class MyArray { /** * 打印数组的内容 * @param src 一维数组 * @param seperator 分隔符,如,或| */ public static void print(int[] src,String seperator){ // } /** * 排序指定的数组 * @param src 一维数组 * @param isAsc true:升序;false:降序 * @return */ public static int[] sort(int[] src,boolean isAsc){ //略 return null; } /** * 查找数组中指定元素的索引,如果没有,则返回-1 * @param src 原数组 * @param findData 要查找的元素 * @return 返回元素在数组中的位置,没有找到则返回-1 */ public static int indexOf(int[] src,int findData){ return -1; } }
本文来自博客园,作者:xiaoyongdata(微信号:xiaoyongdata),转载请注明原文链接:https://www.cnblogs.com/xiaoyongdata/p/16284642.html
浙公网安备 33010602011771号