java基础知识问题

笔记记录来自哔哩哔哩视频:【狂神说Java】Java零基础学习视频通俗易懂

入门:

打开CMD的方式

1.开始+系统+命令提示符

2.win+R 输入cmd打开控制台(推荐使用)

3.在任意的文件夹下面,按住shift键+鼠标右键点击,在此处打开命令行窗口

4.资源管理器的地址栏前面加上cmd和一个空格 路径

管理员方式运行:选择以管理员方式运行

常用的DOS命令

wins下的操作:
​
#盘符切换  E:   
#查看当前目录下的所有文件  dir
#切换目录  cd change directory
​
cd /d f:
cd /d f:\package
​
cd ..  
# 返回上一级目录
​
F:\>cd package
​
#清理屏幕 cls  (clear  screen)
#退出终端 exit
#查看电脑的ip ipconfig
​
#打开应用
  #查看计算机器 calc
  #查看画图工具 mspaint
  #查看记事本 notepad
  
#ping 命令
ping www.baidu.com
​
#文件操作
  #创建目录名文件  md test
  #创建一个txt文件   cd>a.txt
  #删除一个文件      del a.txt
  #移除目录文件  rd test
​
​
​

卸载JDK

1.删除Java的安装目录

2.删除高级环境变量 中JAVA_HOME的路径

3.删除path下关于Java的目录

4.cmd 下 输入java -version 查看是否还有jdk

安装JDK

1.百度搜索jdk8,找到下载地址

2.同意协议

3.下载电脑对应的版本

4.双击安装jdk

5.记住安装的路径

6.配置环境变量

1.我的电脑-右键-属性

2.环境变量-系统变量-新建系统变量 JAVA_HOME C:\Program Files\Java\jdk1.8.0_121

3.配置path变量 %JAVA_HOME%\bin

%JAVA_HOME%\jre\bin

(%代表引用,查找路径)

7.测试jdk是否安装成功

1.打开cmd下

2.输入 java -version

注:1、 在path变量中配置的E:\Program Files\Java\jdk1.8.0_211\bin,作用是设置javac和java的路径,而非是仅仅是java的路径。设置了此环境变量之后,不用设置CLASSPATH环境变量:=.; %JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar,也可以实现通过CMD命令对javac的访问。 2、 在java安装完成时,系统会自动在path变量中生成一个变量:C:\Program Files (x86)\Common Files\Oracle\Java\javapath,此变量便是java的路径设置。若不设置path变量:E:\Program Files\Java\jdk1.8.0_211\bin的话则无法对设置javac的路径,所以CMD也执行不了javac命令。实际上光设置path变量:E:\Program Files\Java\jdk1.8.0_211\bin即可实现java和javac的路径设置。 3、 CLASSPATH环境变量:=.; %JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar并非是javac的环境变量,也不是java的变量。 4、 同时也需要注意,在设置完环境变量之后,需要重启CMD命令窗口,不然CMD依然是按照设置之前的环境变量进行代码执行,导致出错。 4、在cmd中验证Java、Javac的环境变量是否配置好以及是否安装完成。运行代码之后出现如下运行结果之后即表示安装完成,同时环境变量也配置好了;

java程序运行机制

文件名 Hello.java

public class Hello{ 
    public static void main(String args []){
        System.out.println("Hello Java!"); } 
}

编译:javac Hello.java

编译成功后会出现一个.class文件

运行:java Hello

出现错误: 找不到或无法加载主类

错误原因:

当安装Jdk8版本时,系统会自动在环境变量path中配置一个变量:C:\Program Files (x86)\Common Files\Oracle\Java\javapath。此变量便是java的路径设置,然后只需在path变量中设置一下javac的环境变量:E:\Program Files\Java\jdk1.8.0_211\bin即可,无需再设置一个classpath变量,若设置之后,则在CMD中运行代码时产生一个错误:错误: 找不到或无法加载主类Hello。

解决方法:

删除用户环境变量和系统环境变量中的classpath,重启CMD命令行窗口。然后在按照如下方式执行代码即可

计算机的高级编程语言类型:编译型、解释型。

二者区别:时机不同

Java既有编译型特点,又有解释型特点。

编译型(compile):有一个负责翻译的编译器,将整个源代码翻译成计算机可以识别的代码。

因为已经把所有东西都翻译好,所以执行速度快,对操作系统的要求较低

常用于操作系统开发,C,C++ …

解释型:代码走一句解释一句。若回头执行同一段重复代码,又得重新翻译。

常用于网页和服务器的脚本,因为对速度要求不高 理解:Java在真正执行之前,经过了一次预编译,生成了介于机器码和Java源代码之间的代码(字节码),字节码运行时进入JVM虚拟机,将class的类放入类装载器中,字节码校验器用于查看代码的正误,之后进入解释器走一步运行一步程序。

Java 先编译,后解释

基础

Java三种注释:

单行注释

多行注释

文档注释

//单行注释   注释一行文字


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



/**
 * @Description  文档注释
 * @Author  lyt
 */


标识符

关键字:

 

 数据类型

package demolyt;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/9/21 21:28
 */
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;//F float类型要在数字后面加个F
        double num6=3.1414926;
        //字符
        char name='A';//代表一个字
        //字符串,String不是关键字,类
        // String name="lyt";
        //布尔值:是非
        boolean flag=true;
        //boolean flag=false;
        
    }
}

数据类型扩展:

二进制0b

八进制0

十六进制0x

package com.test.lyt.dataType;

import org.springframework.boot.security.reactive.ApplicationContextServerWebExchangeMatcher;

/**
 * @author lyt
 * @description
 * @date 2021/9/17 14:56
 */
public class Demo03 {


    public static void main(String[] args) {

        //整数扩展:进制  二进制0b   十进制  八进制0   十六进制0x
        int i=10;     //十进制  表示10
        int i2=010;   //八进制0  表示8
        int i3=0x10;    //十六进制0x  0~9 A~F    表示16

        //浮点数扩展  银行业务 使用BigDecimal 数学工具类

        float f=0.1f; //0.1
        double d=1.0/10;//0.1
        System.out.println(f==d);//false


        // float 有限 离散 舍入误差 大约    接近但不等于
        //最好完全使用浮点数进行比较
        float d1=23333333333333f;
        float d2=d1+1;
        System.out.println(d1==d2);//true

        char c1='a';
        char c2='中';
        System.out.println(c1);//a
        System.out.println((int)c1);//强制转换97

        System.out.println(c2);//中
        System.out.println((int)c2);//强制转换 20013
        //所有的字符本质还是数字
        //编码 Unicode   表:(97=a  65=A) 2字节  0~65536
        // Excel表里的 2的16方 = 65536

        //U0000 UFFFF
        char c3='\u0061';//转义字符
        System.out.println(c3);//a
        //转义字符
        // \t  制表符
        // \n 换行
        System.out.println("hello\tworld");// hello   world

        String hello1 = new String("hello");
        String hello2 = new String("hello");
        System.out.println(hello1==hello2);//false
        String hello3 = "hello";
        String hello4 = "hello";
        System.out.println(hello3==hello4);//true
        //对象  从内存分析

        //布尔值扩展
        boolean flag=true;
        if(flag==true){}  //新手
        if(flag){}  //老手

    }
}

package com.test.lyt.dataType;

/**
 * @author lyt
 * @description
 * @date 2021/9/17 16:36
 */
public class Demo05 {
    public static void main(String[] args) {
       // java是强类型语言   类型 低到高
        //byte(1B),short(2B),char(2B) ->  int(4B)  ->  long(8B)  ->   float(4B)   ->    double(8B)
        //boolean (1B)
        //String:不是关键字,是一个类
        //char:一个字(无论英文还是中文)
        int i=128;
        byte b=(byte)i;//内存溢出
        //强制类型转换 (类型)变量名  高到低
        //自动类型转换  低到高
        System.out.println(i);//128
        System.out.println(b);//-128  内存溢出

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

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

    }
}

类型转换

package com.test.lyt.dataType;

/**
 * @author lyt
 * @description
 * @date 2021/9/18 9:29
 */
public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //jdk7新特性,数字之间可以用下划线分割
        int money=10_0000_0000;
        System.out.println(money);//1000000000
        int years=20;
        int total=money*years;// -1474836480 计算的时候溢出了
        long total2=money*years;// -1474836480 默认是int,转换之前已经存在问题了
        System.out.println(total);
        System.out.println(total2);

        long total23=money*((long)years);// 20000000000  先把一个数转换成long
        System.out.println(total23);

    }
}

变量

变量的命名规范

所有变量、方法、类名:

  • 类成员变量:首字母小写和驼峰原则:monthSalary
  • 局部变量:首字母小写和驼峰原则
  • 常量:大写字母和下划线:MAX_VALUE
  • 类名:首字母大写和驼峰原则:Man GoodMan
  • 方法名:首字母小写和驼峰原则:run(),runRun()

package com.test.lyt.dataType;

import javax.swing.*;

/**
 * @author lyt
 * @description   变量: 是可以变化的量
 * Java是一种强类型语言,每个变量都必须声明其类型。
 * Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
 * 注意:数据类型 变量名=值;
 * 每个变量都有类型,类型可以是基本类型,也可以是引用类型
 * 变量名必须是合法的标识符。
 * 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
 *
 *
 * @date 2021/9/18 10:05
 */
//类里面 
public class Demo08 {
    //属性:变量


    //类成员变量:静态域变量 static   类变量:如果不自行初始化,这个类型的默认值 0  0.0  null  u0000
    static double salary=250;


    //实例变量:域变量  从属于对象: 如果不自行初始化,这个类型的默认值 0  0.0  null  u0000
    //布尔值:默认值是false
    //除了基本类型八个(byte、short、int、long、char、boolean、double、float),其余的默认值都是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);

    }

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

    }
}

package com.test.lyt.dataType;

/**
 * @author lyt
 * @description
 * 常量 final 常量名=值;
 *  *   final double PI=3.14;
 *  *   常量名一般使用  大写字符
 *  * 常量:初始化后不能再改变值,不会变动的值
 *  * 所谓常量可以理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变
 * @date 2021/9/18 10:41
 */
//类名:Demo09 首字母大写
public class Demo09 {

    //static final是修饰符,不存在先后顺序   常量
    static final double PI=3.14;


    public static void main(String[] args) {
        System.out.println(PI);  //引用 修饰符static final

    }
}

 运算符

Java语言支持如下运算符:

  • 算术运算符: +,-,*,/,%,++,--
  • 赋值运算符:=
  • 关系运算符:>,<,>=,<=,==,!=   ,    instanceof(面向对象的时候遇到)
  • 逻辑运算符:&&,||,!     与或非
  • 位运算符:&,|,^,~,>>,<<,>>>    (二进制)
  • 条件运算符   ?:
  • 扩展复制运算符:+=,-=,*=,/=
package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description
 * @date 2021/9/18 11:19
 */
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 / b);
        /* 30 -10 200 0 */

        System.out.println(a/(double)b);//0.5
    }
}

package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description
 * @date 2021/9/18 11:24
 */
public class Demo02 {
    public static void main(String[] args) {
        long a=12324244L;
        int b=123;
        short c=10;
        byte d=8;
        System.out.println(a+b+c+d);//Long  只要有一个是Long类型就是Long
        System.out.println(b+c+d);//Int 除了Long其他类型都转成Int类型
        System.out.println(c+d);//Int 除了Long其他类型都转成Int类型

    }
}

package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description
 * @date 2021/9/18 11:29
 */
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确  错误  布尔值
        int a=10;
        int b=20;
        System.out.println(a>b); //false
        System.out.println(a<b);//true
        System.out.println(a==b);//false
        System.out.println(a!=b); //true
        //取模 模运算 10/20=0......10
        System.out.println(a%b);//10
    }
}
package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description
 * @date 2021/9/18 14:19
 */
public class Demo04 {
    public static void main(String[] args) {
        //++  -- 自增 自减  一元运算符
        int a=3;
        int d=7;
        int b=a++;//执行完成这行代码后,先给b赋值,再自增
        int c=++d;//执行完这行代码前,先自增,再给c赋值
        System.out.println(a);//4
        System.out.println(b);//3
        System.out.println(c);//8
        System.out.println(d);//8
        //幂运算2^3   2*2*2=8    使用数学工具类来操作
       double pow= Math.pow(2,3);
        System.out.println(pow);
    }
}
package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description  逻辑运算符
 * @date 2021/9/18 14:28
 */
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<4)&&(c++<4);
        System.out.println(d);//false
        System.out.println(c);//5
    }
}

package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description 位运算符
 * @date 2021/9/18 14:41
 */
public class Demo06 {
    public static void main(String[] args) {
        /*
           A=0011 1100
           B=0000 1101
           --------------------
        A&B= 0000 1100
        A|B= 0011 1101
        A^B= 0011 0001 取反 相反为1,相同为0
        ~B = 1111 0010

        2*8=16  2*2*2*2=16
        << *2 乘以2
        >> /2 除以2
        0000 0000 0
        0000 0001 1
        0000 0010 2
        0000 0011 3
        0000 0100 4
        0000 1000 8
        0001 0000 16
         */
        System.out.println(2<<3);//16

    }
}
package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description 扩展赋值运算符
 * @date 2021/9/18 16:00
 */
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);//10
        //字符串连接符+,String
        System.out.println(""+a+b);//1020  先拼接成字符串
        System.out.println(a+b+"");//30  先运算后拼接成字符串
    }
}

package com.test.lyt.dataType.operator;

/**
 * @author lyt
 * @description  三元运算符
 * @date 2021/9/18 16:20
 */
public class Demo08 {
    public static void main(String[] args) {
        //x?y:z
        //如果x==true,则结果为y,否则结果为z
        int score=80;
        String type=score<60?"不及格":"及格";
        System.out.println(type);//及格
    }
}

包机制

JavaDoc

javadoc命令是用来生成自己API文档的

参数信息:

/**
 * @author lyt 作者名
 * @version 1.0 版本号
 * @date 2021/9/21 21:44
 * @param 参数名
 * @return 返回值情况
 * @throws 异常抛出情况
 * @since 指明需要最早使用的jdk版本
 * 
 */
package demolyt;

/**
 * @Description 类的注释
 * @author lyt 作者名
 * @version 1.0 版本号
 * @date 2021/9/21 21:44
 *
 * @return 返回值情况
 *
 * @since 1.8指明需要最早使用的jdk版本
 *
 */
public class Doc {
    String name;


    /**
     * @Description 方法注释
     * @param name
     * @return
     * @throws Exception
     */
    public String  test(String name)throws Exception{
        return name;
    }

}

cmd 进入文件所在位置

 C:\Users\26644\Documents\Javawork\demolyt\src\main\java\demolyt>javadoc -encoding UTF-8 -charset UTF-8 Doc.java

 通过命令行生成javaDoc文档

学会查找使用IDEA生产javaDoc文档

直接在idea中Tools-Generate JavaDoc  可参照:

学会查找使用idea生产javadoc文档_甜甜甜baby的博客-CSDN博客

流程控制

用户交互Scanner

 

package com.test.lyt.scanner;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 9:24
 */
public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收");
        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            //使用next方式接收
            String next = scanner.next();//程序会等待用户输入完毕
            System.out.println("输出的内容为"+next);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关闭
        scanner.close();
    }
}

 

package com.test.lyt.scanner;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 9:32
 */
public class Demo02 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收:");
        //判断是否还有输入
        if(scanner.hasNextLine()){
            String next = scanner.nextLine();
            System.out.println("输出的内容为:"+next);
        }
        scanner.close();

    }
}

 

package com.test.lyt.scanner;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 9:38
 */
public class Demo03 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

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

        String next = scanner.nextLine();

        System.out.println("输出的内容为:" + next);

        scanner.close();

    }
}

package com.test.lyt.scanner;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 9:45
 */
public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i=0;
        float f=0.0f;
        System.out.println("请输入整数:");
        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.test.lyt.scanner;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 9:53
 */
public class Demo05 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:
        Scanner scanner = new Scanner(System.in);

        //和
        double sum=0;
        //计算输入了多少个数字
        int m =0;
        //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
        while(scanner.hasNextDouble()){
            double v = scanner.nextDouble();
            m=m+1;//m++
            sum=sum+v;
            System.out.println("你输入了第"+m+"个数据,然后但前结果sum"+sum);
        }
        System.out.println(m+"个数的和为"+sum);
        System.out.println(m+"个数的平均值是"+(sum/m));
        scanner.close();
    }
}

 顺序结构

 选择结构

  • if单选择结构
  • if双选择结构
  • if多选择结构
  • 嵌套的if结构
  • switch多选择结构

package com.test.lyt.struct;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 10:12
 */
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();
    }

}

请输入内容:
hello
hello
end

Process finished with exit code 0

package com.test.lyt.struct;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 10:18
 */
public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于60及格,小于60不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输出成绩:");
        int i = scanner.nextInt();
        if(i>60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}

请输出成绩:
70
及格

Process finished with exit code 0

package com.test.lyt.struct;

import java.util.Scanner;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 10:18
 */
public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于60及格,小于60不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输出成绩:");
        int i = scanner.nextInt();
        if (i == 100) {
            System.out.println("满分");
        } else if (i < 100 && i >= 90) {
            System.out.println("A等级");
        } else if (i < 90 && i >= 80) {
            System.out.println("B等级");
        } else if (i < 80 && i >= 60) {
            System.out.println("C等级");
        } else if (i < 60 && i >= 0) {
            System.out.println("不及格");
        } else {
            System.out.println("输入不合法");
        }

        scanner.close();
    }
}

请输出成绩:
190
输入不合法

Process finished with exit code 0

switch多选择结构

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 10:51
 */
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("位置等级");
        }
    }
}

及格

Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 10:59
 */
public class SwitchDemo02 {
    public static void main(String[] args) {
        //jdk7的新特性,表达式结果可以是字符串!
        //字符的本质还是数字
        //反编译  Java----class(字节码文件)------反编译(IDEA)
        String name="狂神";
        switch (name){
            case "刘":
                System.out.println("刘");
                break;
            case "狂神":
                System.out.println("狂神");
                break;
            default:
                System.out.println("干啥子");
        }
    }
}

狂神

Process finished with exit code 0

 循环结构

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 11:13
 */
public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1~100
        int i=0;
        while(i<10){
            i++;
            System.out.println(i);
        }
    }

}

1
2
3
4
5
6
7
8
9
10

Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 11:15
 */
public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while(true){
            //等待客户端连接
            //定时检查
            //...
        }
    }

}
package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 11:17
 */
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);
    }
}

5050

Process finished with exit code 0

 

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 11:21
 */
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        do{
            sum+=i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}

5050

Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 11:24
 */
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);

    }
}

----
0

Process finished with exit code 0
 

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 11:29
 */
public class ForDemo01 {
    public static void main(String[] args) {
        int a=1;//初始化条件
        while (a<=10){//条件判断
            System.out.println(a);//循环体
            a+=1;//迭代
        }
        System.out.println("while循环结束!");
        /*
        关于for循环:
        最先执行初始化步骤:可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以空语句
        然后:检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环体后面的语句
        执行一次循环体后,更新循环控制变量(迭代因子控制循环变量的增减)
        再次检测布尔表达式,循环执行上面的过程
         */
        //初始化  条件判断 迭代
        for(int i=1;i<=10;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束!");
    }
}

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 15:18
 */
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;
            }else {//偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }
}

奇数的和:2500
偶数的和:2550

Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 15:23
 */
public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(5*3)==0){//换行
                System.out.println();
                //System.out.print("\n");
                //println输出完会换行
                //print 输出完不会换行
            }
        }
    }
}

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 15:28
 */
public class ForDemo04 {
    public static void main(String[] args) {
        //九九乘法表
        //先打印第一列
        //把固定的1再用一个循环包起来
        //去掉重复项,i<=j
        //调整样式
        for (int j = 1; j <=9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"=" +(i*j)+"\t");
            }
            System.out.println();
        }


    }
}

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    

Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 15:43
 */
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 x:numbers){
            System.out.println(x);
        }
    }
}

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 15:52
 */
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("---");
    }
}

省略从1开始

27
28
29
30
---

package com.test.lyt.struct;

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

123456789
111213141516171819
212223242526272829
313233343536373839
414243444546474849
515253545556575859
616263646566676869
717273747576777879
818283848586878889
919293949596979899
---

Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 16:05
 */
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++) {
                if(i%j==0){
                    continue outer;
                }
            }
            System.out.print(i+" ");
        }
    }
}

101 103 107 109 113 127 131 137 139 149 
Process finished with exit code 0

package com.test.lyt.struct;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 16:18
 */
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();
        }

    }
}

     *
    ***
   *****
  *******
 *********

Process finished with exit code 0
 

方法 

什么是方法

package com.test.lyt.method;

/**
 * @author lyt
 * @description
 * @date 2021/9/24 16:43
 */
public class Demo01 {
     //main方法
    public static void main(String[] args) {
        //实际参数:实际调用传递给他的参数
        int sum = add(1, 2);
        System.out.println(sum);
    }
    //加法
    //形式参数,用来定义作用的
    public static int add(int a,int b){
        return a+b;
    }
}

 方法的定义和调用

 

package demolyt.method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/9/25 19:24
 */
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;//终止方法
        }

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

java值传递

方法的重载 

 

package demolyt.method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/9/25 19:24
 */
public class Demo02 {
    public static void main(String[] args) {
        int max=max(10,20.0);
        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;//终止方法
        }

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

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

 命令行传递参数

有时候你希望运行一个程序时候再传递给它消息,这要靠传递命令行参数给main()函数实现

package com.test.lyt.method;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 9:00
 */
public class Demo03 {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+i+"]:"+args[i]);
        }
    }
}

 可变参数

package com.test.lyt.method;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 9:18
 */
public class Demo04 {
    public static void main(String[] args) {
        //调整可变参数的方法
        printMax(34,3,3,4,67);
        printMax(new double[]{1,2,3});
        test("sss",34,4545,656);

    }
    //可变参数 省略号
    public static void printMax(double...numbers){
        if ( numbers.length==0) {
            System.out.println("No argument passed");
            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 is:"+result);
    }
    //其他类型参数要放在可变参数之前
    public static void test(String s,double...numbers){
        String name=s;
        double result=numbers[0];
        //排序
        for (int i = 1; i < numbers.length; i++) {
            if(numbers[i]>result){
                result=numbers[i];
            }
        }
        System.out.println("s"+s+"The max is:"+result);
    }

}

递归讲解 

 

package com.test.lyt.method;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 9:39
 */
public class Demo05 {
    //5!  递归思想
    public static void main(String[] args) {
        System.out.println(f(5));
    }

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

 数组

 数组概述

数组声明创建

package com.test.lyt.array;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 9:53
 */
public class Demo01 {
    //变量的类型  变量的名称=变量的值;
    //数组类型
    public static void main(String[] args) {
        int[] nums;//首选的定义  1声明一个数组
        int nums2[];//效果相同,但不是首选方法
        nums = new int[10];//2创建一个数组 定义了十个int类型的数字
        //int[] nums1 = new int[10];
        //3给数组元素中赋值
        nums[0] = 1;
        nums[1] = 2;
        //计算所有的元素的和
        int sum = 0;
        //获取数组长度:array.length
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }
        System.out.println("sum:" + sum);
    }
}

数组使用

package com.test.lyt.array;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 11:19
 */
public class Demo02 {
    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;
        System.out.println(b[0]);
    }
}

多维数组

 

数组的使用

  • 普通的For循环
  • For-Each循环
  • 数组作方法入参
  • 数组作返回值
package com.test.lyt.array;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 14:03
 */
public class Demo03 {
    public static void main(String[] args) {
        int[] arrays={1,2,3,4};
        //打印全部的数组元素
        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="+sum);
        System.out.println("---------");

    }
}
package com.test.lyt.array;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 14:18
 */
public class Demo04 {

    public static void main(String[] args) {
        int[] arrays={1,2,3,4};
        //JDK1.5  没有下标
        for(int array:arrays){
            System.out.println(array);
        }
        printArray(arrays);
        int[] reverse = reverse(arrays);
        printArray(reverse);
    }

    //打印数组元素
    public static void printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }
    //反转数组元素
    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;
    }


}

 多维数组

package com.test.lyt.array;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 14:38
 */
public class Demo05 {
    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}};
        printArray(array[0]);
    }

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

 

package com.test.lyt.array;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 14:51
 */
public class Demo06 {
    public static void main(String[] args) {
        int[] a={5,10,2,13,4};
        System.out.println(a);// 反射[I@5b6f7412
        //打印数组元素 Arrays.toString
        System.out.println(Arrays.toString(a));

        Arrays.sort(a);//数组进行排序
        System.out.println(Arrays.toString(a));//[2, 4, 5, 10, 13]

        Arrays.fill(a,2,4,0);//数组填充  从下标2到下标4  [2, 4, 0, 0, 13]
        System.out.println(Arrays.toString(a));
    }
}

冒泡

package com.test.lyt.array;

import java.util.Arrays;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 15:09
 */
public class Demo07 {
    public static void main(String[] args) {

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

    //冒泡排序
    //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
    //2.每一次比较,都会产生一个最大,或者最小的数字
    //3.下一轮则可以少一次排序
    //4.依次循环,直到结束
    public static int[] sort(int[] array) {
        //临时变量
        int temp = 0;
        //外层循环,判断我们这个要走多少次
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            //内存循环,比价判断两个数,如果第一个数,比第二个数大,则交换位置
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j + 1] > array[j]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    flag=true;
                }
            }
            if (flag == false) {
                break;
            }
        }
        return array;
    }
}

 

 

package com.test.lyt.array;

/**
 * @author lyt
 * @description
 * @date 2021/9/26 15:42
 */
public class Demo08 {
    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 aInt:ints){
                System.out.print(aInt+"\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;
                    array2[count][1]=j;
                    array2[count][2]=array1[i][j];
                }
            }
        }
        //输出稀疏数组
        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];
        }
        //输出还原的数组
        System.out.println("输出还原的数组");
        for(int[] ints:array3){
            for(int aInt:ints){
                System.out.print(aInt+"\t");
            }
            System.out.println();
        }
    }
}

输出原始的数组
0    0    0    0    0    0    0    0    0    0    0    
0    0    1    0    0    0    0    0    0    0    0    
0    0    0    2    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
---------------
有效值的个数:2
稀疏数组
11    11    2    
1    2    1    
2    3    2    
================
还原
输出还原的数组
0    0    0    0    0    0    0    0    0    0    0    
0    0    1    0    0    0    0    0    0    0    0    
0    0    0    2    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    

Process finished with exit code 0

面向对象编程OOP     

OO面向对象

初识面向对象

 

方向回顾和加深

package method;

import java.io.IOException;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/14 20:48
 */
//Demo01类
public class Demo01 {
    //main方法
    public static void main(String[] args) {

    }
    /*
    修饰符 返回值类型 方法名(){}
    //方法体
    return 返回值;
     */
    public String sayHello(){
        return "hello,world";
    }
    public void hello(){
        return;
    }
    public int max(int a,int b){
        return a>b?a:b;//三元运算符
    }
    //数组下标越界,Arrayindexoutofbounds
    public void readFile(String file)throws IOException{

    }
}

如果都是一个包下的,不用导入包就能实例化,创建对象; 

package method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 16:32
 */
//学生类
public class Student {
    //非静态方法
    public void say(){
        System.out.println("学生说话了");
    }
}

package method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 15:39
 */
public class Demo02 {

   public static void main(String[] args) {
       //实例化这个类new
       //对象类型 对象名=对象值;
       Student student=new Student();
       student.say();
   }
   //和类一起加载的
   public static void a(){

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

   }

}
package method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 16:40
 */
public class Demo03 {
    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应
        int add = Demo03.add(1, 2);
        System.out.println(add);
    }
    public static int add(int a,int b){
        return a+b;
    }
}
package method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 16:50
 */
//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a=1;
        System.out.println(a);
        Demo04.change(a);
        System.out.println(a);
    }
    //返回值为空
    public static void change(int a){
        a=10;
    }
}
package method;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 16:57
 */
//引用传递:对象,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        Demo05.change(person);
        System.out.println(person.name);//哈哈
    }

    public static void change(Person person){
        //person是一个对象:指向的---》Person person=new Person();这是一个具体的人,可以改变属性
        person.name="哈哈";
    }
}
//定义了一个Person类,有一个属性:name
class Person{
    String name;//null
}

对象的创建分析

package oop.demo02;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 17:15
 */
//学生类
public class Student {
    //属性:字段
    String name;//null
    int age;//0
    //方法
    public void study(){
        System.out.println(this.name+"学生在学习");
    }
}
/*
//类:抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Student类的具体实例
        Student xiaoming = new Student();
        Student student = new Student();
        xiaoming.name="小明";
        xiaoming.age=1;

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

        student.name="小红";
        student.age=3;
        System.out.println(student.name);
        System.out.println(student.age);
 */

package oop.demo02;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 17:53
 */
//java-->class
public class Person {
//一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器
    String name;
//实例化初始值
    //1.使用new关键字,必须要有构造器
    //无参构造
public Person(){
    this.name="哈哈";
}
//有参构造:一旦定义了有参构造,无参构造必须显示定义
    public Person(String name){
        this.name=name;
    }
//alt+insert 出现构造器快捷键
}
/*
 //new 实例化一个对象
        Person person = new Person("嘻嘻");
        System.out.println(person.name);//null

        构造器:
        1.和类名相同
        2.没有返回值
        作用:
        1.new 本质在调用构造方法
        2.初始化对象的值
        注意点:
        1.定义了有参构造之后,如果想使用无参构造,必须显示定义一个无参构造

        快捷键生成构造器
        this. =

 */
package oop.Demo03;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 18:15
 */
public class Pet {
   public String name;
   public int age;
    //无参构造
    public void shout(){
        System.out.println("叫汪汪");
    }
}
/*
        Pet dog = new Pet();
        dog.age=3;
        dog.name="旺财";
        dog.shout();
        System.out.println(dog.age);
        System.out.println(dog.name);
 */

package oop;

import oop.Demo03.Pet;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 17:16
 */
//一个项目应该只存一个main方法
public class Application {
    public static void main(String[] args) {
/*
1.类与对象
类是一个模板:抽象,对象是一个具体的实例
2.方法
定义 调用
3.对象的引用
引用类型:基本类型(8)
对象是通过引用来操作的:栈-->堆
4.属性:字段field 成员变量
默认初始化:
数字:0 0.0
char: u0000
boolean:false
引用:null
修饰符:属性类型 属性名=属性值
5.对象的创建和使用
必须使用new关键字创造对象,构造器Person k=new Person();
对象的属性 k.name
对象的方法 k.sleep()
6.类:
静态的属性  属性
动态的行为  方法
 */

    }
}

package oop.demo04;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 21:01
 */
//类
    /*
    1.提高程序的安全性,保护数据
    2.隐藏代码的实现细节
    3.统一接口
    4.系统可维护增加
     */
public class Student {
    //属性私有
    //名字
    private String name;

    //学号
    private int id;

    //性别
    private char sex;

    //年龄
    private int age;

    //提供一些可以操作这个属性的方法
    //提供一些public 的 set  get方法
    //get获得这个数据

    public String getName() {
        return name;
    }

    //set 给这个数据设置值

    public void setName(String name) {
        this.name = name;
    }

    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;
        }

    }

}
/*
        Student s1 = new Student();
        s1.setName("哈哈");
        //方法名,参数列表
        System.out.println(s1.getName());
        s1.setAge(999);//不合法
        System.out.println(s1.getAge());
 */
/*
1.类与对象
类是一个模板:抽象,对象是一个具体的实例
2.方法
定义 调用
3.对象的引用
引用类型:基本类型(8)
对象是通过引用来操作的:栈-->堆
4.属性:字段field 成员变量
默认初始化:
数字:0 0.0
char: u0000
boolean:false
引用:null
修饰符:属性类型 属性名=属性值
5.对象的创建和使用
必须使用new关键字创造对象,构造器Person k=new Person();
对象的属性 k.name
对象的方法 k.sleep()
6.类:
静态的属性  属性
动态的行为  方法
 */

面向对象的三大特性

继承

package oop.Demo05;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 21:42
 */
//ctrl+H 查看继承的树状图
//Person 父类
//在java中,所有的类都默认直接或间接继承object
public class Person /* extends Object*/ {
    //public
    //protected
    //default
    //private  私有的无法被继承
    protected String name = "hhh";

    private int money = 10_0000_0000;

    public Person() {
        System.out.println("Person无参构造器");
    }

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

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

    public int getMoney() {
        return money;
    }

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

package oop.Demo05;

/**
 * super:注意点:
 * 1.super调用父类的构造方法,必须在构造方法的第一个
 * 2.super必须只能出现在子类的方法或者构造方法中
 * 3.super和this不能同时调用构造方法
 * vs this:
 *  代表的对象不同:
 *  this:本身调用者这个对象
 *  super:代表父类对象的引用
 *  this:没有继承也可以使用
 *  super:只能在继承条件下才能使用
 *  构造方法
 *  this();本类的构造
 *  super();父类的构造
 *
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 21:49
 */
//学生 is 人   子类、派生类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person {
    private String name = "qin";

    public Student() {
        //隐藏代码;调用了父类的无参构造
        super();//调用父类的构造器,必须要在子类构造器的第一行;默认情况不写,自动调用父类的无参
        System.out.println("子类Student无参执行");
    }

    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);
        System.out.println(super.name);
    }
}

package oop;


import oop.Demo05.Student;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 17:16
 */
//一个项目应该只存一个main方法
public class Application {
    public static void main(String[] args) {

       Student student = new Student();
       student.say();
       System.out.println(student.getMoney());
       student.test("哈哈哈哈");
       student.test1();
    }
}

Person无参构造器
子类Student无参执行

1000000000
哈哈哈哈
qin
hhh
Student
Student
Person 

 

 

package oop.demo06;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 22:48
 */
//重写都是方法的重写,和属性无关
//父类
public class B {
    public static void test(){
        System.out.println("B=>test");
    }
    public  void test11(){
        System.out.println("B=>test11");
    }
}
package oop.demo06;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/16 22:47
 */
//继承
public class A extends B{
    public static void test(){
        System.out.println("A=>test");
    }
    //重写
    /*
    重写:需要有继承关系,子类重写父类的方法
    1.方法名必须相同
    2.参数列表列表必须相同
    3.修饰符:范围可以被扩大  public>protected>default>private
    4.抛出的异常,可以被缩小,但不能扩大  ClassNotFoundException--->Exception(大)
    重写:子类的方法和父类的方法必须要一致;方法体不同
    为什么需要重写
    1.父类的功能,子类不一定需要,或者不一定满足
    alt+insert:  override;
     */
    @Override//注解:有功能的注解
    public  void test11(){
        System.out.println("A=>test11");
    }
}

 测试:

  /*
        静态的方法和非静态的方法区别很大
        //静态方法:方法的调用只和左边,定义的数据类型有关
         */

//        //方法的调用只和左边,定义的数据类型有关
//        A a = new A();
//        a.test();//A=>test
//
//        //父类的引用指向了子类
//        B b = new A();
//        b.test();//B=>test



        /*
            重写测试
        */
        A a = new A();
        a.test11();//A=>test11

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

 多态

package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 11:21
 */
public class Person {
    public void run(){
        System.out.println("run");
    }
}

package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 11:21
 */
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }
}
/**
*多态注意事项:
 * 1.多态是方法的多态,属性没有多态
 * 2.父类和子类,有联系  类型转换异常  ClassCastException
 * 3.存在条件:继承关系,方法需要重写,父类引用指向子类对象 father f1 = new Son();
 *
 * 不能重写的有:(不能重写当然也不能实现多态)
 * 1.static 静态方法,属于类,它不属于实例
 * 2.final常量:常量池
 * 3.private 私有方法
 */
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类
Student s1 = new Student();//Student能调用的方法都是自己或者继承父类的   子类指向的引用
Person s2= new Student();//Person 父类型,可以指向子类,但是不能调用子类独有的方法   父类指向的引用
Object s3 = new Student();
s1.run();//son
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
s2.run();//son //应该看Person,但是Student继承Person,并且重写了run方法,子类重写了父类的方法,执行子类的方法

 

package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 11:21
 */
public class Person {
    public void run(){
        System.out.println("run");
    }
}

package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 11:21
 */
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }
}
package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 16:27
 */
public class Teacher extends Person{
}

 /**
  * instanceof
  */
 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);//false
 System.out.println(object instanceof String);//false
 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);//False
 //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);//编译报错
package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 11:21
 */
public class Person {
    public void run(){
        System.out.println("run");
    }
}

package oop.demo07;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 11:21
 */
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }
    public void go(){
        System.out.println("go");
    }
}
//        //类型之间的转化: 父 转 子
//        //高                    低
//        Person obj = new Student();
//        //将student 将这个对象转换为Student类型,我们就可以使用Student类型的方法了
//        //Student student = (Student) obj;
//        // student.go();
//        ((Student)obj).go();

        //类型转换:子类 转 父类   可能会丢失自己的本来的一些方法
        // 低到高
        Student student = new Student();
        Person person=student;
/**
 * 1.父类引用指向子类的对象
 * 2.把子类转换为父类,向上转型;
 * 3.把父类转换为子类,向下转型;强制转换
 * 4.方便方法的调用,减少重复的代码
 * 抽象:封装 继承 多态
 * 抽象类、接口
 */

    }

package oop.demo08;

//静态导入包
import static java.lang.Math.random;
/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 17:32
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
    }
}

package oop.demo08;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 17:24
 */
public class Person {
    //3
    public Person(){
        System.out.println("构造方法");
    }

    //2 附初始值
    {
        //代码块  (匿名代码块)
        System.out.println("匿名代码块");
    }


    //1 只执行一次
    static {
        //静态代码块 类加载的时候就只执行一次静态代码块
        System.out.println("静态代码块");
    }



    public static void main(String[] args) {
        Person person1 = new Person();
        System.out.println("---------");
        Person person2=new Person();
    }
}

 静态代码块
匿名代码块
构造方法
---------
匿名代码块
构造方法

package oop.demo08;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 17:09
 */
public class Student {
    private static int age;//静态的变量   多线程
    private double score;//非静态的变量

    public void run(){

    }
    public static void go(){
        //静态的方法
    }
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println(Student.age);
        System.out.println(student.score);
        System.out.println(student.age);
        student.run();
        Student.go();
    }
}

抽象类和接口

package oop.demo09;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 18:52
 */
//abstract抽象类:类 extends 单继承 (接口可以多继承)
public abstract class Action {
    public void go(){

    }
    //约束,有人帮我们实现
    //abstract抽象方法  只有方法名字,没有方法的实现
    public abstract void doSomething();

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

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 18:54
 */
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法
public class A extends Action{
    @Override
    public void doSomething() {

    }
}

 

package oop.demo10;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 19:12
 */
public interface TimeService {
    void timer();
}

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

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 19:06
 */
//抽象的思维

//interface 定义的关键字 接口都需要有实现类
public interface UserService {
    //属性都是常量 public  static  final  int AGE=99;
    int AGE=99;
    public void run();
    //接口中的所有定义的方法其实都是抽象的public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}

package oop.demo10;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 19:09
 */
//abstract抽象类  单继承
//类可以实现接口 implements 接口   接口可以多继承
    //实现了接口的类,就需要重写接口中的方法
public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void run() {

    }

    @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 timer() {

    }
}

内部类及OOP实践

内部类没有总结

异常

 什么是异常

简单分类

异常体系结构

Error

Exception(运行时异常   非运行时异常)

异常处理机制 

 

package exception.demo01;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 19:53
 */
public class Test {
    public static void main(String[] args) {
        /**
         * 要捕获多个异常,从小到大
         */
        int a=1;
        int b=0;
        try{
            if(b==0){
                throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
            }
            System.out.println(a / b);
            //try监控区域
        }catch (ArithmeticException e){
            //catch捕获异常  想要捕获的异常类型
            System.out.println("程序出现异常,变量b不能为0");
        }catch (Error e) {
            System.out.println("Error");
        }catch (Throwable t){
            System.out.println("Throwable");
        }finally
         {
            //finally 可以不要finally 处理善后工作
            //finally假设IO  资源 关闭
            System.out.println("final");
        }

    }
}

程序出现异常,变量b不能为0
final

Process finished with exit code 0

package exception.demo01;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 20:02
 */
public class Test2 {
    public static void main(String[] args) {

        /**
         * ctrl+alt+t  选中抛出异常
         */
        try {
           new Test2().test(1,0);
        } catch (Exception e) {
            e.printStackTrace();//打印错误的栈信息
        }

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

java.lang.ArithmeticException
    at exception.demo01.Test2.test(Test2.java:24)
    at exception.demo01.Test2.main(Test2.java:15)

Process finished with exit code 0

自定义异常

package exception.demo02;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 20:21
 */
//自定义的异常类
public class MyException extends Exception{
    //传递数字>10
    private int detail;
    public MyException(int a){
        this.detail=a;
    }
   //异常的打印信息
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}
package exception.demo02;

/**
 * @author lyt
 * @version 1.0
 * @date 2021/10/17 20:24
 */
public class Test {
    //可能会存在的异常的方法
    static void test(int a) throws MyException {
        System.out.println("传递的参数为"+a);
        if(a>10){
            throw new MyException(a);//抛出
        }
        System.out.println("ok");
    }

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值