训练营-4月

一、环境

1、idea安装

idea是开发工具,写代码的地方

目录结构:
---项目(工程 project)		比如:京东
----模块(module)				比如:购物车、秒杀、订单等
-----包(package)				比如:com.jd
------类(class)					比如:用户类、购物车类、订单类等
-------代码(方法、属性等)

2、jdk安装

三个检测命令
java
javac
java -version
(1)安装jdk,同时也会安装jre
(2)配置系统环境变量
①系统变量下 -> 新建JAVA_HOME
②系统变量下 -> 双击编辑Path

注:jdk是java开发环境,jre是java运行环境

package com.p1;

//创建了一个类,类名Test1
public class Test1 {
    //main方法,程序运行的入口
    public static void main(String args[]) {
        //在控制台打印一段话:这是我写的第一个Java测试类,类名为Test1
        System.out.println("这是我写的第一个Java测试类,类名为Test1");
    }
}

二、数据类型

1、基本数据类型
(1)整型:存整数,默认是int
①byte
存储范围:-128~127
②short
存储范围:-32768~32767
③int
存储范围:-2147483648~2147483647
④long
存储的数超过了int范围,需要在变量值后面加后缀L(l)

(2)浮点型:存小数,默认是double
①float
需要加后缀F(f)
②double

(3)布尔型:存true或者false
boolean

(4)字符型:存单个字符(一个中文汉字或者一个英文字母或者一个数字[0,65535] )
char

注:当char存储一个数字时,会输出对应的字符(基于ASCII码表得到的)

从大类型到小类型:double > float > long > int > short > byte

基本数据类型转换:
①强制转换:大给小

格式:
byte b1 = (byte)128;//大给小
小类型 = (小类型或者比左边还要小的类型)大类型的值;

注:强转是有风险的,可能会造成数据精度丢失,会出现问题,因此要慎用

②自动转换:隐式转换,小给大

格式:
大类型 = 小类型的值
package com.p1;

/**
 * 测试基本数据类型
 */
public class Test2 {
    public static void main(String[] args) {
        //定义变量,     格式:数据类型 变量名 = 变量值;
        byte b1 = -128;//[-128,127]
        short s1 = 32767;//[-32768,32767]
        int i1 = 2147483647;//[-2147483648,2147483647]
        long l1 = 125;
        System.out.println(b1);
        System.out.println(s1);
        System.out.println(i1);
        System.out.println(l1);

        long l2 = 2147483648l;

        float f1 = 5.88F;
        double d1 = 5.88;

        boolean b2 = true;
        boolean b3 = false;

        char c1 = '达';
        System.out.println(c1);
        char c2 = 'a';
        System.out.println(c2);
        char c3 = 98;//由于ASCII码表
        System.out.println(c3);
    }
}
package com.p1;

/**
 * 测试数据类型转换
 * 从大到小:double > float > long > int > short > byte
 */
public class Test3 {
    public static void main(String[] args) {
        byte b1 = (byte)128;//大给小
        System.out.println(b1);

        short s1 = (short)37269;//short = (short/byte)int
        System.out.println(s1);


        int i1 = 127;//小给大
    }
}

2、引用类型:除了基本数据类型以外的

三、运算符

1、算术运算符:

+:有双重含义,一种是正常的加法运算,还有一种是字符串拼接
-*/++--%:

注:+-*/ 作浮点类型运算时,可能会出现精度丢失,数据错误等问题
package com.p1;

/**
 * 测试算术运算符
 */
public class Test4 {
    public static void main(String[] args) {
        //第一种:正常的加法运算
        byte b1 = 5;
        byte b2 = 10;
        byte b3 = (byte)(b1+b2);//基本数据类型之间做加法运算时,运算结果的类型是根据最大的那个类型得来的
        System.out.println(b3);//15

        //第二种:字符串拼接
        int age = 31;
        System.out.println("小明的年龄是"+age);

        //测试减法
        double d1 = 15.0;
        double d2 = 8.3;
        System.out.println(d1-d2);//出现数据精度丢失问题


        //测试++和--
        short s1 = 10;
        short s3 = s1++;//先赋值,再运算 s1++=10,s1=11
        System.out.println(s3);//10
        System.out.println(s1);//11

        short s4 = 10;
        short s6 = ++s4;//先运算,再赋值
        System.out.println(s6);//11
        System.out.println(s4);//11

        byte b4 = 20;
        byte b5 = b4--;//20
        System.out.println(b5);//20
        System.out.println(b4);//19

        byte b6 = 20;
        byte b7 = --b6; //19
        System.out.println(b7);//19
        System.out.println(b6);//19

        //测试%
        long l1 = 10;
        long l2 = 3;
        long l3 = -2;
        //大取小,求余数
        long l4 = l1%l2;//1
        System.out.println(l4);
        //小取大,直接得到最小的那个数
        long l5 = l3%l1;//-2
        System.out.println(l5);
    }
}

2、关系运算符:

>
>=
<
<=
==
!=
package com.p1;

/**
 * 测试关系运算符
 */
public class Test5 {
    public static void main(String[] args) {
        int i1,i2,i3,i4,i5,i6;
        boolean b1,b2,b3;
        i1 = -50;
        i2 = 37;
        i3 = -28;
        i4 = 99;
        i5 = 101;
        b1 = i1 >=i3;//false
        System.out.println(b1);
        b2 = i2 == i4;//false
        System.out.println(b2);
        b3 = i2 != i5;//true
        System.out.println(b3);
    }
}

3、逻辑运算符

&&():一假则假,全真则真
||():一真则真,全假则假
!():取反,如果是真,取反后的结果是假,如果是假,取反后的结果是真
package com.p1;

/**
 * 测试逻辑运算符
 */
public class Test6 {
    public static void main(String[] args) {
        int i1,i2,i3,i4;
        i1 = 20;
        i2 = -15;
        i3 = 27;
        i4 = 18;

        //两边为真,则为真,一边为假,则为假
        System.out.println((i1>i3) && (i2<i4));//false = false&&true

        //两边为假,则为假,一边为真,则为真
        System.out.println((i2>i3) || (i1>i4));//true = false||true

        System.out.println(!(i2>i3));//true
    }
}

4、赋值运算符

=
+=
-=
*=
/=
%=
package com.p1;

/**
 * 测试赋值运算符
 */
public class Test7 {
    public static void main(String[] args) {
        int i1,i2,i3,i4;
        i1 = 10;
        i2 = 2;
        i3 = 3;
        i4 = 20;

        i1+=i2;//<=> i1=i1+i2;
        System.out.println(i1);//12

        i4%=i3;//<=> i4=i4%i3;
        System.out.println(i4);//2
    }
}

5、三元(三目或者条件)运算符

a>b?c表达式:d表达式;
package com.p1;

/**
 * 测试条件运算符
 */
public class Test8 {
    public static void main(String[] args) {
        int a,b;
        a = 10;
        b = 20;

        int max = a > b ? a: b;
        System.out.println(max);//20
    }
}

四、分支结构和循环结构

1、分支结构

if(boolean) {} else {}

if(boolean) {} else if(boolean) {} else {}

switch(byte/short/int/char/String/enum) {
	case value1:
	case value2:
	case value3:
	default:
}

没有break的情况下,会一直匹配,直到结束
有break的情况下,从匹配处开始,一直到有break的地方停止
default是当没有匹配到的时候才会走
package com.p1;

/**
 * 测试分支结构
 */
public class Test9 {
    public static void main(String[] args) {
        //第一种
        double score = 88;
        if(score > 90) {
            System.out.println("奖励一台ipad");
        } else {
            System.out.println("什么奖励都没有");
        }

        //第二种
        double mathScore = 73;
        if(mathScore>=90 && mathScore <= 100) {
            System.out.println("你的成绩是优秀");
        } else if(mathScore>=80) {
            System.out.println("你的成绩是良");
        } else if(mathScore>=70) {
            System.out.println("你的成绩是一般");
        } else if(mathScore>=60) {
            System.out.println("你的成绩是及格");
        } else {
            System.out.println("你的成绩不及格");
        }

        //第三种
        int b1 = 100;
        switch (b1) {
            case 20:
                System.out.println("case 20");
            case 30:
                System.out.println("case 30");
            case 10:
                System.out.println("case 10");
            case 50:
                System.out.println("case 50");
            case 40:
                System.out.println("case 40");
            default:
                System.out.println("default");
        }
    }
}

2、循环结构

for循环:
for(①变量定义初始化;②判断表达式;④表达式) {
	③循环体及代码
}

while循环:
while(判断表达式) {
	循环体及代码
}

do while循环:
do {
	循环体及代码
} while(判断表达式);
package com.p1;

/**
 * 三种循环
 */
public class Test10 {
    public static void main(String[] args) {
        //for循环:从1打印到10
        for(int i = 1;i < 11;i++) {
            System.out.println(i);
        }

        System.out.println("---while---");
        //while循环:从1打印到10
        int i = 1;
        while(i < 11) {
            System.out.println(i);
            i++;
        }

        //do while循环:从1打印到10
        System.out.println("---do while---");
        int j = 1;
        do {
            System.out.println(j);
            j++;
        } while(j < 11);
    }
}
package com.p1;

/**
 * 测试循环
 */
public class Test11 {
    public static void main(String[] args) {
        //for循环:分别找出1-100里面的奇数和偶数并输出
        for(int i = 1;i < 101;i++) {
            if(i%2==0) {//偶数
                System.out.println("偶数:" + i);
            } else {//奇数
                System.out.println("奇数:" + i);
            }
        }

        //while循环
        System.out.println("---while---");
        int i = 1;
        while(i < 101) {
            if(i%2==0) {//偶数
                System.out.println("偶数:" + i);
            } else {//奇数
                System.out.println("奇数:" + i);
            }
            i++;
        }

        //do while
        System.out.println("---do while---");
        int j = 1;
        do {
            if(j%2==0) {//偶数
                System.out.println("偶数:" + j);
            } else {//奇数
                System.out.println("奇数:" + j);
            }
            j++;
        }while(j < 101);

        //do while循环
    }
}
package com.p1;

/**
 * 嵌套循环-乘法口诀表
 */
public class Test12 {
    public static void main(String[] args) {
        for (int j = 1; j < 10; j++) {
            for(int i = 1;i < j+1;i++) {
                System.out.print(i+"*"+j+"="+i*j + " ");
            }
            System.out.println();
        }

//        for(int j = 1;j < 10;j++) {
//            for(int i = 1;i <= j;i++) {
//                System.out.print(i + "*" + j + "=" + i*j + " ");
//            }
//            System.out.println();
//        }
    }
}
package com.p1;

/**
 * 测试循环
 */
public class Test13 {
    public static void main(String[] args) {
        //案例1:输出1-100之间是3的倍数的数
        //第一种:for
        System.out.println("for");
        for(int i = 1;i < 101;i++) {
            if(i%3==0) {
                System.out.print(i + " ");
            }
        }

        //第二种:while
        System.out.println();
        System.out.println("while");
        int j = 1;
        while(j < 101) {
            if(j%3==0) {
                System.out.print(j + " ");
            }
            j++;
        }

        //第三种:do while
        System.out.println();
        System.out.println("do while");
        int i = 1;
        do{
            if(i%3==0) {
                System.out.print(i + " ");
            }
            i++;
        } while(i < 101);

        System.out.println();
        System.out.println("for");
        //案例2:输出1到100之间以3结尾的数
        //第一种:for
        for (int k = 1; k < 101; k++) {
            if(k%10==3) {
                System.out.print(k + " ");
            }
        }
        //第二种:while
        System.out.println();
        System.out.println("while");
        int num1 = 1;
        while(num1 < 101) {
            if(num1%10==3) {
                System.out.print(num1 + " ");
            }
            num1++;
        }

        //第三种:do while
        System.out.println();
        System.out.println("do while");
        int num2 = 1;
        do {
            if(num2%10==3) {
                System.out.print(num2 + " ");
            }
            num2++;
        } while(num2 < 101);
    }
}
package com.p1;

/**
 * 死循环
 */
public class Test14 {
    public static void main(String[] args) {
        //for
//        for(;;) {
//            System.out.println("for的死循环");
//        }
//        for(int i = 1;i > 0;i++) {
//            System.out.println("for的死循环2");
//        }

        //while
//        while(true) {
//            System.out.println("while的死循环1");
//        }
        int num1 = 1;
        while(num1 > 0) {
            System.out.println("while的死循环2");
            num1++;
        }

        //do while
//        do {
//            System.out.println("do while的死循环1");
//        } while(true);

        int num2 = 0;
        do {
            System.out.println("do while的死循环2");
            num2++;
        } while(num2 > -1);
    }
}
package com.p1;

/**
 * 循环打印三角形
 */
public class Test15 {
    public static void main(String[] args) {
        //*
        //**
        //***
        //****
        //*****
        //for:
        for(int j = 1;j < 6;j++) {
            for(int i = 1;i <= j;i++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println();

        //    *
        //   **
        //  ***
        // ****
        //*****
        for(int j = 1;j < 6;j++) {
            for (int i = 5; i > j; i--) {
                System.out.print(" ");
            }
            for(int i = 1;i <= j;i++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println();

        //*****
        //****
        //***
        //**
        //*
        for (int j = 1; j < 6; j++) {
            for (int i = 6; i > j; i--) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println();

        //*****
        // ****
        //  ***
        //   **
        //    *
        for (int j = 1; j < 6; j++) {
            for (int i = 1; i < j; i++) {
                System.out.print(" ");
            }
            for (int i = 6; i > j;i--) {
                System.out.print("*");
            }
            System.out.println();
        } }
}

五、数组(Array)

看作是一个容器,用来存放同一种类型的多个数据

1、创建方式

(1)静态

①数据类型[] 数组的名字 = new 数据类型[]{元素1,元素2,元素3.....};

②数据类型[] 数组的名字 = {元素1,元素2,元素3.....};

(2)动态

数据类型[] 数组的名字 = new 数据类型[数组的长度];

注:
①数组的长度=数组元素的个数=数组名.length
②数组的元素获取方式:通过下标,下标范围[0,数组的长度-1]

格式:
数组的类型 变量名 = 数组名[数组的下标]
package com.p1;

/**
 * 数组的创建
 */
public class Test16 {
    public static void main(String[] args) {
        //静态:定义一个byte类型的数组arr1
        byte[] arr1 = new byte[]{1,3,5,7,9};
        System.out.println(arr1.length);//[0,4]
        byte b1 = arr1[2];//5
        System.out.println("b1: " + b1);

        //静态:定义一个short类型的数组arr2
        short[] arr2 = {2,4,6,8,10};
        System.out.println(arr2.length);//[0,4]
        short s1 = arr2[0];
        short s2 = arr2[1];
        short s3 = arr2[2];
        short s4 = arr2[3];
        short s5 = arr2[4];
        System.out.println("数组arr2的值如下:");
        System.out.print(s1 + " ");
        System.out.print(s2 + " ");
        System.out.print(s3 + " ");
        System.out.print(s4 + " ");
        System.out.print(s5 + " ");
        System.out.println();

        //动态:定义一个int类型的数组arr3
        int[] arr3 = new int[10];
        System.out.println(arr3.length);//[0,9]
        System.out.println(arr3[0]);
        System.out.println(arr3[3]);
        System.out.println(arr3[6]);
    }
}

2、数组的遍历

package com.p1;

/**
 * 数组的赋值和遍历
 */
public class Test17 {
    public static void main(String[] args) {
        //定义一个long类型数组larr1,长度是5
        long[] larr1 = new long[5];

        //for遍历larr1
        System.out.print("larr1赋值前遍历的结果:");
        for(int i = 0;i < larr1.length;i++) {
            System.out.print(larr1[i] + " ");
        }

        //while给larr1赋值3,5,7,9,11
        int num1 = 2;
        int index = 0;
        while(num1 < 12) {
            if(num1%2!=0) {
                larr1[index] = num1;
                index++;
            }
            num1++;
        }

        System.out.println();
        System.out.print("larr1赋值后遍历的结果:");

        //do while遍历larr1
        int j = 0;
        do {
            System.out.print(larr1[j] + " ");
            j++;
        } while(j < larr1.length);
    }
}
package com.p1;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * 案例:验证码
 */
public class Test18 {
    public static void main(String[] args) {
        //1、生成验证码
        char[] arr1 = new char[4];

        //生成随机数的两种方式
        //第一种:new Random()
        Random random = new Random();
//        int randomValue1 = random.nextInt();//int的范围

        int index = 0;
        while(true) {
            if(index==4) {
                break;//跳出循环的关键字
            }
            int randomValue1 = random.nextInt(123);
            if(randomValue1 >= 97 && randomValue1 <=122) {
                arr1[index] = (char)randomValue1;
                index++;
            } else if(randomValue1 >= 65 && randomValue1 <= 90){
                arr1[index] = (char)randomValue1;
                index++;
            } else if(randomValue1 >= 48 && randomValue1 <=57) {
                arr1[index] = (char)randomValue1;
                index++;
            }
        }
        //0-9    48-57
        //a-z    97-122
        //A-Z    65-90

//        for (int i = 0; i < arr1.length; i++) {
//            System.out.print(arr1[i] + " ");
//        }
        System.out.println(Arrays.toString(arr1));

        //第二种方式:Math.random()
//        int randomValue2 = (int)Math.random();//[0,1)

        //2、用户输入验证码
        String userInput = new Scanner(System.in).next();

        //3、进行比对,成功就进入系统,失败就不允许访问
        int index1 = 0;
        for (int i = 0;i < arr1.length;i++) {
            if(arr1[i]==userInput.charAt(i)) {//代表相等
                index1++;
            }
        }

        //4:30

        if(index1==4) {
            System.out.println("欢迎进入本系统!");
        } else {
            System.out.println("验证码输入错误");
        }
    }
}

六、方法

(1)静态方法

格式:
访问权限修饰符 static 返回值类型 方法名(方法的形参) {
	//方法体
}

①访问权限修饰符:public > protected > default > privatestaticstatic只能调用static的,不能调用非static的;非static下面既能调用static,也能调用非static的
③返回值类型:void/基本数据类型/引用类型
return关键字:一般用于结束方法,也可以去返回一个值
package com.p1;

import java.util.Arrays;

public class Test20 {
    public static void main(String[] args) {
        m1();

        //调用m3方法
        m3();
        System.out.println("m3方法的返回值:" + m3());//直接打印m3方法的返回值
        int num1 = m3();//定义num1变量来接收m3方法的返回值
        System.out.println(num1);

        //调用m4方法并用num2变量去接收该方法的返回值
        byte num2 = m4();
        System.out.println("m4方法的返回值" + num2);

        //调用m5方法,并传入两个int参数
        int A,B;
        A = 18;
        B = 28;
        int result = m5(A,B);
        System.out.println("m5方法的返回值:" + result);

        //调用m6方法,传入两个int参数和两个int数组
        int[] a1 = new int[50];
        int[] a2 = new int[50];
        int[] arr2 = m6(1,101,a1,a2);
        System.out.println("奇数数组:" + Arrays.toString(arr2));
    }

    //静态方法
    public static void m1() {
        System.out.println("调用了m1方法");
    }

    //普通方法
    public static void m2() {
        System.out.println("调用了m2方法");
    }

    //定义静态方法m3()
    public static int m3() {
        return 10;
    }

    //定义静态方法m4()
    public static byte m4() {
        byte a = 10;
        byte b = 20;
        return (byte)(a+b);
    }

    //定义静态方法 int m5(int num1,int num2)
    public static int m5(int num1,int num2) {
        int num3 = num1+num2;
        return num3;
    }

    //定义静态方法 int[] m6(int n1,int n2,int[] arr1,int arr2)
    public static int[] m6(int n1,int n2,int[] arr1,int[] arr2) {
        int index1 = 0;
        int index2 = 0;
        for (int i = n1; i < n2; i++) {
            if(i%2==0) {
                arr1[index1] = i;
                index1++;
            } else {
                arr2[index2] = i;
                index2++;
            }
        }
        System.out.println("偶数数组:" + Arrays.toString(arr1));
        return arr2;
    }
}

(2)构造方法
作用:用于构建对象(创建类的实例)

格式:
访问权限修饰符 方法名(方法的形参) {
	//方法体
}

①方法名要和类名一样
②它没有void,也没有返回值
③构造方法分两种: 无参构造(默认就有)、含参构造(自己声明)
无参构造隐式存在,但是如果一个类中声明了含参构造(1个或者多个),默认的无参构造会被覆盖,如果想要使用无参构造,需要显式声明
package com.p1;

public class Student {
    //定义一些类的属性
    int stuNumber = 1001;//学号
    int age = 23;//年龄
    char address = '沪';//籍贯,住址
    double mathScore = 88;//数学成绩
    double englishScore = 90;//英语成绩

    //无参构造
    public Student() {
        System.out.println("调用了Student的无参构造...");
    }
}
package com.p1;

public class Test21 {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println(student.stuNumber);//学号
    }
}

(3)普通方法
一般通过对象点的方式去调用

格式:
访问权限修饰符 void/基本数据类型/引用类型 方法名(方法的形参) {
	//方法体
}
package com.p1;

public class Teacher {
    //定义普通方法 void m1();
    public void m1() {
        System.out.println("执行了m1方法...");
    }

    //定义普通方法 int m2(int a,int b);
    public int m2(int a,int b) {
        System.out.println("执行了m2方法...");
        return a+b;
    }

    //定义普通方法 int[] m3(int n1,int n2,int[] arr1)
    public int[] m3(int n1,int n2,int[] arr1) {
        return m4(n1,n2,arr1);
    }

    //定义静态方法m4
    public static int[] m4(int num1,int num2,int [] arr2) {
        int index = 0;
        for (int i = num1; i < num2; i++) {
            if(i%10==3) {
                arr2[index] = i;
                index++;
            }
        }
        return arr2;
    }
}
package com.p1;

import java.util.Arrays;

/**
 * 测试Teacher类
 */
public class Test22 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.m1();
        int result1 = teacher.m2(5,10);
        System.out.println("m2方法的返回值:" + result1);

        int[] arr = new int[10];
        int[] newArr = teacher.m3(1,101,arr);
        System.out.println("m3方法的返回值:" + Arrays.toString(newArr));
    }
}

七、面向对象

1、封装
关键字:private
作用:提高程序的安全性,不被外界所访问

package com.p1;

/**
 * 测试封装
 */
public class Test23 {
    public static void main(String[] args) {
        Sub sub1 = new Sub();
        System.out.println(sub1.age);
    }
}

class Sub {
    int id = 1001;//工号
    int age = 31;//年龄
    private double money = 500000;//薪资

    //查询工资
    private void selectSalary() {
        System.out.println(money);
    }
}

2、继承
关键字:extends
作用:可以继承父类的一些非私有化的东西,继承具有传递性

①java里仅支持单继承
②我们可以对继承过来的东西去进行重写
重写:方法名相同,返回值类型必须相同,方法的参数类型、列表的个数也得相同
package com.p1;

/**
 * 测试继承
 */
public class Test24 {
    public static void main(String[] args) {
        Sub1 sub = new Sub1();
        System.out.println(sub.salary);
    }
}

//父类(超类)
class Super1 {
    private int id = 3001;//工号
    private int age = 45;//年龄
    double salary = 8000;//工资
}

//子类(派生类)
class Sub1 extends Super1 {
//    private int id = 4001;//工号
//    private int age = 28;//年龄
//    double salary = 8000;//工资
}
package com.p1;

/**
 * 测试继承
 */
public class Test25 {
    public static void main(String[] args) {
        Adult adult = new Adult();
        adult.teachJava(150,5.0);
        adult.teachPython();
        System.out.println(adult.boos);

        Juvenile juvenile = new Juvenile();
        juvenile.teachJava(240,8.0);
        juvenile.teachPython();
        System.out.println(adult.boos);
    }
}

class Tedu {
    String boos = "hsy";

    public void teachJava(int days,double month) {//教java
        System.out.println("tedu 教java");
    }

    public void teachPython() {//教python
        System.out.println("tedu 教python");
    }
}

class Adult extends Tedu { //成人
    //重写
    @Override
    public void teachJava(int days,double month) {
        System.out.println("教adult java");
    }

    @Override
    public void teachPython() {
        System.out.println("教adult python");
    }
}

class Juvenile extends Tedu{ //少儿
    //重写

    @Override
    public void teachJava(int days,double month) {
        System.out.println("教juvenile java");
    }

    @Override
    public void teachPython() {
        System.out.println("教juvenile python");
    }
}

3、多态
前提:要有多态

①父类的引用指向子类的实例
②编译看左边,运行看右边
方法:如果子类发生了重写,会调用子类的东西,如果没有重写,就调用父类的
③编译看左边,运行看左边
属性:一直调用父类的
④可以调用父类特有的方法以及子类重写的方法
package com.p1;

import jdk.internal.org.objectweb.asm.tree.analysis.Analyzer;

/**
 * 测试多态
 */
public class Test26 {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.eat();
        animal.m1();
        System.out.println(animal.age);
    }
}

class Animal {//动物类
    int age = 10;

    //吃东西
    public void eat() {
        System.out.println("执行了Animal方法.....");
    }

    public void m1() {

    }
}

class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫在吃猫粮");
    }
}

class Dog extends Animal{
    int age = 20;

    @Override
    public void eat() {
        System.out.println("狗在吃肉");
    }

    public void m2() {

    }
}


八、抽象类和接口

1、抽象类

(1)关键字:abstract
(2)抽象类不能被实例化,即不能被new
(3)抽象类可以有抽象方法,也可以有普通方法

一、案例:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?
1. 定义一个int类型变量hours,赋值为89
2. 定义一个int类型变量day,用来保存89小时中天数的结果
3. 定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果
4. 输出结果

二、今天是周2100天以后是周几?
1. 定义一个int类型变量week,赋值为2
2. 修改week的值,在原值基础上加上100
3. 修改week的值,在原值基础上模以7
4. 输出结果,在输出结果的时候考虑特殊值,例如周日

三、求三个整数x,y,z中的最大值
1. 定义三个int类型变量,x,y,z,随意赋值整数值
2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符)
3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符)
4. 输出结果

四、计算折扣后金额
从键盘输入订单总价格totalPrice(总价格必须>=0),根据优惠政策计算打折后的总价格。
编写步骤:
1. 判断当`totalPrice >=500` ,discount赋值为0.8
2. 判断当`totalPrice >=400` 且`<500`时,discount赋值为0.85
3. 判断当`totalPrice >=300` 且`<400`时,discount赋值为0.9
4. 判断当`totalPrice >=200` 且`<300`时,discount赋值为0.95
5. 判断当`totalPrice >=0` 且`<200`时,不打折,即discount赋值为1
6. 判断当`totalPrice<0`时,显示输入有误
7. 输出结果

五、 计算今天是星期几
定义变量week赋值为上一年1231日的星期值(可以通过查询日历获取),定义变量year、month、day,分别赋值今天日期年、月、日值。计算今天是星期几。

六、5个一行输出1-100之间的偶数
输出1-100偶数,每5个一行,一行中的每个数字之间使用逗号分隔

七、计算这一天是这一年的第几天
案例需求:从键盘分别输入年、月、日,使用循环for+if实现,判断这一天是当年的第几天
package com.p1;

/**
 * 一、案例:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?
 * 1. 定义一个int类型变量hours,赋值为89
 * 2. 定义一个int类型变量day,用来保存89小时中天数的结果
 * 3. 定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果
 * 4. 输出结果
 *
 */
public class Test27 {
    public static void main(String[] args) {
        int hours = 89;
        int day = 0;//保留天数
        int hour = 0;//保存小时
        hour=hours%24;
        day=hours/24;
//        while(true) {
//            if(hours < 24) {
//                hour = hours;
//                break;
//            }
//            hours-=24;
//            day++;
//        }
        System.out.println("天数" + day);
        System.out.println("小时数:" + hour);
    }
}

package com.p1;

/**
 *         二、今天是周2,100天以后是周几?
 *         1. 定义一个int类型变量week,赋值为2
 *         2. 修改week的值,在原值基础上加上100
 *         3. 修改week的值,在原值基础上模以7
 *         4. 输出结果,在输出结果的时候考虑特殊值,例如周日
 *
 */
public class Test28 {
    public static void main(String[] args) {
        int week = 0;
        week+=0;
        week%=7;
        System.out.println(week);
    }
}


package com.p1;

/**
 * 三、求三个整数x,y,z中的最大值
 * 1. 定义三个int类型变量,x,y,z,随意赋值整数值
 * 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符)
 * 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符)
 * 4. 输出结果
 *
 */
public class Test29 {
    public static void main(String[] args) {
        int x,y,z;
        x = 28;
        y = 77;
        z = 39;
//        int max = x>y?x:y;
//        max = max>z?max:z;

        System.out.println("2"+System.currentTimeMillis());
        int max1 = x>y?(x>z?x:z):(y>z?y:z);
        System.out.println(max1);
        System.out.println("2"+System.currentTimeMillis());

//        System.out.println(max);

        System.out.println("1"+ System.currentTimeMillis());
        int max = 0;
        if(x>y) {
            if(x>z) {
                max = x;
            } else {
                max = z;
            }
        } else {
            if(y>z) {
                max = y;
            } else {
                max = z;
            }
        }
        System.out.println(max);
        System.out.println("1"+System.currentTimeMillis());
    }
}

package com.p1;

import java.util.Scanner;

/**
 * 四、计算折扣后金额
 * 从键盘输入订单总价格totalPrice(总价格必须>=0),根据优惠政策计算打折后的总价格。
 * 编写步骤:
 * 1. 判断当`totalPrice >=500` ,discount赋值为0.8
 * 2. 判断当`totalPrice >=400` 且`<500`时,discount赋值为0.85
 * 3. 判断当`totalPrice >=300` 且`<400`时,discount赋值为0.9
 * 4. 判断当`totalPrice >=200` 且`<300`时,discount赋值为0.95
 * 5. 判断当`totalPrice >=0` 且`<200`时,不打折,即discount赋值为1
 * 6. 判断当`totalPrice<0`时,显示输入有误
 * 7. 输出结果
 *
 */
public class Test30 {
    public static void main(String[] args) {
        double totalPrice = new Scanner(System.in).nextDouble();//接收一个整数
        System.out.println("折扣前的价格:" + totalPrice);
        double discount;
        if(totalPrice>=500) {
            discount = 0.8;
            totalPrice*=discount;
        } else if(totalPrice>=400) {
            discount = 0.85;
            totalPrice*=discount;
        } else if(totalPrice>=300) {
            discount = 0.9;
            totalPrice*=discount;
        } else if(totalPrice>=200) {
            discount = 0.95;
            totalPrice*=discount;
        } else if(totalPrice>=0) {
            discount = 1;
            totalPrice*=discount;
        } else if(totalPrice<0) {
            System.out.println("金额有误!");
        }
        System.out.println("折扣后的价格:" + totalPrice);
    }
}

package com.p1;

/**
 * 五、 计算今天是星期几
 * 定义变量week赋值为上一年12月31日的星期值(可以通过查询日历获取),定义变量year、month、day,分别赋值今天日期年、月、日值。计算今天是星期几。
 */
public class Test31 {
    public static void main(String[] args) {
        int week = 6;
        int year, month, day;
        year = 2023;
        month = 4;
        day = 20;

        while (true) {
            month--;
            if (month < 1) {
                break;
            } else if (month == 2) {//闰年,2月29
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    day += 29;
                } else {//平年,2月28
                    day += 28;
                }
            } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                day += 31;
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                day += 30;
            }
        }
        System.out.println(day);
        week = (day + 6) % 7;
        System.out.println(week);

        System.out.println("---switch case---");
        while(true) {
            month--;
            if (month < 1) {
                break;
            }
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    day+=31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day+=30;
                    break;
                case 2:
                    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                        day+=29;
                    } else {
                        day+=28;
                    }
            }
        }
        System.out.println(day);
        week = (day + 6) % 7;
        System.out.println(week);
    }
}

package com.p1;

/**
 * 六、5个一行输出1-100之间的偶数
 * 输出1-100偶数,每5个一行,一行中的每个数字之间使用逗号分隔
 */
public class Test32 {
    public static void main(String[] args) {
        int index = 0;
        for (int i = 1; i < 101; i++) {
            if (i % 2 == 0) {
                index++;
                if (index % 5 == 0) {
                    System.out.println(i);
                } else {
                    System.out.print(i + ",");
                }
            }
        }
    }
}

package com.p1;

import java.util.Scanner;

/**
 * 七、计算这一天是这一年的第几天
 * 案例需求:从键盘分别输入年、月、日,使用循环for+if实现,判断这一天是当年的第几天
 */
public class Test33 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int year = scanner.nextInt();
        int month = scanner.nextInt();
        int day = scanner.nextInt();
        for (; ; ) {
            month--;
            if (month < 1) {
                break;
            } else if (month == 2) {
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    day += 29;
                } else {
                    day += 28;
                }
            } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                day += 31;
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                day += 30;
            }
        }

        System.out.println(day);
    }
}

九、数据库

SQL(Structured Query Language):结构化查询语言,规范—>普通话

(1)Mysql -->方言
(2)Oracle -->方言

SQL分类:
DDL 数据定义语言:CREATE、DROP、ALTER等
DML 数据操作语言:INSERT、DELETE、UPDATE、SELECT等
DQL 数据查询语言:SELECT
DCL 数据控制语言:GRANT、REVOKE、ROLLBACK、SAVEPOINT等

1、Mysql安装

2、注释

(1)单行注释

#单行注释1
# 单行注释2
-- 单行注释3

(2)多行注释

/*
多行注释
...
...
*/

3、导入数据库

(1)使用source命令 —用的居多
(2)使用navicat可视化工具导入

4、基本的SELECT语句

4.1 SELECT…

SELECT 1;
SELECT 9/2;

4.2 SELECT … FROM

语法:
SELECT 列名1,列名2 FROM 表名

SELECT stu_id,stu_name FROM student;
SELECT t_name FROM teacher;
SELECT * FROM student;
SELECT * FROM teacher;

5、列的别名(AS)

语法:
SELECT 列名1 AS 别名1,列名2 AS 别名2 FROM 表名

SELECT last_name AS 姓氏,commission_pct AS 奖金百分比 FROM employees;

SELECT last_name name,commission_pct comm FROM employees;

SELECT last_name "Name",salary*12 "Annual Salary" FROM employees;

注:AS可以省略

6、去除重复行(DISTINCT)

语法:
SELECT DISTINCT 列名1,列名2 FROM 表名

#单列去重
SELECT DISTINCT department_id FROM employees;
#多列去重
SELECT DISTINCT department_id,salary FROM employees;

SELECT salary,DISTINCT department_id FROM employees; #如果要去重的话,需要将DISTINCT 字段放在一个

7、空值参与运算

所有运算符或列值遇到null值时,运算的结果都为null

SELECT employee_id,salary,commission_pct,salary*12*(1+commission_pct) annual_salary FROM employees;

8、着重号

SELECT * FROM order; #会报错,order虽然是一个表名但也是一个关键字
SELECT * FROM `order`;

注:表名、字段名尽量避免和关键字冲突

9、查询常数

SELECT last_name FROM employees;

SELECT '达内' corporation,last_name FROM employees;

10、显示表结构

DESCRIBE employees;
或者
DESC employees;

11、过滤条件(where)

语法:
SELECT 列名1,列名2
FROM 表名
WHERE 过滤条件

#查询90号部门工作的员工信息
SELECT employee_id,last_name,job_id,department_id FROM employees
WHERE department_id = 90;

12、排序(ORDER BY)

使用ORDER BY字句排序
ASC(ascend)	升序(默认)
DESC(descend)	降序
ORDER BY字句在SELECT语句的结尾

单列排序
SELECT 列名1,列名2 FROM 表名 ORDER BY 列名 DESC

#查询员工的信息并按照出生日期升序排序
SELECT last_name,job_id,department_id,hire_date
FROM employees
ORDER BY hire_date;

#查询员工的信息并按照出生日期降序排序
SELECT last_name,job_id,department_id,hire_date
FROM employees
ORDER BY hire_date DESC;

#查询员工的信息和年薪,并按照年薪升序排序
SELECT employee_id,last_name,salary*12 annual_salary
FROM employees
ORDER BY annual_salary;

多列排序
SELECT 列名1,列名2 FROM 表名 ORDER BY 列名1 DESC/ASC,列名2 DESC/ASC;

#查询员工的信息,并按照部门升序,再按照薪资降序
SELECT last_name,department_id,salary
FROM employees
ORDER BY department_id,salary DESC;

13、分页

背景1:查询返回的记录太多了,查看起来不方便
背景2:表里有4条数据,只想显示第2、3条数据

分页原理
分页显式公式:

SELECT * FROM 表名 LIMIT (PageNo-1)*PageSize,PageSize;

PageNo是页码值
PageSize是每页的大小,也是每页想显示的条数
注意:LIMIET子句需要放到整个SELECT 语句的最后

#每页显示20条记录,当前页是第1页
SELECT employee_id,last_name
FROM employees
LIMIT 0,20;

#每页显示20条记录,当前页是第2页
SELECT employee_id,last_name
FROM employees
LIMIT 20,20;

#每页显示20条记录,当前页是第3页
SELECT employee_id,last_name
FROM employees
LIMIT 40,20;

#查询出第32、第33条数据
SELECT employee_id,last_name
FROM employees
LIMIT 31,2;

14、聚合函数

聚合函数作用于一组数据,并对一组数据返回一个值

常见的聚合函数:
AVG() 求平均值
SUM() 求和
MAX() 求最大值
MIN() 求最小值
COUNT() 统计数量

#查询部门编号是90的员工的工资平均值、最大值、最小值、总工资
SELECT AVG(salary),MAX(salary),MIN(salary),SUM(salary)
FROM employees
WHERE deparment_id = 90;

#查询员工表里面出生日期的最大值和最小值
SELECT MIN(hire_date),MAX(hire_date)
FROM employees;

# COUNT(*),返回表中记录总数,适用于任意数据类型
#查询部门编号是50的员工数量	
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;

# COUNT(列名) 返回不为空记录总数,如果COUNT列名有空的值,则空的值是不计算的

#查询部门编号是50的员工数量
SELECT COUNT(employee_id)
FROM employees
WHERE department_id = 50;

#查询部门编号是50的员工数量
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 50;

#COUNT(常数)
#查询编号是50的员工数量
SELECT (1)
FROM emploees
WHERE deparment_id = 50;

总结:
1COUNT(*) COUNT(列名) COUNT(常数) 谁更好?
COUNT(*)COUNT(常数) 优于COUNT(列名)

2、能不能用COUNT(列名)替代COUNT(*)?
不能

3、AVG、SUM只适用于数值类型的字段(或变量)
SELECT AVG(salary),SUM(salary),AVG(salary)*107
FROM employees

SELECT SUM(last_name),AVG(last_name),SUM(hire_date)
FROM employees;

4、MAX、MIN只适用于数值类型、字符串类型、日期时间类型的字段(或变量)
SELECT MAX(salary),MIN(salary)
FROM employees;

SELECT MAX(last_name),MIN(last_name),MAX(hire_date),MIN(hire_date)
FROM employees;

5、查询公司中平均的奖金率
SELECT AVG(commission_pct)
FROM employees;#错误的,应该/107,而不是/35

SELECT SUM(commission_pct)/COUNT(IFNULL(commission_pct,0)),
AVG(IFNULL(commission_pct,0))
FROM employees;

15、分组(GROUP BY)

语法:
SELECT 列名1,列名2
FROM 表名
(WHERE 过滤条件)
GROUP BY 列名
(ORDER BY 列名 DESC/ASC);
注:WHERE一定要放在FROM后面

#查询各个部门的平均工资和部门编号
SELECT department_id,AVG(salary)
FROM employees
GROUP BY department_id;

#查询各个部门的平均工资
SELECT AVG(salary)
FROM employees
GROUP BY department_id;

使用多个列分组
语法:
SELECT 列名1,列名2
FROM 表名
GROUP BY 列名1,列名2

#查询部门编号、工种、总薪资,先按部门编号分组,再按工种分组
SELECT deparment_id dept_id,job_id,SUM(salary)
FROM employees;
GROUP BY department_id,job_id;

16、HAVING

#查询部门工资比10000高的部门
SELECT department_id,salary
FROM employees
WHERE salary > 10000
GROUP BY department_id;

SELECT department_id,salary
FROM employees
HAVING salary > 10000
GROPU BY department_id;#会报错,where一般用于group by前面,having一般用于group by后面

#正确写法:
SELECT department_id,salary
FROM employees
GROUP BY department_id
HAVING salary > 10000;

17、模糊查询(LIKE)

两个通配符
(1)%,匹配一个或多个
(2)_,匹配一个

语法:
SELECT 列名1,列名2
FROM 表名
(WHERE 列名 LIKE 模糊查询条件)
GROUP BY 列名
ORDER BY 列名 DESC/ASC

#查询员工表里姓氏包含ia的员工信息
SELECT last_name,salary
FROM employees
WHERE last_name LIKE '%ia%';

#查询员工表里姓氏里面第二个字母是r,其余的任意的员工信息
SELECT last_name,department_id
FROM employees
WHERE last_name LIKE '_r%'
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值