Java零基础学习全套视频笔记

Java零基础学习全套视频笔记

一、Java基础

1.注释

注释并不会被执行,是给我们写代码的人看的,防止项目结构代码太多忘记代码相关功能。
书写注释是一个非常好的习惯,平时写代码也一定要注意规范。

Java中的注释有三种:

  • 单行注释
  • 多行注释
  • 文档注释
public class HelloWorld {
    public static void main(String[] args) {
        //单行注释:只能注释一行文字,使用//
        //输出一个 Hello,World!
        System.out.println("Hello,World!");

        //多行注释:可以注释一段文字  /*注释*/
        /*
        我是多行注释 
        我是多行注释 
        我是多行注释 
         */

        //JavaDoc:文档注释  /**开头*/结尾
        /**
         *@Description HelloWorld
         *@Author rj
         */
    }
}

有趣的代码注释整理(魔性图注释):https://blog.csdn.net/ydk888888/article/details/81563608

2.标识符和关键字

在这里插入图片描述
在这里插入图片描述

public class Demo01 {
    public static void main(String[] args) {

        String 王者荣耀 = "百星王者";
//        String 王者荣耀 = "倔强青铜";
        System.out.println(王者荣耀);

        //标识符大小写十分敏感
        String Man = "xuexi";
        String man = "xuexi";

        String name = "xuexi";

        String Ahello = "xuexi";
        String hhello = "xuexi";
        String $hello = "xuexi";
        String _hello = "xuexi";

//        String 1hello = "xuexi";
//        String #hello = "xuexi";
//        String *hello = "xuexi";
    }
}

3.数据类型讲解

在这里插入图片描述
在这里插入图片描述

public class Demo02 {
    public static void main(String[] args) {
        //八大基本数据类型

        //整数
        int num1 = 10;//最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;//long类型要在数字的后面加个L
 
        //小数:浮点数
        float num5 = 50.1F;//float类型要在数字的后面加个F
        double num6 = 3.1415926;

        //字符
        char name = '国';
        //字符串,Stirng不是关键字,类
        //String namea="学习";

        //布尔值:是 非
        boolean flag = true;
        //boolean flag=false;
    }
}

4.数据类型扩展及面试题讲解

import java.math.BigDecimal;

public class Demo3 {
    public static void main(String[] args) {
        //整数拓展  进制 二进制0b 十进制 八进制0  十六进制0x
        int i = 10;
        int i2 = 010;//八进制0
        int i3 = 0x10;//十六进制0x 0~9 A~F 16

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("=============================");

        //==================================================
        //浮点数 拓展?银行业务怎么表示? 钱
        //BigDecimal   数学工具类
        //==================================================
        //float  有限的 离散的 舍入误差 大约 接近但不等于
        //double
        //最好完全使用浮点数进行比较
        //最好完全使用浮点数进行比较
        //最好完全使用浮点数进行比较

        float f = 0.1f; //0.1
        double d = 0.1f; //0.1

        System.out.println(f == d); //false

        float d1 = 231313131313131f;
        float d2 = d1 + 1;
        System.out.println(d1 == d2);//true


        //==================================================
        //字符拓展?
        //==================================================
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);

        System.out.println((int) c1);//强制转换

        System.out.println(c1);

        System.out.println((int) c1);//强制转换

        //所有的字符本质还是数字
        //编码 Unicode 表:(97 = a  65=A)   2字节  65536  Excel  2  16 = 65536

        //u0000 UFFF

        char c3 = '\u0061';
        System.out.println(c3); //a

        //转义字符
        // \t 制表符
        // \n 换行
        //......
        System.out.println("Hello\tWorld");
        System.out.println("Hello\nWorld");

        System.out.println("=======================");
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa == sb);

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc == sd);
        //对象  从内存分析

        //布尔值扩展
        boolean flag = true;
        if (flag == true) {} //新手
        if (flag) {}  //老手
        //Less is More  代码要精简易读

    }
}

5.类型转换

在这里插入图片描述

public class Demo5 {
    public static void main(String[] args) {
        int i = 128;
//        byte b = (byte) i;//内存溢出
        double b = i;

        //强制转换  (类型)变量名  高---低
        //自动转换  低---高

        System.out.println(i);
        System.out.println(b);

        /*
        注意点:
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转换到低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题!
         */
        System.out.println("==============");
        System.out.println((int) 23.7);//23
        System.out.println((int) -45.89f); //-45

        System.out.println("==============");
        char c = 'a';
        int d = c + 1;
        System.out.println(d);
        System.out.println((char) d);

    }
}

public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money * years; //-1474836480  计算的时候溢出了
        long total2 = money * years; //默认是int, 转换之前已经存在问题了?

        long total3 = money * ((long) years); //先把一个数转换为Long
        System.out.println(total3);
    }
}

6.变量、常量、作用域

在这里插入图片描述

public class Demo07 {
    public static void main(String[] args) {
//        int a,b,c;
//        int a = 1, b = 2, c = 3; //程序可读性
        String name = "xuexi";
        char x = 'X';
        double pi = 3.14;

    }
}

在这里插入图片描述

public class Demo08 {

    //类变量 static
    static double salary = 2500;

    //属性:变量

    //实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0  0.0
    //布尔值:默认是false
    //除了基本类型,其余的默认值都是null;
    String name;
    int age;

    //main方法
    public static void main(String[] args) {
        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //变量类型 变量名字 = new Demo08();
        Demo08 demo08 = new Demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);

        //类变量 static
        System.out.println(salary);
    }

    //其他方法
    public void add() {

    }
}

在这里插入图片描述

public class Demo09 {

    //修饰符,不存在先后顺序
    static final double PI = 3.14;
    
    public static void main(String[] args) {
        System.out.println(PI);
    }
}

在这里插入图片描述

7.基本运算符

在这里插入图片描述

package operator;

public class Demo01 {
   public static void main(String[] args) {
       //二元运算符
       //Ctrl+D ;复制当前行到下一行
       int a = 10;
       int b = 20;
       int c = 25;
       int d = 25;

       System.out.println(a + b);
       System.out.println(a - b);
       System.out.println(a * b);
       System.out.println(a / (double) b);

   }
}
package operator;

public class Demo02 {
   public static void main(String[] args) {
       long a = 123123123123L;
       int b = 123;
       short c = 10;
       byte d = 8;

       System.out.println(a + b + c + d);//Long
       System.out.println(b + c + d);//Int
       System.out.println(c + d);//Int
   }
}

package operator;

public class Demo03 {
   public static void main(String[] args) {
       //关系运算符返回的结果:正确,错误  布尔值
       int a = 10;
       int b = 20;
       int c = 21;

       //取余,模运算
       System.out.println(c % a); //c /a 21/10=2...1

       System.out.println(a > b);
       System.out.println(a < b);
       System.out.println(a == b);
       System.out.println(a != b);
   }
}

8.自增自减运算符、初识Math类

package operator;

public class Demo04 {
    public static void main(String[] args) {
        //++ -- 自增 自减  一元运算符
        int a = 3;
        int b = a++; //执行完这行代码后,先给b赋值,再自增
        //a=a+1
        System.out.println(a);
        //a=a=1
        int c = ++a; //执行完这行代码前,先自增,再给b赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 2^3  2*2*2=8 很多运算我们会使用有一些工具类来操作!
        double pow = Math.pow(2, 3);
        System.out.println(pow);
    }
}

9.逻辑运算符、位运算符

package operator;

//逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        //与(and) 或(or)  非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a&&b:" + (a && b));//逻辑与运算,两个变量都为真,结果才为true
        System.out.println("a||b:" + (a || b));//逻辑或运算,两个变量有一个为真,则结果才为true
        System.out.println("!(a&&b):" + !(a && b));//如果是真,则变为假;如果是假,则变为真。

        //短路运算 验证前面不满足后面不执行
        int c = 5;
        boolean d = (c < 5) && (c++ < 4);
        System.out.println(d);
        System.out.println(c);
    }
}
package operator;

public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        ------------------------------------
        //与 或 异或 取反
        A&B = 0000 1100 两个都为1才为1
        A|B = 0011 1101 对应位都为0则为0,否则为1
        A^B = 0011 0001 两个位置相同则为0,否则为1
        ~B =  1111 0010 取反,就是完全相反

        2*8=16 2*2*2*2
        左移<< *2  3<<3  意思就是3*2*2*2=24
        右移>> /2  24>>3 意思就是24/2/2/2=3
        0000 0000     0
        0000 0000     1
        0000 0010     2
        0000 0011     3
        0000 0100     4
        0000 1000     8
        0001 0000     16
         */
        System.out.println(3 << 3);
    }
}

10.三元运算符及小节

package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a += b; //a=a+b
        a -= b; //a=a-b

        System.out.println(a);

        //字符串连接符 + ,String
        System.out.println("" + a + b);
        System.out.println(a + b + "");
    }
}

package operator;

//三元运算符
public class Demo08 {
    public static void main(String[] args) {
        //x ? y  :  z
        //如果x==true,则结果为y,否则结果为z

        int score = 50;
        String type = score < 60 ? "不及格" : "及格"; //必须掌握
        //if
        System.out.println(type);
    }
}

11.包机制

在这里插入图片描述

12.JavaDoc生成文档

在这里插入图片描述

package com.kuang.base;

/**
 * @author yrj
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;

    /**
     * @author yrj
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception {
        return name;
    }

    //通过命令行实现  javadoc -encoding UTF-8 -charset UTF-8 Doc.java

    //作业:学会查找使用IDEA生产JavaDoc文档!  面向百度编程

    //基础部分的一切知识,后面几乎每天都会用!
}

二、Java流程控制

1.用户交互Scanner

在这里插入图片描述

package com.kuang.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
      s  if (scanner.hasNext()) {
            //使用next方式接收
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
        }
        //凡是属于IO流的类,如果不关闭会一直占用资源,要养成良好的习惯用完就关掉
        scanner.close();
    }
}

package com.kuang.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收:");

        //判断是否还有输入
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输入的内容为:"+str);
        }
        //关闭流
        scanner.close();
    }
}

在这里插入图片描述

package com.kuang.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收:");

        String str = scanner.nextLine();
        
        System.out.println("输入的内容为:" + str);
        //关闭流
        scanner.close();
    }
}

2.Scanner进阶使用

package com.kuang.scanner;

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        //从键盘接收数据

        int i = 0;
        float f = 0.0f;

        System.out.println("请输入整数:");

        Scanner scanner = new Scanner(System.in);
        //如果... 那么
        if (scanner.hasNextInt()) {
            i = scanner.nextInt();
            System.out.println("整数数据:" + i);
        } else {
            System.out.println("你输入的不是整数!");
        }


        System.out.println("请输入小数:");
        //如果... 那么
        if (scanner.hasNextFloat()) {
            f = scanner.nextFloat();
            System.out.println("小数数据:" + f);
        } else {
            System.out.println("你输入的不是小数!");
        }

        scanner.close();

    }
}

package com.kuang.scanner;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        //我们可以输入多个数字求总和和平均数,每输入一个数字用回车确认,通过输入非数字结束输入并输出执行结果:
        Scanner scanner = new Scanner(System.in);
        //和
        double sum = 0;
        //总共输入了多少个数字
        int m = 0;
        System.out.println("请输入要计算的非数字:");
        while (scanner.hasNextDouble()) {
            double x = scanner.nextDouble();
            m = m + 1; //m++
            sum = sum + x;
            System.out.println("你输入了第" + m + "个数据,当前它们的和为" + sum);
        }
        System.out.println("个数的和为:" + sum);
        System.out.println("当前数的平均值为:" + (sum / m));
        scanner.close();
    }
}

3.顺序结构

在这里插入图片描述

package com.kuang.struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

4.if选择结构

在这里插入图片描述

package com.kuang.struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        //equals:用于判断字符串是否相等
        if (s.equals("Hello")) {
            System.out.println(s);
        }

        System.out.println("End");
        scanner.close();
    }
}

在这里插入图片描述

package com.kuang.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于60分为及格,小于60分为不及格

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        //获取输入的内容
        int score = scanner.nextInt();

        if (score > 60) {
            System.out.println("及格,分数为:" + score);
        } else {
            System.out.println("不及格,分数为:" + score);
        }

        scanner.close();
    }
}

在这里插入图片描述

package com.kuang.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        //考试分数大于60分为及格,小于60分为不及格
        Scanner scanner = new Scanner(System.in);

        /*
        if语句至多有1个else语句,else 语句在所有的else if 语句之后。
        if语句可以有若干个else if语句,它们必须在else语句之前。
        一旦其中一个else if 语句检测为true.其他的else if 以及elsa语句都将跳过执行。
         */

        System.out.println("请输入成绩:");
        //获取输入的内容
        int score = scanner.nextInt();

        if (score == 100) {
            System.out.println("恭喜满分");
        } else if (score < 100 && score >= 90) {
            System.out.println("A级");
        } else if (score < 90 && score >= 80) {
            System.out.println("B级");
        } else if (score < 80 && score >= 70) {
            System.out.println("C级");
        } else if (score < 70 && score >= 60) {
            System.out.println("D级");
        } else if (score < 60) {
            System.out.println("不及格");
        } else {
            System.out.println("成绩不合法");
        }

        scanner.close();
    }
}

在这里插入图片描述

5.Switch选择结构

在这里插入图片描述

package com.kuang.struct;

import java.util.Scanner;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透  switch 匹配有一个具体的值
        char grade = 'C';

        switch (grade) {
            case 'A':
                System.out.println("优秀");
                break;//可选
            case 'B':
                System.out.println("良好");
                break;//可选
            case 'C':
                System.out.println("及格");
                break;//可选
            case 'D':
                System.out.println("再接再厉");
                break;//可选
            case 'E':
                System.out.println("挂科");
                break;//可选
            default:
                System.out.println("未知等级");
        }
    }
}

package com.kuang.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "学习";

        //JDK7的新特性,表达式结果可以是字符串!!!
        // 字符的本质还是数字 通过反编译可以看出来 JDK8中
        //111反编译java--class(字节码文件)反输译(IDEA)
        switch (name) {
            case "学习":
                System.out.println("学习");
                break;
            case "上课":
                System.out.println("上课");
                break;
            default:
                System.out.println("注意力要集中!");
        }
    }
}

6.While循环详解

在这里插入图片描述

package com.kuang.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1~100

        int i = 0;
        
        while (i < 100) {
            i++;
            System.out.println(i);
        }
    }
}
package com.kuang.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true) {
            //等待客户端连接
            //定时检查
            //.....
        }
    }
}

package com.kuang.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+...+100=?
        int i = 0;
        int sum = 0;
        while (i <= 100) {
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}

7.DoWhile循环

在这里插入图片描述

package com.kuang.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum + i;
            i++;
        } while (i <= 100);
        System.out.println(sum);
    }
}

package com.kuang.struct;

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        while (a < 0) {
            System.out.println(a);
            a++;
        }
        System.out.println("================");
        do {
            System.out.println(a);
            a++;
        } while (a < 0);
    }
}

8.For循环详解

在这里插入图片描述

package com.kuang.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1; //初始化条件
        while (a <= 100) { //条件判断
            System.out.println(a);//循环体
            a += 2;//迭代
        }
        System.out.println("while循环结束!");

        //初始化  //条件判断 //迭代
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }

        System.out.println("for循环结束!");
        /*
        关于for循环有以下几点说明:最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,1也可以是空语句。
        然后,检测布尔表达式的值。如果为true.循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
        执行一次循环后,更新循环控制变量(选代因子控制循环变量的增减)。
        再次检测布尔表达式,循环执行上面的过程。
         */
        //for循环的死循环写法
        for ( ; ; ) {

        }
    }
}

package com.kuang.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:计算0~100之间奇数和偶数的和

        int oddSum = 0; //奇数
        int evenSum = 0; //偶数
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0) {//奇数
                oddSum += i;  //oddSum=oddSum+i;
            } else { //偶数
                evenSum += i; //evneSum=evenSum+i;
            }
        }
        System.out.println("奇数的和:" + oddSum);
        System.out.println("偶数的和:" + evenSum);
    }
}

package com.kuang.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:用while或者for循环输出1~1000之间被5整除的数,并且每行输出3个

        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
            }
            if (i % (5 * 3) == 0) {
                System.out.println();
                //System.out.println("\n");
            }
        }
        //println 输出完会换行
        //print  输出完不会换行
    }
}

9.打印九九乘法表

package com.kuang.struct;


/*
    1*1=1
    1*2=2	2*2=4
    1*3=3	2*3=6	3*3=9
    1*4=4	2*4=8	3*4=12	4*4=16
    1*5=5	2*5=10	3*5=15	4*5=20	5*5=25
    1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36
    1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49
    1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64
    1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81
 */
public class ForDemo04 {
    public static void main(String[] args) {
        //练习3:打印九九乘法表
        //1,我们先打印第一列,这个大家应该都会
        //2.我们把固定的的1再用一个循环包起来
        //3,去掉重复项,i<=j
        //4,调整样式

        //后写
        for (int j = 1; j <= 9; j++) {
            //先写
            for (int i = 1; i <= j; i++) {
                //这里要用print
                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
            }
            //输出换行
            System.out.println();
//            System.out.print("\n");
        }

    }
}

10.增强for循环

在这里插入图片描述

package com.kuang.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50}; //定义一个数组

        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }

        System.out.println("===============");

        //遍历数组的元素
        for (int i : numbers) {
            System.out.println(i);
        }
    }
}

11.break、continue、goto

在这里插入图片描述

package com.kuang.struct;

public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            System.out.println(i);
            if (i == 30) {
                break;
            }
        }
        System.out.println("123");
    }
}

package com.kuang.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            if (i % 10 == 0) {
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
        //break  在任何循环语句的主体部分,均可用break控制循环的流程。
        //break  用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
        //continue 用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
    }
}

package com.kuang.struct;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101~150之间的质数
        质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。

        int count = 0;
        outer:for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i / 2; j++) { //一个数能成功除以2
                if (i % j == 0) {   //且求模为0的不满足
                    continue outer;
                }
            }
            System.out.print(i + " ");
        }
    }
}

12.打印三角形及Debug

package com.kuang.struct;

public class TestDemo {
    public static void main(String[] args) {
        //打印一个三角形 5行
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

三、Java方法

1.什么是方法?

在这里插入图片描述

package com.kuang.method;

public class Demo01 {
    //main方法
    public static void main(String[] args) {
//        int sum = add(1, 2);
//        System.out.println(sum);
        test();
    }

    //加法
    public static int add(int a, int b) {
        return a + b;
    }

    public static void test() {
        for (int i = 1; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
            }
            if (i % (5 * 3) == 0) {
                System.out.println();
                //System.out.println("\n");
            }
        }
    }
}

2.方法的定义和调用

在这里插入图片描述

public class Demo01 {
    //main方法
    public static void main(String[] args) {
        //实际参数:实际调用传递给他的参数
        int sum = add(1, 2);
        System.out.println(sum);
//        test();
    }

    //加法
    //形式参数,用来定义作用的
    public static int add(int a, int b) {
        return a + b;
    }
}

在这里插入图片描述

package com.kuang.method;

public class Demo02 {
    public static void main(String[] args) {
        int max = max(10, 10);
        System.out.println(max);
    }

    //比大小
    public static int max(int num1, int num2) {
        int result = 0;
        if (num1 == num2) {
            System.out.println("num1==num2");
            return 0;  //return 可以作为终止方法
        }

        if (num1 > num2) {
            result = num1;
        } else {
            result = num2;
        }
        return result;
    }
}

3.方法的重载

在这里插入图片描述

package com.kuang.method;

public class Demo02 {
    public static void main(String[] args) {
        int max = max(10, 20, 30);
        System.out.println(max);
    }

    //比大小 2个double参数
    public static int max(double num1, double num2) {
        int result = 0;
        if (num1 == num2) {
            System.out.println("num1==num2");
            return 0;  //return 可以作为终止方法
        }

        if (num1 > num2) {
            result = (int) num1; //强制转换
        } else {
            result = (int) num2;  //强制转换
        }
        return result;
    }

    //比大小 2个int参数
    public static int max(int num1, int num2) {
        int result = 0;
        if (num1 == num2) {
            System.out.println("num1==num2");
            return 0;  //return 可以作为终止方法
        }

        if (num1 > num2) {
            result = num1;
        } else {
            result = num2;
        }
        return result;
    }

    //比大小 3个int参数
    public static int max(int num1, int num2, int num3) {
        int result = 0;
        if (num1 == num2) {
            System.out.println("num1==num2");
            return 0;  //return 可以作为终止方法
        }

        if (num1 > num2) {
            result = num1;
        } else {
            result = num2;
        }
        return result;
    }

}

4.命令号传递参数

在这里插入图片描述

package com.kuang.method;

public class Demo03 {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("args[" + i + "]:" + args[i]);
        }
    }
}

PS D:\SoftProjects\IDEAProjects\JavaSE\基础语法\src\com\kuang\method> javac .\Demo03.java
PS D:\SoftProjects\IDEAProjects\JavaSE\基础语法\src\com\kuang\method> cd ../../../
PS D:\SoftProjects\IDEAProjects\JavaSE\基础语法\src> java com.kuang.method.Demo03 this is xuexi
args[0]:this
args[1]:is   
args[2]:xuexi
PS D:\SoftProjects\IDEAProjects\JavaSE\基础语法\src>

5.可变参数

在这里插入图片描述

package com.kuang.method;

public class Demo04 {
    public static void main(String[] args) {
        printMax(1.5, 2.5, 2.1);
        printMax(new double[]{1, 2, 3});
    }

    private static void printMax(double... numbers) {
        if (numbers.length == 0) {
            System.out.println("No argument passend");
            return;
        }
        double result = numbers[0];

        //排序;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > result) {
                result = numbers[i];
            }
        }
        System.out.println("The max vaule is " + result);
    }
}

6.递归讲解

在这里插入图片描述

package com.kuang.method;

public class Demo06 {

    //递归思想
    public static void main(String[] args) {
        System.out.println(f(5));
    }

    private static int f(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * f(n - 1);
        }
    }
}

四、Java数组

1.什么是数组?

在这里插入图片描述

2.数组的声明和创建

在这里插入图片描述

package com.kuang.array;

public class ArrayDemo01 {
    //变量的类型  变量的名字  =变量的值
    //现在是数组类型

    public static void main(String[] args) {
        int[] nums; //1.声明一个int类型数组变量名字为nums

        nums = new int[10];  //2.创建一个数组,开辟数组空间

        //3.给数组元素种赋值
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;

        System.out.println(nums[5]);

        //计算所有元素的和
        int sum = 0;
        //获取数组的长度 arrays.length
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }
        System.out.println("总和为:" + sum);
    }
}


3.三种初始化和内存分析

在这里插入图片描述

package com.kuang.array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        //数组的静态初始化 创建+赋值
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println(a[0]);

        //数组的动态初始化,包含默认初始化
        int[] b = new int[10];
        b[0] = 10;
        b[1] = 20;

        System.out.println(b[0]);
        System.out.println(b[1]);
        System.out.println(b[2]);
    }
}

在这里插入图片描述
在这里插入图片描述

4.下标越界及小节

在这里插入图片描述
在这里插入图片描述

public static void main(String[] args) {
        //数组的静态初始化 创建+赋值
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println(a[0]);
        //数组下标越界异常的例子 ArrayIndexOutOfBoundsException
        for (int i = 0; i <= a.length; i++) {
            System.out.println(a[i]);
        }
    }

5.数组的使用

package com.kuang.array;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5};
        //遍历出数组的所有元素
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
        System.out.println("===========");

        //查找数组中所有元素的和
        int sum = 0;
        for (int i = 0; i < arrays.length; i++) {
            sum += arrays[i];
        }
        System.out.println("总和为:" + sum);
        System.out.println("===========");

        //查找数组中元素的最大值
        int max = arrays[0];
        for (int i = 1; i < arrays.length; i++) {
            if (arrays[i] > max) {
                max = arrays[i];
            }
        }
        System.out.println("最大值为:" + max);
    }
}

package com.kuang.array;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5};
        //JDK1.5 没有下标输出数组
        //遍历输出数组
        for (int array : arrays) {
            System.out.println(array);
        }
        System.out.println("============");

        //调用方法打印输出数组
        printArray(arrays);

        //数组的反转输出
        int[] reverse = reverse(arrays);
        printArray(reverse);
    }

    //反转数组
    public static int[] reverse(int[] arrays) {
        int[] result = new int[arrays.length];

        //反转的操作
        for (int i = 0, j = result.length - 1; i < arrays.length; i++, j--) {
            result[j] = arrays[i];
        }
        return result;
    }

    //调用方法打印输出数组
    public static void printArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i] + " ");
        }
        System.out.println("\n============");
    }

}

6.多维数组

在这里插入图片描述
在这里插入图片描述

package com.kuang.array;

public class ArrayDemo05 {
    public static void main(String[] args) {
        //[4][2] 四行两列
        /*
            1,2  array[0]
            2,3  array[1]
            3,4  array[2]
            4,5  array[3]
         */
        int[][] array = {{1, 2}, {2, 3}, {3, 4}, {4, 5}};

        System.out.println(array[0][1]);
        System.out.println(array.length); //数组的总长度
        System.out.println(array[0].length); //数组中第一个数组的总长度
        System.out.println("============");

        printArray(array[3]);
        System.out.println("============");

        //两层数组遍历输出所有元素
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }

    }

    //打印数组元素
    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i] + " ");
        }
    }
}

7.Arrays类讲解

在这里插入图片描述

package com.kuang.array;

import com.sun.jdi.ArrayReference;

import java.util.Arrays;

public class ArrayDemo06 {
    public static void main(String[] args) {
        int[] a = {1, 2, 34, 6, 457, 78, 8, 79};

        System.out.println(a); //[I@7c30a502
        //打印数组元素 Arrays.toString()
        System.out.println(Arrays.toString(a)); //直接打印出数组
        System.out.println("===========================");
        printArray(a);
        System.out.println("\n===========================");

        Arrays.sort(a);//数组进行排序:升序
        System.out.println(Arrays.toString(a));
        System.out.println("===========================");

        Arrays.fill(a,0);  //数组填充 用0
//        Arrays.fill(a,2,4,0); //2~4数组填充 用0
        System.out.println(Arrays.toString(a));

    }

    //造轮子 模拟Arrays.toString()输出
    public static void printArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            if (i == 0) {
                System.out.print("[");
            }
            if (i == a.length - 1) {
                System.out.print(a[i] + "]");
            } else {
                System.out.print(a[i] + ", ");
            }
        }
    }
}

8.冒泡排序

在这里插入图片描述

package com.kuang.array;

import java.util.Arrays;

/*
    //冒泡排序
    //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
    //2,每一次比较,都公产生出一个最大,或者最小的数字;
    //3,下一轮则可以少一次接序!
    //4,依次循环,直到结束!
 */
public class ArrayDemo07 {
    public static void main(String[] args) {
        int[] a = {1, 2, 4, 35, 7, 9, 99, 634, 6};

        int[] sort = sort(a); //调用完我们自己写的排序算法以后,返回一个排序后的数组
        System.out.println(Arrays.toString(sort));

        int[] sort1 = sort1(a); //调用完我们自己写的排序算法以后,返回一个排序后的数组
        System.out.println(Arrays.toString(sort1));
    }

    //排序算法
    public static int[] sort(int[] arrays) {
        //临时变量 用来交换
        int tmp = 0;
        //外层循环判断这个要走多少次
        for (int i = 0; i < arrays.length - 1; i++) {
            //内层循环,比较判断两个数,如果第一个数比第二个数大,则交换位置。
            for (int j = 0; j < arrays.length - 1 - i; j++) {
                if (arrays[j + 1] > arrays[j]) {
                    tmp = arrays[j];
                    arrays[j] = arrays[j + 1];
                    arrays[j + 1] = tmp;
                }
            }
        }
        return arrays;
    }

    //排序算法---优化
    public static int[] sort1(int[] arrays) {
        //临时变量 用来交换
        int tmp = 0;
        //外层循环判断这个要走多少次
        for (int i = 0; i < arrays.length - 1; i++) {
            boolean flag = false;  //通过falg标识位减少没有意义的比较
            //内层循环,比较判断两个数,如果第一个数比第二个数大,则交换位置。
            for (int j = 0; j < arrays.length - 1 - i; j++) {
                if (arrays[j + 1] < arrays[j]) {
                    tmp = arrays[j];
                    arrays[j] = arrays[j + 1];
                    arrays[j + 1] = tmp;
                    flag = true;
                }
            }
            if (flag == false) {
                break;
            }
        }
        return arrays;
    }
}

9.稀疏数组

在这里插入图片描述
在这里插入图片描述

package com.kuang.array;

public class ArrayDemo08 {
    public static void main(String[] args) {
        //1.创建一个二维数组11*11  0:没有棋子  1:黑棋  2:白棋
        int[][] array1 = new int[11][11];
        array1[1][2] = 1; //黑棋
        array1[2][3] = 2; //白棋
        //输出原始的数组
        System.out.println("输出原始的数组");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

        System.out.println("================");
        //转换为稀疏数组保存
        //获取有效值的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array1[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数" + sum);

        //2.创建一个稀疏数组的数组
        int[][] array2 = new int[sum + 1][3]; //行和列数
        //打印稀疏数组的头行
        array2[0][0] = 11; //数组的行数
        array2[0][1] = 11; //数组的列数
        array2[0][2] = sum; //稀疏数组的有效数字
        //遍历二维数组,将非零的值,存放稀疏数组中
        int count = 0;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0) {
                    count++;
                    array2[count][0] = i;//count行0列
                    array2[count][1] = j;//count行1列
                    array2[count][2] = array1[i][j];//count行2列
                }
            }
        }
        //输出稀疏数组
        System.out.println("稀疏数组");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i][0] + "\t" +
                    array2[i][1] + "\t" +
                    array2[i][2] + "\t");
        }
        System.out.println("================");
        System.out.println("还原原始数组");
        //1.读取稀疏数组
        int[][] array3 = new int[array2[0][0]][array2[0][1]];//总行和列在稀疏数组的头行中

        //2.给其中的元素还原值
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];//稀疏数组的i行i列及值存入到新数组
        }
        //3.打印
        for (int[] ints : array3) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }
}

五、面向对象

1.什么是面向对象?

在这里插入图片描述
在这里插入图片描述

2.回顾方法的定义

在这里插入图片描述

package com.oop;

import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) {

    }
    
    /*
    修饰符 返回值类型  方法名(...){
        方法体
        return 返回值;
    }
     */
    //return 结束方法,返回一个结果。
    public String sayHello() {
        return "Hello,World!";
    }

    public void print() {
        return;
    }

    public int max(int a, int b) {
        return a > b ? a : b;//三元运算符
    }

    //数组下标越界:Arrayindexoutofbounds

    public void readFile(String File) throws IOException {

    }
}

3.回顾方法的调用

在这里插入图片描述

package com.oop;

//静态方法非静态方法
public class Demo02 {
    public static void main(String[] args) {
        //实例化这个类
        //对象类型  对象名  对象值
        Student student = new Student();
        student.say();
    }

    //和类一起加载的
    public static void a() {
        //这里不能直接调用当前没有实例化的。
        //b();
    }

    //类实例化之后才存在
    public void b() {

    }
}

package com.oop;

//学生类
public class Student {
    public void say() {
        System.out.println("学生说话了!");
    }
}
package com.oop;
//形参和实参
public class Demo03 {
    public static void main(String[] args) {
        //实际参数的类型和形式参数的类型要对应
        int add = add(1, 2);
        System.out.println(add);
    }

    public static int add(int a, int b) {
        return a + b;
    }
}

package com.oop;

//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a); //1

        Demo04.change(a);
        System.out.println(a); //1
    }

    //返回值为空
    public static void change(int a) {
        a = 10;
    }
}

package com.oop;

//引用传递:对象,本质还是值传递
//对象,内存!
public class Demo05 {
    public static void main(String[] args) {
        Perosn perosn = new Perosn();
        System.out.println(perosn.name); //null

        Demo05.chang(perosn);
        System.out.println(perosn.name); //学习
    }

    public static void chang(Perosn perosn) {
        //perosn是一个对象,指向的--->Perosn perosn = new Perosn();这是一个具体的人,可以改变属性!
        perosn.name = "学习";
    }
}

class Perosn {
    String name;
}

4.类与对象的创建

在这里插入图片描述
在这里插入图片描述

package com.oop.demo02;

//学生类
public class Student {
    //属性,字段
    String name;//null
    int age;//0

    //方法
    public void study() {
        System.out.println(this.name + "学生在学习");
    }

}

package com.oop;

//一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
        //类,抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Student类的具体实例
        Student xiaoming = new Student();
        Student xh = new Student();

        xiaoming.name = "小明";
        xiaoming.age = 3;

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        xh.name = "小红";
        xh.age = 3;

        System.out.println(xh.name);
        System.out.println(xh.age);

    }
}

5.构造器详解

在这里插入图片描述

package com.oop.demo02;

//构造器 java-->class
public class Person {
    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器
    String name;
    int age;

    //1.使用new关键字,本质是在调用构造器
    //2.用来初始化值
    public Person() {
        this.name = "xuexi";
    }

    //有参构造:一旦定义了有参构造,无参构造必须显示定义
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //快捷键Alt+Insert可快速生成有参无参的构造方法
}
package com.oop;

//一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
        //new 实例化了一个对象
        Person person = new Person("kanshu", 23);
        System.out.println(person.name);//xuexi
    }
}

6.创建对象内存分析

在这里插入图片描述

package com.oop.demo03;

public class Pet {
    public String name;
    public int age;

    //默认有无参构造

    public void shout() {
        System.out.println("叫了一声!");
    }
}

package com.oop;

import com.oop.demo03.Pet;

//一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
        Pet dog = new Pet();
        dog.name = "旺财";
        dog.age = 3;
        dog.shout();

        System.out.println(dog.name);
        System.out.println(dog.age);

        Pet cat = new Pet();
    }
}

7.简单小节类与对象

package com.oop;

//一个项目应该只有一个main方法
public class Application {
    public static void main(String[] args) {
        /*
            1. 类与对象
                类是一个模板:抽象,对象是一个具体的实例
            2. 方法
                定义、调用!
            3. 对应的引用
                引用类型:基本类型(8)
                对象是通过引用来操作的:栈-->堆
            4. 属性:字段Field成员变量
                默认初始化:
                    数字:Θ  0.0
                    char:u000
                    boolean:false
                    引用:null

                修饰符  属性类型  属性名 = 属性值!
            5. 对象的创建和使用
                    必须使用new关键字创造对象,构造器 Person xuexi = new Person();
            -       对象的属性 xuexi. name
                    对象的方法 xuexi.sleep()
            6. 类:
                静态的属性 属性
        */
    }
}

8.封装详解

在这里插入图片描述

package com.oop.demo04;

//类 private :私有
public class Student {
    //属性私有
    private String name;//姓名
    private int id;//学号
    private char sex;//性别
    private int age;//年龄

    //提供一些可以操作这个属性的方法
    //提供一些public的get、set方法

    //get 获取这个数据
    public String getName() {
        return this.name;
    }

    //set 给这个数据设置值
    public void setName(String name) {
        this.name = name;
    }

    //Alt + insert
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    //可做一些过滤或判断等
    public void setAge(int age) {
        if (age > 120 || age < 0) { //不合法
            this.age = 3;
        } else {
            this.age = age;
        }
    }
}

package com.oop;

import com.oop.demo04.Student;

/*
    1.提高程序的安全性,保护数据
    2.隐藏代码的实现细节
    3.统一接口
    4.系统可维护增加
 */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("学习");
        //方法名相同,参数列表相同 就是同一个方法
        System.out.println(s1.getName());

        s1.setAge(999); //不合法
        System.out.println(s1.getAge());
    }
}

9.什么是继承?

在这里插入图片描述

package com.oop.demo05;

//在Java中,所有的类,都默认直接或间接继承Object
//Person 人:父类
public class Person /*extends Object*/ {

    public int money = 10_0000_0000;

    public void say() {
        System.out.println("说了一句话");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

package com.oop.demo05;

//学生 is 人:派生类:子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person {
    //Ctrl + H 查看继承情况
    
}

package com.oop;

import com.oop.demo05.Person;
import com.oop.demo05.Student;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
        System.out.println(student.money);

        Person person = new Person();
        System.out.println(person.getMoney());
    }
}

10.Super详解

package com.oop.demo05;

//在Java中,所有的类,都默认直接或间接继承Object
//Person 人:父类
public class Person /*extends Object*/ {
    public Person(String name) {
        System.out.println("Person有参执行了");
    }

    protected String name = "xuexi";

    //私有的东西无法被继承
    public void print() {
        System.out.println("Person");
    }
}

package com.oop.demo05;

//学生 is 人:派生类:子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person {
    public Student() {
        //隐藏代码:调用了父类的无参构造
        super("hello"); //调用父类的构造器,必须要在子类的第一行
        System.out.println("Student无参执行了");
    }

    private String name = "shangke";

    public void print() {
        System.out.println("Student");
    }

    public void test1() {
        print();
        this.print();
        super.print();
    }

    public void test(String name) {
        System.out.println(name);//上课
        System.out.println(this.name);//shangke
        System.out.println(super.name);//xuexi

    }

}

package com.oop;

import com.oop.demo05.Student;

public class Application {
    public static void main(String[] args) {
        //验证构造器有参无参父类子类的调用
        Student student = new Student();
    }
}

super注意点:
    1.super调用父类的构造方法,必须在构造方法的第一个
    2.super 必须只能出现在子类的方法或者构造方法中!
    3.super和 this不能同时调用构造方法!
对比 this:
    代表的对象不同:
        this:本身调用者这个对象
        super:代表父类对象的应用
    前提
        this:没有继承也可以使用
        super:只能在继承条件下才可以使用
    构造方法
        this();本类的构造!
        super():父类的构造!

11.方法重写

package com.oop.demo05;

//重写都是方法的重写,和属性无关
//父类
public class B {
    public void test() {
        System.out.println("B=>test()");
    }
}

package com.oop.demo05;

//继承 子类继承父类
public class A extends B {
    //Override 重写
    @Override//注解:有功能的注解
    public void test() {
        //super.test();
        System.out.println("A=>test()");
    }
}

package com.oop;

import com.oop.demo05.A;
import com.oop.demo05.B;

public class Application {
    //静态的方法和非静态的方法区别很大
    //静态方法: //方法的调用只和左边定义的类型有关
    //非静态的方法才可以重写
    public static void main(String[] args) {
        //方法的调用只和左边定义的类型有关
        A a = new A();
        a.test(); //A

        //父类的引用指向了子类
        B b = new A(); //子类重写了父类的方法
        b.test();//B

    }
}
重写:需要有继承关系,子类重写父类的方法
    1.方法名必须相同
    2.参数列表必须相同
    3.修饰符:范围不可以扩大但可以缩小:public > Protected > Default > private
    4.抛出的异常:范围,可以被缩小,但不能扩大:ClassNotFoundException==>Exception

重写,子类的方法和父类必要一致:方法体不同!

为什么需要重写;
    1.父类的功能,子类不一定需要,或者不一定满足!
    记住快捷键Alt + Insert;    选择override;

12.什么是多态?

在这里插入图片描述

package com.oop.demo06;

//父类
public class Person {
    public void run(){
        System.out.println("run");
    }
}
package com.oop.demo06;

//子类
public class Student extends Person {
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat() {
        System.out.println("eat");
    }
}

package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //可以指向的引用类型就不确定了:父类的引用指向子类
        //Student能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //Person父类型,可以指向子类,但是不能调用子类中独有的方法
        Person s2 = new Student();
        Object s3 = new Student();

        //对象能执行哪些方法,主要看对象左边的类型,和右边的类型关系不大!
        s1.run(); //子类重写了父类的方法,执行子类的方法
        //s2.eat();//父类不能调用子类中独有的方法
        ((Student) s2).eat();//将父类强制转换为子类再调用
        s1.eat();//子类直接调用子类里独有的方法
    }
}
多态注意事项:
    1,多态是方法的多态,属性没有多态
    2.父类和子类,有联系 类型转换异常!CLassCastException!
    3.存在条件:继承关系,方法需要重写,父类引用指向子类对象!Father f1= new Son();

    1.static 方法,属于类,它不属于实例
    2.final 常量:
    3.private方法:

13.instanceof和类型转换

package com.oop.demo06;

//父类
public class Person {
    public void run() {
        System.out.println("run");
    }
}
package com.oop.demo06;

//子类
public class Student extends Person {
    public void go() {
        System.out.println("go");
    }
}

package com.oop.demo06;

//子类
public class Teacher extends Person{

}

package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
    public static void main(String[] args) {
        //Object>String
        //Object>Person>Teacher
        //Object>Person>Student
        //System.out.println(X instanceof Y);//是否有子类父类关系,主要看能不能编译通过。
        Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//fasle
        System.out.println(object instanceof String);//fasle
        System.out.println("==========================");

        Person person = new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//fasle
//        System.out.println(person instanceof String);//编译报错!
        System.out.println("==========================");

        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
//        System.out.println(student instanceof Teacher);//编译报错!
//        System.out.println(student instanceof String);//编译报错!
        System.out.println("==========================");


        //类型之间的转化:父类   子类
        //高                  低       低转高不需要强制转化,高转低需要强制转化
        Person person1 = new Student();
        //obj 将这个对象转换为Student类型,我们就可以使用Student 类型的方法了
        ((Student) person1).go();//将person强制转化为Student类型

        //子类转换为父类,可能会丢失自己本来的一些方法!
        Student student1 = new Student();
        student1.go();
        
        Person person2 = student; //多态
    }
}
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型:需强制转换
4.方便方法的调用,减少重复的代码!简洁

抽象:封装、继承、多态!

14.static关键字详解

package com.oop.demo07;

//static
public class Student {
    private static int age;//静态的变量  多线程用使用多
    private double score; //非静态的变量

    public void run() {

    }

    public static void go() {

    }

    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);
        //System.out.println(Student.score); //非静态不可直接调
        //非静态只可通过以下方式调用
        System.out.println(s1.age);
        System.out.println(s1.score);

        go(); //静态方法直接调
        new Student().run(); //非静态调用
    }
}

package com.oop.demo07;

//代码块
public class Person {

    //2:可以用来赋 初始值~
    {
        System.out.println("匿名代码块");
    }

    //1:只执行一次~
    static {
        System.out.println("静态代码块");
    }

    //3
    public Person() {
        System.out.println("构造方法");
    }

    //
    public static void main(String[] args) {
        //验证调用顺序  静态代码块>匿名代码块>构造方法
        Person person1 = new Person();
        System.out.println("=============");
        Person person2 = new Person();

    }
}

package com.oop.demo07;

//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;

public class Test {
    public static void main(String[] args) {
        //不导入包,可以直接用写
        System.out.println(Math.random());
        //导入静态包可以直接用
        System.out.println(random());
        System.out.println(PI);
    }
}

15.抽象类

在这里插入图片描述

package com.oop.demo08;

//abstract  抽象类:类  extends:java类是单继承~  (接口可以多继承)
public abstract class Action {
    //约束~ 有人帮我们实现~
    //abstract,抽象方法,只有方法的名字,没有方法的实现
    public abstract void doSometthing();
}

package com.oop.demo08;

//抽闲类的所有方法,继承了它的子类,都必须要实现
//子类继承抽象父类,方法中的方法都要必须重写实现
public class A extends Action{
    @Override
    public void doSometthing() {

    }
}

    //1.不能new 这个抽象类,只能靠子类去实现它:约束!
    //2.抽象类中可以写普通方法~
    //3.抽象方法必须在抽象类中~
    //抽象的抽象:约束~

    //思考题? 抽象类不能new,那存在构造器吗?
              //存在的意义是什么? 抽象出来~ 提高开发效率

16.接口的定义与实现

在这里插入图片描述

package com.oop.demo09;

//锻炼抽象的思维  Java
//interface 定义的关键字; 接口都需要有实现类
public interface UserService {
    //常量~ public static final
    int AGE=99;

    //接口中的所有定义其实都是抽象的 public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}

package com.oop.demo09;

public interface TimeService {
    void time();
}

package com.oop.demo09;

//抽象类:extends~
//类 可以实现接口  implements  接口
//实现了接口的类,就需要重写接口中的方法~
//多继承~利用接口实现多继承~
public class UserServiceImpl implements UserService,TimeService {
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void time() {

    }
}

接口的作用:
    1.约束
    2.定义一些方法,让不同的人实现~ 10  --->  1
    3.public abstract  //接口里的方法默认有
    4.public static final  //接口里的常量默认有
    5.接口不能被实例化在~,接口没有构造方法~
    6.implements可以实现多个接口
    7.必须要重写接口中的方法

    一定要总结博客~~

17.N中内部类

在这里插入图片描述

package com.oop.demo10;

//外部类
public class Outer {
    private int id = 10;

    public void out() {
        System.out.println("这是外部类的方法");
    }

    //成员内部类
    public class Inner {
        public void in() {
            System.out.println("这是内部类的方法");
        }

        //内部类获得外部类的私有属性~
        public void getID() {
            System.out.println(id);
        }
    }
}
package com.oop;

import com.oop.demo10.Outer;

public class Application {
    public static void main(String[] args) {
        //new
        Outer outer = new Outer();
        //通过外部类来实例化内部类~
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}
package com.oop.demo10;

//外部类
public class Outer {
    private static int id1 = 11;

    public void out() {
        System.out.println("这是外部类的方法");
    }

    //静态内部类 先执行
    public static class Inner1 {
        public void in() {
            System.out.println("这是内部类的方法");
        }

        //内部类获得外部类的私有属性~
        public void getID() {
            System.out.println(id1);
        }
    }


}

package com.oop.demo10;

//外部类
public class Outer {
    //局部内部类
    public void method() {
        class Inner2 {
            public void in(){

            }
        }
    }
}

package com.oop.demo10;

//外部类
public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不用将实例保存到变量中~
        new Apple().eat();
        //匿名内部类
        UserService userService = new UserService() {
            @Override
            public void hello() {

            }
        };
    }
}

class Apple {
    public void eat() {
        System.out.println("1");
    }
}

//接口
interface UserService {
    void hello();
}

六、异常

1.Error和Exception

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.捕获和抛出异常

在这里插入图片描述

package com.exception.demo01;

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        //假设要捕获多个异常,从小到大去捕获,否则就会出错。
        try {//try为监控区域
            System.out.println(a / b);
        } catch (Error e) { //catch(想要捕获的异常类型!)捕获异常
            System.out.println("Error");
        } catch (Exception e) { //catch(想要捕获的异常类型!)捕获异常
            System.out.println("Exception");
        } catch (Throwable e) { //catch(想要捕获的异常类型!)捕获异常
            System.out.println("Throwable");
        } finally {//处理善后工作
            System.out.println("finally");
        }

        //finally可以不要,finally主要用在IO,资源的关闭
    }
}

package com.exception.demo01;

public class Test {
    public static void main(String[] args) {
        new Test().test(1,0);
    }

    //假设这个方法中,处理不了这个异常,方法主动抛出异常
    public void test(int a, int b) throws ArithmeticException {
        if (b == 0) { //throw   throws
            throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
        }
        System.out.println(a / b);
    }
}

3.自定义异常及经验小结

在这里插入图片描述

package com.exception.demo02;

//自定义异常类
public class MyException extends Exception {
    //传递数字>10
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }

    //toString:异常的打印信息
    @Override
    public String toString() {
        //增加一些处理异常的代码块
        return "MyException{" + detail + '}';
    }
}

package com.exception.demo02;

public class Test {

    //可能会存在异常的方法
    static void test(int a) throws MyException {

        System.out.println("传递的参数为:" + a);

        if (a > 0) {
            throw new MyException(a);//抛出
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(1);
            //test(11);
        } catch (MyException e) {
            System.out.println("MyException=>" + e);
        }
    }
}

在这里插入图片描述

七、JavaSE总结

JavaSE总结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值