[Java]面向对象高级篇

news2024/7/4 5:45:12

文章目录

  • 包装类
    • 包装类层次结构
    • 基本类型包装类
    • 特殊包装类
  • 数组
    • 一维数组
    • 多维数组
    • 可变长参数
  • 字符串
    • String类
    • StringBuilder类
  • 内部类
    • 成员内部类
    • 静态内部类
    • 局部内部类
    • 匿名内部类
    • Lambda表达式
    • 方法引用
  • 异常机制
    • 自定义异常
    • 抛出异常
    • 异常的处理
  • 常用工具类
    • 数学工具类
    • 随机数
    • 数组工具类

包装类

包装类层次结构

在这里插入图片描述

基本类型包装类

package com.test.entity;
public class Main {
    public static void main(String[] args) {
        Integer a = new Integer(10);
        Integer b = new Integer(10);
        System.out.println(a == b);
    }
}

public static void main(String[] args) {
    Integer a = 10, b = 10;
    System.out.println(a == b);
}

package com.test.entity;
public class Main {
    public static void main(String[] args) {
        Integer a = 128, b = 128;
        System.out.println(a == b);
    }
}

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)   
        //这里会有一个IntegerCache,如果在范围内,那么会直接返回已经提前创建好的对象
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

false

true

false

IntegerCache会默认缓存-128~127之间的所有值,将这些值提前做成包装类放在数组中存放

package com.test.entity;
public class Main {
    public static void main(String[] args) {
        Integer i = Integer.valueOf("5555");
        System.out.println(i);
        Integer j = Integer.parseInt("5555");
        System.out.println(j);
    }
}
package com.test.entity;
public class Main {
    public static void main(String[] args) {
        Integer i = Integer.decode("036");
        System.out.println(i);
        System.out.println(Integer.toHexString(166));
    }
}

30
a6

特殊包装类

package com.test.entity;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
public class Main {
    public static void main(String[] args) {
        BigDecimal i = BigDecimal.valueOf(10);
        i = i.divide(BigDecimal.valueOf(3), 100, RoundingMode.CEILING);
        //计算10/3的结果,精确到小数点后100位
        //RoundingMode是舍入模式,就是精确到最后一位时,该怎么处理,这里CEILING表示向上取整
        System.out.println(i);
        BigInteger j = BigInteger.valueOf(Long.MAX_VALUE);
        j = j.pow(100); //来个100次方吧
        System.out.println(j);
    }
}

数组

一维数组

package com.test.entity;
public class Main {
    public static void main(String[] args) {
        int[] array = new int[]{1,2,43,5};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
        //foreach
        for (int a:array){
            System.out.print(a+" ");
        }
    }
}
public static void main(String[] args) {
    String[] arr = new String[10];
    Object[] array = arr;    //引用类型的数组同样支持向上转型
    Object[] arr = new Object[10];
    String[] array = (String[]) arr;   //引用类型数组也支持向下转型
}

多维数组

package com.test.entity;
public class Main
{
    public static void main(String[] args)
    {
        int[][] arr = new int[][]{{1, 2}, {3, 4}, {5, 6}};
        for (int i = 0; i < arr.length; i++)
        {    //要遍历一个二维数组,那么我们得一列一列一行一行地来
            for (int j = 0; j < arr[0].length; j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
}

可变长参数

package com.test.entity;
public class Main
{
    public static void main(String[] args)
    {
       test("co","le","ak","66");
    }
    public static void test(String... strings){   //strings这个变量就是一个String[]类型的
        for (String string : strings) {
            System.out.print(string);   //遍历打印数组中每一个元素
        }
    }
}

字符串

String类

package com.test.entity;
public class Main
{
    public static void main(String[] args) {
        String str1 = "Hello World";
        String str2 = "Hello World";
        System.out.println(str1 == str2);
    }
}

直接使用双引号创建的字符串,如果内容相同,为了优化效率,那么始终都是同一个对象

package com.test.entity;
public class Main
{
    public static void main(String[] args) {
        String str1 = new String("Hello World");
        String str2 = new String("Hello World");
        System.out.println(str1 == str2);
        System.out.println(str1.equals(str2));   //字符串的内容比较,一定要用equals
    }
}

false
true

package com.test.entity;
public class Main
{
    public static void main(String[] args) {
        String str = "Hello World";
        String[] strings = str.split(" ");
        for (String string : strings)
        {
            System.out.println(string);
        }
        String sub = str.substring(6, 8);   //分割字符串,返回新的子串对象,这里返回67字符
        System.out.println(sub);
    }
}

Hello
World
Wo

package com.test.entity;
public class Main
{
    public static void main(String[] args)
    {
        String str = "Hello World";
        char[] chars = str.toCharArray();
        for (char aChar : chars)
        {
            System.out.println(aChar);
        }
        char[] chars2 = new char[]{'c', 'o', 'l'};
        String str2 = new String(chars2);
        System.out.println(str2);
    }
}

StringBuilder类

package com.test.entity;
public class Main
{
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();   //一开始创建时,内部什么都没有
        builder.append("AAA");   //我们可以使用append方法来讲字符串拼接到后面
        builder.append("BBB");
        System.out.println(builder);   //当我们字符串编辑完成之后,就可以使用toString转换为字符串了
        builder.delete(2, 5);   //删除234这个范围内的字符
        System.out.println(builder.toString());
    }
}

AAABBB
AAB

内部类

成员内部类

package com.test.entity;
public class Main
{
    public static void main(String[] args) {
        Test a = new Test("小明");
        Test.Inner inner = a.new Inner();   //依附于a创建的对象,那么就是a的
        inner.test("coleak");
    }
}

package com.test.entity;
public class Test {
    private final String name;
    public Test(String name){
        this.name = name;
    }
    public class Inner {
        String  name;
        public void test(String name)
        {
            System.out.println("方法参数的name = "+name);    //就近原则
            System.out.println("成员内部类的name = "+this.name);   //表示内部类对象
            System.out.println("成员内部类的name = "+Test.this.name);
            //如果需要指定为外部的对象,那么需要在前面添加外部类型名称
        }
    }
}

方法参数的name = coleak
成员内部类的name = null
成员内部类的name = 小明

静态内部类

package com.test.entity;
public class Test {
    private final String name;
    public Test(String name){
        this.name = name;
    }
    public static class Inner {
        public void test(){
            System.out.println("我是静态内部类!");
        }
    }
}

package com.test.entity;
public class Main
{
    public static void main(String[] args) {
        Test.Inner inner = new Test.Inner();
        inner.test();
    }
}

仅使用静态内部类时,不会加载外部类,而是直接加载内部类,完成内部静态类的初始化和构造方法

局部内部类

public class Test {
    public void hello(){
        class Inner{   //局部内部类跟局部变量一样,先声明后使用
            public void test(){
                System.out.println("我是局部内部类");
            }
        }
        
        Inner inner = new Inner();   //局部内部类直接使用类名就行
        inner.test();
    }
}

匿名内部类

public class Main {
    public static void main(String[] args) {
//        Student student = new Student();   //抽象类和接口均无法实例化
//        student.test();
    }
}


public abstract class Student {
    public abstract void test();
    protected String a="coleak";
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student() {
            int b;
            @Override
            public void test() {
                System.out.println(a);
                System.out.println("我是匿名内部类的实现!");
            }
        };
        student.test();
    }
}

coleak
我是匿名内部类的实现!

Lambda表达式

如果一个接口中有且只有一个待实现的抽象方法,那么我们可以将匿名内部类简写为Lambda表达式

package com.test.entity;
public interface Study {
    String stu(String str);
    static int num=10;
}

import com.test.entity.Study;
public class Main
{
    public static void main(String[] args)
    {
        Study study = (a) ->
        {
            System.out.println(Study.num);
                return "我是学习方法!"+"   "+a;
        };
        System.out.println(study.stu("coleak"));
    }
}

10
我是学习方法! coleak

方法引用

package com.test.entity;

public interface Study {
        int sum(int a, int b);
}

import com.test.entity.Study;
public class Main {
    public static void main(String[] args) {
    //Study study = (a, b) -> a + b;
    Study study = (a, b) -> Integer.sum(a, b);   //直接使用Integer提供求和方法
        System.out.println(study.sum(10, 20));
    }
}

public static void main(String[] args) {
    Study study = Integer::sum;    //双冒号来进行方法引用,静态方法使用 类名::方法名 的形式
    System.out.println(study.sum(10, 20));
}
public static int sum(int a, int b) {
    return a + b;
}

public static void main(String[] args){
    Main main = new Main();
    Study study = String::new;
    }

异常机制

自定义异常

异常两大类,编译时异常,运行时异常

抛出异常

当别人调用我们的方法时,如果传入了错误的参数导致程序无法正常运行,这时我们就可以手动抛出一个异常来终止程序继续运行下去,同时告知上一级方法执行出现了问题。

我们在重写方法时,如果父类中的方法表明了会抛出某个异常,只要重写的内容中不会抛出对应的异常我们可以直接省去:

public class Main
{
    public static void main(String[] args) throws Exception {
        System.out.println(test(30,4));
        System.out.println(test(30,0));
        System.out.println("coleak");
    }
    public static int test(int a, int b) throws Exception{
        if(b == 0)
            throw new RuntimeException("被除数不能为0");  //使用throw关键字来抛出异常
        return a / b;
    }
}

异常的处理

public class Main
{
    public static void main(String[] args) {
        try {    //使用try-catch语句进行异常捕获
            Object object = null;
            object.toString();
        } catch (NullPointerException e){
            e.printStackTrace();   //打印栈追踪信息
            System.out.println("异常错误信息:"+e.getMessage());   //获取异常的错误信息
        }
        System.out.println("程序继续正常运行!");
    }
}


import java.io.IOException;

public class Main
{
    public static void main(String[] args) throws IOException {
        test(10);    //必须要进行异常的捕获,否则报错
    }

    private static void test(int a) throws IOException {  //明确会抛出IOException
        throw new IOException();
    }
}

如果已经是主方法了,那么就相当于到顶层了,此时发生异常再往上抛出的话,就会直接交给JVM进行处理,默认会让整个程序终止并打印栈追踪信息。

import java.io.IOException;
public class Main
{
    public static void main(String[] args) throws IOException {
        try {
            int[] arr = new int[1];
            arr[1] = 100;    //这里发生的是数组越界异常,它是运行时异常的子类
        } catch (RuntimeException e){  //使用运行时异常同样可以捕获到
            System.out.println("捕获到异常");
        }
    }
}

如果我们要捕获的异常,是某个异常的父类,那么当发生这个异常时,同样可以捕获到

try {
  //....
}
catch (NullPointerException e){}
catch (IndexOutOfBoundsException e){} 
catch (RuntimeException e){}

try {
     //....
} catch (NullPointerException | IndexOutOfBoundsException e) {  //用|隔开每种类型即可
}

当代码可能出现多种类型的异常时,我们希望能够分不同情况处理不同类型的异常,就可以使用多重异常捕获

try {
    //....
}catch (Exception e){
            
}finally {
      System.out.println("lbwnb");   //无论是否出现异常,都会在最后执行
}

无论是否出现异常,都会在最后执行任务,可以交给finally语句块来处理

try语句块至少要配合catchfinally中的一个

常用工具类

数学工具类

import java.io.IOException;
public class Main
{
    public static void main(String[] args) {
        //Math也是java.lang包下的类,所以说默认就可以直接使用
        System.out.println(Math.pow(5, 3.5));   //我们可以使用pow方法直接计算a的b次方
        Math.abs(-1);    //abs方法可以求绝对值
        Math.max(19, 20);    //快速取最大值
        Math.min(2, 4);   //快速取最小值
        System.out.println(Math.sqrt(9));    //求一个数的算术平方根
    }
}

import java.io.IOException;
public class Main
{
    public static void main(String[] args) {
        Math.sin(Math.PI / 2);     //求π/2的正弦值,这里我们可以使用预置的PI进行计算
        Math.cos(Math.PI);       //求π的余弦值
        Math.tan(Math.PI / 4);    //求π/4的正切值

        System.out.println(Math.asin(1));     //求arcsin1的值
        Math.acos(1);
        Math.atan(0);
    }
}


public static void main(String[] args) {
    Math.log(Math.E);    //e为底的对数函数,其实就是ln,我们可以直接使用Math中定义好的e
    Math.log10(100);     //10为底的对数函数
    //利用换底公式,我们可以弄出来任何我们想求的对数函数
    double a = Math.log(4) / Math.log(2);   //这里是求以2为底4的对数,log(2)4 = ln4 / ln2
    System.out.println(a);
}
import java.io.IOException;
public class Main
{
    public static void main(String[] args) {
        System.out.println(Math.log(Math.E));    //e为底的对数函数,其实就是ln,我们可以直接使用Math中定义好的e
        System.out.println(Math.log10(100));     //10为底的对数函数
        //利用换底公式,我们可以弄出来任何我们想求的对数函数
        double a = Math.log(4) / Math.log(2);   //这里是求以2为底4的对数,log(2)4 = ln4 / ln2
        System.out.println(a);
    }
    }

1.0
2.0
2.0

随机数

import java.util.Random;
public class Main
{
    public static void main(String[] args) {
        Random random = new Random();   //创建Random对象
        for (int i = 0; i < 30; i++) {
            System.out.print(random.nextInt(100)+"      ");  
            //nextInt方法可以指定创建0 - x之内的随机数
        }
    }
    }

数组工具类

import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 4, 5, 8, 2, 0, 9, 7, 3, 6};
        System.out.println(Arrays.toString(arr));
        Arrays.sort(arr);    //可以对数组进行排序,将所有的元素按照从小到大的顺序排放
        System.out.println(Arrays.toString(arr));
        int[] arr2 = new int[10];
        Arrays.fill(arr2, 66);
        System.out.println(Arrays.toString(arr2));
        int[] target = Arrays.copyOf(arr, 10);
        System.out.println(Arrays.toString(target));   //拷贝数组的全部内容,并生成一个新的数组对象
        System.out.println(arr == target);
        int[] target2 = Arrays.copyOfRange(arr, 3, 5);   //也可以只拷贝某个范围内的内容
        System.out.println(Arrays.toString(target2));
        int[] target3 = new int[10];
        System.arraycopy(arr, 2, target3, 4, 5);   //使用System.arraycopy进行搬运
        System.out.println(Arrays.toString(target3));
        System.out.println(Arrays.binarySearch(target3, 5));
        int[][] array = new int[][]{{2, 8, 4, 1}, {9, 2, 0, 3}};
        System.out.println(Arrays.deepToString(array));
        int[][] a = new int[][]{{2, 8, 4, 1}, {9, 2, 0, 3}};
        int[][] b = new int[][]{{2, 8, 4, 1}, {9, 2, 0, 3}};
        System.out.println(Arrays.equals(a, b));   //equals仅适用于一维数组
        System.out.println(Arrays.deepEquals(a, b));
    }
}

[1, 4, 5, 8, 2, 0, 9, 7, 3, 6]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[66, 66, 66, 66, 66, 66, 66, 66, 66, 66]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
false
[3, 4]
[0, 0, 0, 0, 2, 3, 4, 5, 6, 0]
7
[[2, 8, 4, 1], [9, 2, 0, 3]]
false
true

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/425045.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【AIGC】Visual ChatGPT 视觉模型深度解析

欢迎关注【youcans的AGI学习笔记】原创作品 【AIGC】Visual ChatGPT 视觉模型深度解析1. 【Visual- ChatGPT】火热来袭2. 【Visual-GPT】操作实例2.1 处理流程2.2 操作实例3. 【Visual-GPT】技术原理分析3.1 技术原理3.2 系统架构3.3 模块说明3.4 Prompt Manager 功能与规则3.5…

Distilling Knowledge via Knowledge Review(引言翻译)

翻译得可能不太准确&#xff0c;希望有能力的各位批评指正&#xff01; Introduction 第一段 深度卷积神经网络&#xff08;CNN&#xff09;在计算机视觉多数任务中取得了显著的成功。 然而&#xff0c;卷积网络的成功往往伴随着相当大的计算和内存消耗&#xff0c; 使得将…

人工智能交互系统界面设计(Tkinter界面设计)

文章目录前言一、项目介绍二、项目准备三、项目实施1.导入相关库文件2.人脸信息验证功能3.语音交互与TCP数据通信4.数据信息可视化四、相关附件前言 在现代信息化时代&#xff0c;图形化用户界面&#xff08;Graphical User Interface, GUI&#xff09;已经成为各种软件应用和…

SpringBoot——Scheduled定时任务

目录 1.静态定时任务 2.动态定时任务 在一些业务场景中&#xff0c;我们需要定义一些任务在我们指定的时间或是每隔一个时间段就自动执行&#xff0c;来作为任务的前提&#xff0c;保证业务的执行。比如&#xff1a;我们需要一个定时任务&#xff0c;每天早上6点执行&#xf…

【springcloud 微服务】Spring Cloud Alibaba Nacos使用详解

目录 一、前言 二、nacos介绍 2.1 什么是 Nacos 2.2 nacos 核心能力 2.2.1 服务发现和服务健康监测 2.2.2 动态配置服务 2.2.3 动态 DNS 服务 2.2.4 服务及其元数据管理 2.2.5 nacos生态地图 2.3 与其他配置中心对比 三、nacos快速部署 3.1 获取安装包 3.2 修改脚…

【分享NVIDIA GTC 23大会干货】加速生成式AI在生物学和医疗领域的应用

【分享NVIDIA GTC 23大会干货】加速生成式AI在生物学和医疗领域的应用1. NVIDIA医疗领域AI计算平台——NVIDIA CLARA2. NVIDIA CLARA医学影像子平台——MONAI3. NVIDIA CLARA医疗设备子平台——Holoscan4. NVIDIA基因组学解决方案Parabricks5. NVIDIA药物研发解决方案6. 个人思…

互联网医院源码|互联网医院软件体现智慧医疗的优势

现在大家看病一般都会直接在互联网医院平台上去就诊&#xff0c;每次大家需要看病时&#xff0c;可以在手机上直接去预约指定的医生&#xff0c;同城周边的所有医院都是可以去直接选择的&#xff0c;这样也可以去帮助大家节省很多的看病时间&#xff0c;在互联网医院软件中所具…

【ApiPost】实现【gRPC】调试【上手篇】

ApiPost下载地址 下载中心-Apipost-中文版接口调试与文档管理工具Apipost官方下载中心为您提供Apipost软件最新版本,其中包括Windows、Mac、Linux等多个客户端的安装包&#xff0c;Apipost下载就上Apipost.cn&#xff0c;国内专业的接口测试软件,一键生成API文档。https://www…

中核科技:科技匠心 智启未来

​  2023 年4月 13—15 日&#xff0c;2023年易派客工业品展览会、石油石化工业展览会、第七届中国石油和化工行业采购年会&#xff0c;在苏州国际博览中心胜利召开。本次展会展览面积53000平方米&#xff0c;参展企业500余家&#xff0c;汇集了中国工业制造领域的大型国企央…

Parcel 实践指南

Parcel 是一个极速零配置的 Web 应用程序打包器。它的零配置特性使得开发者可以更快速地进行项目的构建。本文将向你展示如何在项目中实践 Parcel&#xff0c;并讨论一些性能优化策略以及不同场景下的最佳实践。 总结 Parcel 是一个强大而灵活的打包工具&#xff0c;它可以让你…

【Python_Scrapy学习笔记(八)】基于Scrapy框架实现多级页面数据抓取

基于Scrapy框架实现多级页面数据抓取 前言 本文中介绍 如何基于 Scrapy 框架实现多级页面数据的抓取&#xff0c;并以抓取汽车之家二手车数据为例进行讲解。 正文 在介绍如何基于 Scrapy 框架实现多级页面数据的抓取之前&#xff0c;先介绍下 Scrapy 框架的请求对象 reques…

Linux超级强大的十六进制dump工具:XXD命令,我教你应该如何使用!

在Linux操作系统中&#xff0c;XXD是一个十六进制dump工具&#xff0c;可以将二进制文件转换为十六进制表示&#xff0c;并以可读的形式显示。XXD命令可用于显示文件内容、编辑文件等用途。本文将介绍如何在Linux中使用XXD命令。 安装XXD命令 通常情况下&#xff0c;XXD命令已…

Java面试题总结 | Java基础部分2(持续更新)

文章目录反射的优缺点BIO、AIO、NIO同步异步概念**阻塞与非阻塞****BIO****NIO****AIO**总结设计模式的好处设计模式一定是好用的吗Integer.ValueOf和new Integer的区别Integer.parseInt(s)与Integer.valueOf(s)的区别String是线程安全的吗&#xff1f;StringBuffer和StringBui…

开源GPT-4小羊驼(Vicuna)快速上手指南

小羊驼&#xff08;Vicuna)是什么 Vicuna: 一个开源的GPT&#xff0c;宣称实现了GPT-4 90%的功能。 UC伯克利学者联手CMU、斯坦福等&#xff0c;再次推出一个全新模型70亿/130亿参数的Vicuna&#xff0c;俗称「小羊驼」&#xff08;骆马&#xff09;。 并且和其他以往不同的是…

数据库管理-第六十五期 Oracle 23c新特性(20230411)

数据库管理 2023-04-11第六十五期 Oracle 23c新特性1 免费版23c目录结构2 新特性总结第六十五期 Oracle 23c新特性 上一期装了免费版23c&#xff0c;这一期根据安装的数据库&#xff0c;对Oracle 23c的部分新特性进行实验展示。 1 免费版23c目录结构 通过RPM包安装的免费版2…

静态时序分析Static Timing Analysis1——STA概述、标准工艺库、时钟、IO约束的建立

文章目录前言一、静态时序分析概述1、时序路径分类2、STA和动态仿真比较3、PVT4、不同时钟域5、建立时间、保持时间6、恢复时间、移除时间二、标准工艺库1、标准单元延时模型2、slew derate三、STA约束的建立1、时钟约束1.1 时钟定义1.2 时钟不确定性1.3 时钟延时1.4 生成时钟2…

2023年4月的编程语言排行榜,有你中意的开发语言吗?

编程世界变幻莫测&#xff0c;编程语言也是层出不穷&#xff0c;每隔一段时间就有新的风口出现。2023年的风口非人工智能莫属&#xff0c;人工智能领域中不可获取的编程语言就是Python&#xff0c;作为在算法、数据方面有独特优势的编程语言&#xff0c;从去年开始就展现了它不…

Linux03——文件系统及结构、命令

目录 一、前言 二、文件目录 三、文件系统 四、文件目录命令 五、系统信息命令 六、通讯网络命令 七、磁盘类命令 八、进程管理命令 一、前言 Linux特点是开放性遵循OSI国际标准&#xff1b;多用户每个用户有各自权限&#xff1b;多任务&#xff1b;GUI和系统调用界面&…

Java每日一练(20230411)

目录 1. 同构字符串 &#x1f31f; 2. 随机字符串 &#x1f31f; 3. 交错字符串 &#x1f31f;&#x1f31f; &#x1f31f; 每日一练刷题专栏 &#x1f31f; Golang每日一练 专栏 Python每日一练 专栏 C/C每日一练 专栏 Java每日一练 专栏 1. 同构字符串 给定两个…

京东技术专家首推:Spring微服务架构设计,GitHub星标128K

前言 本书提供了实现大型响应式微服务的实用方法和指导原则&#xff0c;并通过示例全面 讲解如何构建微服务。本书深入介绍了Spring Boot、Spring Cloud、 Docker、Mesos和Marathon&#xff0c;还会教授如何用Spring Boot部署自治服务&#xff0c;而 无须使用重量级应用服务器…