scala高级特性
集合:Array所有方法
流:文件读写???
Source.fromFile(File path)
PrintWriter
println
异常机制
Error 系统级异常(硬件级:如内存,网络,硬盘,CPU...)
Exception
直接子类 编译异常:
反射
ClassNotFoundException
IllegalArgmentException
IllegalInvalcationException
线程
InteruptedException
流
FileNotFoundException
IOException
JDBC
SqlException
RuntimeException 运行时异常:
算数
ArithmeticException
NumberFormatException
数组
ArrayIndexOutofBoundException
OOP
ClassCastException
NullPointException
模式匹配:敞亮模式,if守卫,正则守卫,差异化多值守卫,类型守卫
隐式操作
隐式变量 => 为了函数提供隐式参数
def method(name:String)(prefix:String="DEFAULT")={}
implict prefix:String = "DEFAULT"
def method(name:String)(implict prefix:String)={}
隐式函数 => 相当于给函数类型的参数提供默认值
implict def f(name:String):Int={...}
def method(implict f:String=>Int)={}
隐式类型转换 => 可以把数值类型直接转换成其他类型 目的让数据库中的数值自动转换
var a:Int = "123" 会报错
用一下代码转换类型
implicit def fsi(v:String):Option[Int]={
val regexInt = "\\d+".r
if(regexInt.pattern.matcher(v).matches()){
Some(v.toInt)
}else{
Option.empty
}
}
val a:Option[Int] = "123"
println(a)
//隐式类的代码学习 =》我们实现扩展功能的隐式类
implicit class ArrExt[T](arr:Array[T]){
def c()={
if(arr.isInstanceOf[Array[Int]]){ //isInstanceOf 判定 asInstanceOf 转化
arr.asInstanceOf[Array[Int]].sum
}else if(arr.isInstanceOf[Array[String]]){
arr.asInstanceOf[Array[String]].mkString(",")
}else{
throw new RuntimeException("unsupported array type 不支持的数值类型")
}
}
}
//隐式类的测试:
println(Array(2, 5, 9).c())
println(Array("2", "56", "6").c())
//隐式对象
//隐式对象
trait A[T]{ //业务
def combine(arr:Array[T]):T //不空怎么处理
def empty():T //空怎么处理
}
implicit object StrArrA extends A[String] {
override def combine(arr: Array[String]): String = arr.mkString(",")
override def empty(): String = ""
}
implicit object IntArrA extends A[Int] {
override def combine(arr: Array[Int]): Int = arr.sum
override def empty(): Int = 0
}
def combine[T](arr:Array[T])(implicit a:A[T]):T={
if(arr.isEmpty){
a.empty
}else{
a.combine(arr)
}
}
val arrStr2 = Array("aa","bb","cc")
println(combine(arrStr2))
val arrInt = Array(4,7,9)
println(combine(arrInt))
隐式类 => 隐式扩展唯一构造参数指定类型的功能
制作数据
public class DataMaker {
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static Random rand =new Random();
static int day(int year,int month){
int DAY = 31;
switch (month){
case 4:case 6:case 9:case 11:
DAY = 30;
case 2:
DAY = year%4 ==0 && year %100 !=0 || year%400==0 ?29:28;
}
return rand(DAY);
}
static int rand(int...limit){
if(limit.length==1){
return 1+rand.nextInt(limit[0]);
}else{
return limit[0]+rand.nextInt(limit[1]);
}
}
public static void main(String[] args) throws IOException {
Calendar c = Calendar.getInstance();
BufferedWriter bw = new BufferedWriter(new FileWriter("file/sales.txt"));
for (int i = 0; i <100000 ; i++) {
int year = rand(2020,2);
int month = rand(0,12);
int day = day(year,month);
int hour = rand(24);
int minute = rand(60);
int seconds = rand(60);
int shopId = rand(5);
c.set(year,month,day,hour,minute,seconds);
String date = sdf.format(c.getTime());
float saleVolume = rand(2000,20000)/100.0f;
String line = String.format("%d,%s,%.2f", shopId, date, saleVolume);
if(i>0){
bw.newLine();
bw.write(line);
}
}
bw.flush();
bw.close();
}
}
数据:
3,2021-07-01 19:09:38,135.75
1,2021-03-06 10:11:19,69.70
2,2021-09-08 17:01:05,43.51
1,2020-08-30 07:31:02,45.33
3,2020-01-26 01:10:24,55.38
4,2021-02-24 19:41:21,201.54
4,2020-08-25 03:26:13,168.24
3,2021-11-01 02:35:48,60.49
3,2020-03-18 04:50:22,170.29
3,2020-11-12 21:56:29,122.23
1,2020-10-11 19:50:15,205.30
2,2020-04-23 16:38:59,107.63
2,2021-04-30 07:02:50,192.35
。。。n多行
隐式类,用特制去写隐式对象,隐式函数
package cn.kgc.scalajdbc.oop5
object Test {
//隐式类的代码学习 =》我们实现扩展功能的隐式类
implicit class ArrExt[T](arr:Array[T]){
def c()={
if(arr.isInstanceOf[Array[Int]]){ //isInstanceOf 判定 asInstanceOf 转化
arr.asInstanceOf[Array[Int]].sum
}else if(arr.isInstanceOf[Array[String]]){
arr.asInstanceOf[Array[String]].mkString(",")
}else{
throw new RuntimeException("unsupported array type 不支持的数值类型")
}
}
}
def main(args: Array[String]): Unit = {
//隐式函数的代码学习 =》转换数值类型
implicit def fsi(v:String):Option[Int]={
val regexInt = "\\d+".r
if(regexInt.pattern.matcher(v).matches()){
Some(v.toInt)
}else{
Option.empty
}
}
val a:Option[Int] = "123"
println(a)
//隐式类的测试:
println(Array(2, 5, 9).c())
println(Array("2", "56", "6").c())
//隐式对象 用特制去写
trait A[T]{ //业务
def combine(arr:Array[T]):T //不空怎么处理
def empty():T //空怎么处理
}
//隐式对象
implicit object StrArrA extends A[String] {
override def combine(arr: Array[String]): String = arr.mkString(",")
override def empty(): String = ""
}
implicit object IntArrA extends A[Int] {
override def combine(arr: Array[Int]): Int = arr.sum
override def empty(): Int = 0
}
def combine[T](arr:Array[T])(implicit a:A[T]):T={
if(arr.isEmpty){
a.empty
}else{
a.combine(arr)
}
}
val arrStr2 = Array("aa","bb","cc")
println(combine(arrStr2))
val arrInt = Array(4,7,9)
println(combine(arrInt))
}
}
表结构按题要求转变 原表各个店铺每天的销售订单 实现 按天聚合 按月分组 按月 算总的平均值 等
订单文件
3,2021-07-01 19:09:38,135.75
1,2021-03-06 10:11:19,69.70
2,2021-09-08 17:01:05,43.51
1,2020-08-30 07:31:02,45.33
3,2020-01-26 01:10:24,55.38
4,2021-02-24 19:41:21,201.54
4,2020-08-25 03:26:13,168.24
3,2021-11-01 02:35:48,60.49
3,2020-03-18 04:50:22,170.29
3,2020-11-12 21:56:29,122.23
1,2020-10-11 19:50:15,205.30
2,2020-04-23 16:38:59,107.63
2,2021-04-30 07:02:50,192.35
2,2021-12-18 20:15:47,206.04
。。。n多行
店铺文件
1,兰州拉面馆
2,马伍旺
3,邱记水饺
4,肯德基
5,秀兰鸭血粉丝
代码实现
package cn.kgc.scalajdbc.oop5
object Test {
//隐式类的代码学习 =》我们实现扩展功能的隐式类
implicit class ArrExt[T](arr:Array[T]){
def c()={
if(arr.isInstanceOf[Array[Int]]){ //isInstanceOf 判定 asInstanceOf 转化
arr.asInstanceOf[Array[Int]].sum
}else if(arr.isInstanceOf[Array[String]]){
arr.asInstanceOf[Array[String]].mkString(",")
}else{
throw new RuntimeException("unsupported array type 不支持的数值类型")
}
}
}
def main(args: Array[String]): Unit = {
//隐式函数的代码学习 =》转换数值类型
implicit def fsi(v:String):Option[Int]={
val regexInt = "\\d+".r
if(regexInt.pattern.matcher(v).matches()){
Some(v.toInt)
}else{
Option.empty
}
}
val a:Option[Int] = "123"
println(a)
//隐式类的测试:
println(Array(2, 5, 9).c())
println(Array("2", "56", "6").c())
//隐式对象 用特制去写
trait A[T]{ //业务
def combine(arr:Array[T]):T //不空怎么处理
def empty():T //空怎么处理
}
//隐式对象
implicit object StrArrA extends A[String] {
override def combine(arr: Array[String]): String = arr.mkString(",")
override def empty(): String = ""
}
implicit object IntArrA extends A[Int] {
override def combine(arr: Array[Int]): Int = arr.sum
override def empty(): Int = 0
}
def combine[T](arr:Array[T])(implicit a:A[T]):T={
if(arr.isEmpty){
a.empty
}else{
a.combine(arr)
}
}
val arrStr2 = Array("aa","bb","cc")
println(combine(arrStr2))
val arrInt = Array(4,7,9)
println(combine(arrInt))
}
}
输出文件1
2020-01-01 1 4070.38
2020-01-02 1 3284.24
2020-01-03 1 3056.2898
2020-01-04 1 3988.69
2020-01-05 1 2650.24
2020-01-06 1 2547.2
2020-01-07 1 2994.9001
2020-01-08 1 3488.7302
2020-01-09 1 3599.4292
2020-01-10 1 3108.7998
2020-01-11 1 3697.94
2020-01-12 1 3422.7498
2020-01-13 1 2883.79
2020-01-14 1 3391.8105
2020-01-15 1 3137.2402
2020-01-16 1 3745.6904
2020-01-17 1 3689.5198
2020-01-18 1 3560.97
2020-01-19 1 3976.09
。。。n多行
输出文件2
2021-01,5,99523.12,3210.423,99523.12
2021-02,5,93394.69,3335.5247,96458.91
2021-03,5,108576.734,3744.0254,100498.19
2021-04,5,99872.88,3329.0962,100341.86
2021-05,5,110676.805,3816.4417,102408.85
2021-06,5,94321.85,3144.0618,101061.02
2021-07,5,108309.805,3734.8208,102096.56
2021-08,5,98230.47,3168.7249,101613.3
2021-09,5,99646.71,3321.5571,101394.78
2021-10,5,105786.8,3647.8206,101833.984
2021-11,5,97816.68,3260.556,101468.77
2021-12,5,105002.15,3620.7637,101763.22
2020-01,5,103890.18,3351.2961,103890.18
2020-02,5,92272.83,3181.8218,98081.5
2020-03,5,109177.96,3764.7573,101780.32
2020-04,5,92826.0,3094.2,99541.74
2020-05,5,100742.67,3358.089,99781.92
2020-06,5,98766.1,3292.2034,99612.625
2020-07,5,102772.5,3425.75,100064.04
2020-08,5,95376.32,3076.6555,99478.07
2020-09,5,92601.31,3086.7104,98713.984
2020-10,5,104357.52,3478.5842,99278.336
2020-11,5,90807.016,3026.9006,98508.22
2020-12,5,106188.86,3661.6848,99148.27
2021-01,1,96514.59,3113.374,96514.59
2021-02,1,90337.516,3226.3398,93426.055
2021-03,1,110485.414,3809.8418,99112.51
2021-04,1,92886.94,3096.2312,97556.12
2021-05,1,109370.766,3771.4058,99919.05
2021-06,1,97747.445,3258.2483,99557.12
2021-07,1,102675.72,3666.99,100002.625
2021-08,1,100593.7,3244.9583,100076.51
2021-09,1,96342.46,3211.4153,99661.61
2021-10,1,103599.25,3699.9731,100055.375
2021-11,1,92078.336,3069.2778,99330.195
2021-12,1,104216.414,3722.015,99737.375
2020-01,1,100483.836,3241.414,100483.836
2020-02,1,89020.04,3069.6565,94751.94
2020-03,1,102593.47,3537.7058,97365.78
2020-04,1,98790.945,3293.0315,97722.07
2020-05,1,111546.57,3846.4333,100486.97
2020-06,1,103115.05,3437.1682,100924.98
2020-07,1,102143.0,3404.7666,101098.984
2020-08,1,95281.664,3073.602,100371.82
2020-09,1,97041.26,3234.7085,100001.76
2020-10,1,103763.11,3458.7703,100377.89
2020-11,1,95986.6,3199.5535,99978.68
2020-12,1,100829.51,3360.9836,100049.586
2021-01,2,98173.27,3166.88,98173.27
2021-02,2,93734.92,3347.6758,95954.09
2021-03,2,106158.85,3791.3875,99355.68
2021-04,2,101328.6,3377.62,99848.91
2021-05,2,108036.266,3858.438,101486.375
2021-06,2,96230.734,3207.6912,100610.44
2021-07,2,107510.89,3839.6746,101596.21
2021-08,2,102087.27,3293.138,101657.59
2021-09,2,92759.875,3091.9958,100668.96
2021-10,2,100882.7,3478.7139,100690.33
2021-11,2,93290.94,3109.698,100017.66
2021-12,2,100710.45,3596.802,100075.4
2020-01,2,97982.766,3160.7344,97982.766
2020-02,2,92831.7,3201.0933,95407.234
2020-03,2,109609.05,3653.635,100141.164
2020-04,2,96472.24,3215.7415,99223.94
2020-05,2,106721.94,3680.067,100723.54
2020-06,2,98958.555,3298.6184,100429.375
2020-07,2,103683.64,3456.1213,100894.266
2020-08,2,99562.09,3211.6804,100727.75
2020-09,2,100746.664,3358.2222,100729.85
2020-10,2,108486.79,3616.2263,101505.55
2020-11,2,91888.25,3062.9417,100631.25
2020-12,2,108134.18,3604.4727,101256.49
2021-01,3,101623.41,3278.1743,101623.41
2021-02,3,92206.836,3293.1013,96915.125
2021-03,3,116025.95,4000.895,103285.4
2021-04,3,95327.12,3177.5706,101295.83
2021-05,3,105482.81,3637.3384,102133.23
2021-06,3,101840.375,3394.6792,102084.414
2021-07,3,99067.12,3416.1074,101653.375
2021-08,3,99281.61,3202.6326,101356.91
2021-09,3,96316.26,3210.542,100796.836
2021-10,3,104491.72,3731.8472,101166.33
2021-11,3,99405.89,3313.5298,101006.28
2021-12,3,99740.05,3439.312,100900.76
2020-01,3,99185.31,3199.5261,99185.31
2020-02,3,97695.66,3368.8157,98440.484
2020-03,3,105223.664,3507.4556,100701.54
2020-04,3,96724.25,3224.1416,99707.22
2020-05,3,97156.94,3350.2393,99197.164
2020-06,3,97281.54,3242.718,98877.9
2020-07,3,104098.4,3469.9465,99623.68
2020-08,3,102382.01,3302.6455,99968.47
2020-09,3,100612.94,3353.7646,100040.08
2020-10,3,99947.49,3331.583,100030.82
2020-11,3,92611.33,3087.0442,99356.32
2020-12,3,98057.16,3268.5718,99248.055
2021-01,4,90926.87,2933.1248,90926.87
2021-02,4,90893.2,3246.1858,90910.03
2021-03,4,107044.914,3691.2039,96288.32
2021-04,4,95917.72,3197.2573,96195.67
2021-05,4,103659.695,3702.132,97688.48
2021-06,4,95417.91,3180.597,97310.04
2021-07,4,109685.375,3917.3347,99077.945
2021-08,4,96441.086,3111.0027,98748.336
2021-09,4,98022.23,3267.4075,98667.66
2021-10,4,109834.2,3922.6501,99784.31
2021-11,4,96230.0,3207.6667,99461.195
2021-12,4,105023.12,3750.8257,99924.69
2020-01,4,97131.32,3133.2683,97131.32
2020-02,4,91234.66,3146.0227,94182.984
2020-03,4,105128.04,3504.268,97831.336
2020-04,4,98014.984,3267.1663,97877.25
2020-05,4,99741.52,3324.7175,98250.11
2020-06,4,93654.41,3121.8135,97484.16
2020-07,4,97596.93,3253.231,97500.266
2020-08,4,101928.78,3288.0251,98053.83
2020-09,4,89617.125,2987.2375,97116.414
2020-10,4,96323.56,3210.7854,97037.13
2020-11,4,99811.22,3327.0405,97289.32
2020-12,4,105250.94,3508.3645,97952.79