JavaSE概念详解,代码事例,基础,IO,网络,Lambda,反射,模块化,注解,XML解析

更多笔记在这里☞ 全栈之路: https://gitee.com/oldbe/notes 【跳转到】
觉得有用请点个 star ,非常感谢!


数据类型

基础类型

int num1 = 10;
byte num2 = 20;
short num3 = 30;
long num4 = 30L; // Long类型在数字类型加个L
// 浮点数
float num5 = 50.1F;
double num6 = 3.1415926;

// 字符
char name = 'a';
String names = "hello"; // String 不是关键字
// 布尔值
boolean flag = true;

在这里插入图片描述

数组

int [] arr = new int[2];
arr[0] = 0;
arr[1] = 1;
System.out.println(arr.length);
System.out.print(arr[0]);
System.out.print(arr[1]);

int [] arr2 = {
   
    1, 2 };

方法

public class Fun {
   
   
    public  static void main(String[] args) {
   
   
        isEvenNumber(10);
        System.out.println(add(100, 200));
    }
    public static void isEvenNumber(int num) {
   
   
        System.out.println(num % 2 == 0);
    }
    public static int add(int a, int b) {
   
   
        int sum = a + b;
        return sum;
    }
}

方法重载

  • 多个方法在同一个类中
  • 多个方法具有相同的方法名
  • 多个方法的参数不相同,类型或数量
public class Fun {
   
   
    public  static void main(String[] args) {
   
   
        System.out.println(add(1, 2)); // 3
        System.out.println(add(1.1, 2.2)); // 3.3000000000000003
        System.out.println(add(1, 2, 3)); // 6
    }

    public static int add(int a, int b) {
   
   
        return a + b;
    }
    public static double add(double a, double b) {
   
   
        return a + b;
    }
    public static int add(int a, int b, int c) {
   
   
        return a + b + c;
    }
    // 可变参数
    public static int add(int... s) {
   
   
        int sum = 0;
        for (int i = 0;i < s.length; i ++) {
   
   
            sum += s[i];
        }
        return sum;
    }
}

重载: 参数不同,于返回值无关

类和对象

package demo01;

public class Phone {
   
   
    String phoneCode;
    int price;
    public String PhoneCode;

    public void call() {
   
   
        System.out.println("给" + this.getPhoneCode() + "打电话!");
    }
    public void sendMsg() {
   
   
        System.out.println("给" + this.getPhoneCode() + "发短信!");
    }
    public String getPhoneCode() {
   
   
        return this.PhoneCode;
    }
}

对象

public class PhoneDeom {
   
   
    public  static void main(String[] args) {
   
   
        Phone phone = new Phone();
        phone.PhoneCode = "13800000000";
        phone.call();
        phone.sendMsg();
    }
}

两个对象指向不同内存地址

Phone phone = new Phone();
phone.PhoneCode = "13800000000";

Phone phone2 = new Phone();
phone2.PhoneCode = "13800000002";

两个对象指向相同地址

Phone phone = new Phone();
phone.PhoneCode = "13800000000";

Phone phone2 = phone;
phone2.PhoneCode = "13800000002";

成员变量和局部变量

public class Phone {
   
   
    int price; // 成员变量 (类中方法外的变量)

    public void call() {
   
   
        int price; // 局部变量 (类内的变量)
    }
}

封装

private

  • 可以修饰变量 和 方法
  • 被修饰的变量 或 方法,只有在本类中才能访问
public class Student {
   
   
    private String name;

    public void setName(String n) {
   
   
        name = n;
    }
    public String getName() {
   
   
        return name;
    }
}
public class StudentDemo {
   
   
    public  static void main(String[] args) {
   
   
        Student student = new Student();
        student.setName("张三");
        String name = student.getName();
        System.out.println(name);
    }
}

构造方法

public class ConstructionMethod {
   
   
    private int a;
    private int b;
    public ConstructionMethod() {
   
   
        System.out.println("无参构造方法");
    }
    public ConstructionMethod(int a, int b) {
   
   
        this.a = a;
        this.b = b;
    }
    public int add () {
   
   
        return a + b;
    }
}
public class ConstructionMethodDemo {
   
   
    public  static void main(String[] args) {
   
   
        ConstructionMethod constructionMethod = new ConstructionMethod(9, 9);
        int sum = constructionMethod.add();
        System.out.println(sum); // 18
    }
}

String构造方法

String s1 = new String();
System.out.println("s1" + s1);

char [] ch = {
   
    'a', 'b', 'c' };
String s2 = new String(ch);
System.out.println("s2" + s2);

byte[] bys = {
   
    97, 98, 99 };
String s3 = new String(bys);
System.out.println("s3" + s3);
equals 比较字符串相等
char [] chs = {
   
   'A', 'B', 'C'};
String str0 = new String(chs);
String str1 = new String(chs);
String str2 = "Abc";
String str3 = "Abc";
System.out.println(str0 == str1); // false
System.out.println(str0.equals(str1)); // true
System.out.println(str3 == str2); // true
str3.charAt(1) // b 字符串取值

StringBuilder

StringBuilder s1 = new StringBuilder();
StringBuilder s2 = s1.append("hello");
// 添加
s1.append("6666");
s2.append(8888).append("9999").append("----");
System.out.println(s1); // hello666688889999----
System.out.println(s2); // hello666688889999----
System.out.println(s1 == s1);
// 反转
s2.reverse();
System.out.println(s1); // ----999988886666olleh
System.out.println(s2); // ----999988886666olleh

StringBuilder String 换转

String str = s1.toString();
System.out.println(str);
StringBuilder strB = new StringBuilder(str);
System.out.println(strB);

集合

ArrayList<String> arr = new ArrayList<>();

arr.add("hello");
System.out.println(arr); // [hello]

arr.add("hello2");
System.out.println(arr); // [hello, hello2]

arr.add(1, "-hello-");
System.out.println(arr); // [hello, -hello-, hello2]

System.out.println(arr.remove("-hello-")); // true
System.out.println(arr); // [hello, hello2]

arr.remove(1);
System.out.println(arr); // [hello]

arr.set(0, "ttthhhh");
System.out.println(arr); // [ttthhhh]

System.out.println(arr.get(0)); // ttthhhh

System.out.println(arr.size()); // 1

遍历

for (int i = 0; i < arr.size(); i ++) {
    System.out.println(arr.get(i));
}

Demo

package arrList;

public class Student {
   
   
    private String name;
    private int age;

    public Student (String name, int age) {
   
   
        this.name = name;
        this.age = age;
    }

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

    public String getName() {
   
   
        return name;
    }

    public void setAge(int age) {
   
   
        this.age = age;
    }

    public int getAge() {
   
   
        return age;
    }
}
package arrList;

import java.util.ArrayList;

public class StudentDemo {
   
   
    public  static void main(String[] args) {
   
   
        ArrayList<Student> students = new ArrayList<>();
        Student student1 = new Student("ZhangSan", 18);
        Student student2 = new Student("LiSi", 19);
        Student student3 = new Student("WangWu", 20);
        students.add(student1);
        students.add(student2);
        students.add(student3);

        for(int i = 0; i < students.size(); i++) {
   
   
            Student s = students.get(i);
            System.out.println(s.getName() + "," + s.getAge());
                                // ZhangSan,18  // LiSi,19 // WangWu,20
        }
    }
}

改进

package arrList;

import java.util.ArrayList;

public class StudentDemo {
   
   
    public  static void main(String[] args) {
   
   
        ArrayList<Student> students = new ArrayList<>();
        addStudent("ZhangSan", 18, students);
        addStudent("LiSi", 19, students);
        addStudent("WangWu", 20, students);

        for(int i = 0; i < students.size(); i++) {
   
   
            Student s = students.get(i);
            System.out.println(s.getName() + "," + s.getAge());
                                // ZhangSan,18  // LiSi,19 // WangWu,20
        }
    }

    public static void addStudent(String name, int age, ArrayList<Student> arr) {
   
   
        Student student = new Student(name, age);
        arr.add(student);
    }
}

继承

可以使得子类具有父类的属性和方法,还可以在子类中重新定义,追加属性和方法

package extend;

public class Parent {
   
   
    public String name = "hello";

    public void show() {
   
   
        System.out.println("SHOW HELLO!");
    }
}
package extend;

public class Child extends Parent {
   
   
}
package extend;

public class Demo {
   
   
    public  static void main(String[] args) {
   
   
        Child child = new Child();
        child.show();
        System.out.println(child.name);
    }
}

super

package extend;

public class Parent {
   
   
    public String name = "Parent";
}
package extend;

public class Child extends Parent {
   
   
    public String name = "Child";

    public void show() {
   
   
        String name = "Show Fn";
        System.out.println(name); // Show Fn
        System.out.println(this.name); // Child
        System.out.println(super.name); // Parent
    }
}
package extend;

public class Demo {
   
   
    public  static void main(String[] args) {
   
   
        Child child = new Child();
        child.show();
    }
}
继承中构造方法的访问特点
  • 因为子类会继承子类中的数据,可能还会使用父类中的数据。所以,之类在初始化之前,一定要父类先完成初始化;
  • 每个子类构造方法的第一条语句都是 super()
super(); // 有参、无参构造方法

调用父类中的工作方法

重写

package extend;

public class Parent {
   
   
    public void hello(String name) {
   
   
        System.out.println(name + ",你好!");
    }
}
package extend;

public class Child extends Parent {
   
   
    public void hello(String name) {
   
   
        super.hello(name);
        System.out.println(name + ",吃了吗,你嘞!");
    }
}
package extend;

public class Demo {
   
   
    public  static void main(String[] args) {
   
   
        Child child = new Child();
        child.hello("阿卜杜拉沙拉木");
    }
}

私有方法不能被重写(父类私有成员,子类不能继承的)

子类方法访问权限不能更底(public > 默认 > private )

修饰符

包 package

其实就是文件夹,对类进行分类管理

格式 : package 报名;(多级包,用"."分开,如:package com.blm.dto;)

导包 import

import arrList.Student;

修饰符

在这里插入图片描述

状态修饰符

final
  • 修饰方法:表明是最终方法,不能被重写
  • 修饰变量:表明是常量,不能再次赋值
  • 修饰类:表明改类是最终类,不能被继承
public void hello(String name) {
   
   
    
}
// final修饰,不能重写
public final hello(String name) {
   
   
    
}
public final class  Parent {
   
   
    public final int a = 1;
}
static
package demo02;

public class Static {
   
   
    public static String className;
    public String name;
    public int age;

    public void show () {
   
   
        System.out.println(className + "," + name + "," + age + "岁");
    }
}
package demo02;

public class StaticDemo {
    public  static void main(String[] args) {
        Static.className = "计算机科学与技术1班";

        Static s = new Static();
//        s.className = "计算机科学与技术1班";  // 不建议
        s.name = "詹姆斯";
        s.age = 35;
        s.show();

        Static s2 = new Static();
        s2.name = "乔丹";
        s2.age = 65;
        s2.show();
    }
}
static访问特点

静态成员方法,只能访问静态成员方法

非静态的成员方法

  • 能访问静态的成员方法和变量
  • 能访问非静态的成员成员方法和变量

静态的成员方法

  • 只能访问静态的成员方法和变量

多态

同一个对象,再不同时刻表现出来的不同形态

多态的前提和提现

  • 有继承 / 实现关系
  • 有方法重写
  • 有分类引用指向子类对象
package multimode;

public class Animal {
   
   
    public int age = 18;
    public void eat() {
   
   
        System.out.println("动物吃东西!");
    }
}
package multimode;

public class Dog extends Animal {
   
   
    public int age = 19;
    public void eat() {
   
   
        System.out.println("小狗吃肉!");
    }
}
package multimode;

public class Demo {
   
   
    public  static void main(String[] args) {
   
   
        Animal a = new Dog(); // 多态
        a.eat();
        System.out.println(a.age);
    }
}

多态访问特点:

**成员变量:**编译看左边,运行看左边;

**成员方法:**编译看左边,执行看右边;

因为:成员方法有重写,成员变量没有;

抽象类 abstract

package abstract_demo;

public abstract class Animal {
   
    // 抽象类
    public abstract void eat(); // 抽象方法
}
  • 抽象类和抽象方法必须使用abstract关键字修饰
  • 抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类
  • 抽象类不能实例化,
    • 参照多肽的方式,通过子类对象实例化,这叫抽象类多态
  • 抽象类的子类
    • 要么重写抽象类中的所有抽象方法
    • 要么是抽象类
package abstract_demo;

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

抽象类成员的特点

  • 成员变量(变量、常量)

  • 构造方法
    有构造方法不能实例化;用于之类访问父类数据初始化

  • 成员方法
    可以有抽象方法:限定之类必须完成某些动作
    也可以有非抽象方法:提高代码复用性

接口

特点

  • 接口用关键字implement修饰
  • 类实现接口用 implements 表示
  • 接口不能实例化
    接口要参照多态的方式,通过实现类对象实例化,这叫接口多态
    多态的形式:具体类多态,抽象类多态接口多态
    多态的前提:有继承或者实现关系;有方法重写;有父(类 /接口)引用指向(子/ 实现)类对象
package interface_demo;

public interface Jumpping {
   
    // 定义接口
    public abstract void jump();
}
package interface_demo;

public class Cat implements Jumpping {
   
   
    public void jump() {
   
   
        System.out.println("猫跳高");
    }
}
package interface_demo;

public abstract class Dag implements Jumpping {
   
   
}
package interface_demo;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        Jumpping j = new Cat();
        j.jump();
    }
}

接口的成特点

  • 成员变量:只能是常量(默认修饰:static final)

    package interface_demo;
    
    public interface Jumpping {
         
          // 定义接口
        public int num1 = 10;
        public static final int num2 = 10;
        // num1 num2 等价
    }
    
  • 构造方法
    接口没有构造方法,因为接口主要是对行为进行抽象的,没有具体操作
    一个类如果没有父类,默认继承自Object类

    package interface_demo;
    
    public abstract class Dag extends Object implements Jumpping {
         
         
    }
    
  • 成员方法
    只能是抽象方法(默认修饰:public abstract)

    public interface Jumpping {
         
          // 定义接口
        public abstract void jump();
        void jump2();
    }
    

类和接口的关系

实现类可以有多接口,或再继承一个类

public class InterImpl extends Object implements Inter1, Inter2, Inter3 {
   
   }

接口可以继承多个接口

public interface Inter1 extends Inter2, Inter3 {
   
   }

抽象类接口的区别

  • 成员区别

    抽象类 变量,常量;构造方法;抽象、非抽象方法
    接口 常量;抽象方法
  • 关系区别

    类与类 继承,单继承
    类与接口 实现、单实现、多实现
    接口于接口 继承、单继承、多继承
  • 设计理念的区别

    抽象类 对类抽象,抽象属性行为
    接口 对行为抽象,主要是行为

内部类

内部类访问特点

  • 内部类可以直接访问尾部类的成员,包括私有成员
  • 外部类访问内部类的成员,必须创建对象,然后也可以访问私有成员
package inside;

public class A {
   
   
    private String name1 = "helloA";
    public class B {
   
   
        private String name = "helloB";
        public void show () {
   
   
            System.out.println(name1);
        }
    }

    public void show () {
   
   
        B b = new B();
        b.show();
        System.out.println(b.name);
    }
}

成员内部类

  • 在类的成员位置:成员内部类

    package inside;
    
    public class A {
         
         
        public class B {
         
         
        }
    }
    
    public class Demo {
         
         
        public static void main(String[] args) {
         
         
            A.B ab = new A().new B();
            // 内部类对象(不常见,内部类目的是,不让外部直接访问)
        }
    }
    
  • 在类的局部位置:局部内部类

package inside;

public class A {
   
   
    private String name1 = "helloA";
    public void method () {
   
   
      class B {
   
   
          public void show () {
   
   
              System.out.println(name1);
          }
      }
      B b = new B();
      b.show();
    }
}

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        A a = new A();
        a.method();
    }
}

局部内部类是在方法中定义的类,所以外界是无法直接使用的,需要内部创建对象

内部类可以直接访问外部类的成员、方法内的局部变量

匿名内部类

package inside;

public interface I {
   
   
    void show();
}
package inside;

public class A {
   
   
    private String name1 = "helloA";
    public void method () {
   
   
       /**
        new I() {
            public void show() {
                System.out.println("内部类");
            }
        }.show();
        */
        I i =  new I() {
   
   
            public void show() {
   
   
                System.out.println("内部类");
            }
        };
        i.show();
    }
}
package inside;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        A a = new A();
        a.method();
    }
}

内部类开放中的应用

package inside;

public interface I {
   
   
    void eat();
    void doing();
}
package inside;

public class OP {
   
   
    public void operation(I i) {
   
   
        i.eat();
        i.doing();
    }
}
package inside;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        OP op = new OP();

        op.operation(new I() {
   
   
            @Override
            public void eat() {
   
   
                System.out.println("猫吃鱼!");
            }
            
            @Override
            public void doing() {
   
   
                System.out.println("猫跳高!");
            }
        });

        op.operation(new I() {
   
   
            @Override
            public void eat() {
   
   
                System.out.println("狗吃骨头!");
            }

            @Override
            public void doing() {
   
   
                System.out.println("狗跳墙!");
            }
        });
    }
}

常用 API

Math

package math;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        System.out.println(Math.abs(-99)); // 绝对值
        System.out.println(Math.ceil(12.03) + "," + Math.ceil(12.93)); // 先上取整 返回double
        System.out.println(Math.floor(12.03) + "," + Math.floor(12.93)); // 先上取整 返回double
        System.out.println(Math.round(12.03) + "," + Math.round(12.93)); // 四舍五入 返回int
    }
}

System

package math;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        System.out.println("开始");
        System.exit(0); // 终止当前运行的虚拟机,非0表示异常终止
        System.out.println("结束");

        System.out.println(System.currentTimeMillis()); // 时间戳
    }
}

Object

Object是类层次结构的根。每个类都有Object作为超类。所有对象(包括数组)都实现了这个类的方法。

toString

System.out.println 调用了超类Object的toString()方法,不变阅读,

重写

public String toString() {
   
   
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
equals

Object类的equals方法,比较事会地址比较,

但两个对象中比较数据是否相同是,重写equals方法:

public boolean equals(Object o) {
   
   
        if (this == o) return true;
    // 判断两个类是否来自同一个类
        if (o == null || getClass() != o.getClass()) return false;

        Cat cat = (Cat) o;

        if (age != cat.age) return false; // 比较age是否相同
        return Objects.equals(name, cat.name); // 比较name是否相同
    }

Arrays

toSring & sort
import java.util.Arrays;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        int arr [] = {
   
    1, 3, 9, 5, 6 };
        System.out.println(Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

基本类型包装类

Integer
public class Demo {
   
   
    public static void main(String[] args) {
   
   
        System.out.println(Integer.MAX_VALUE); // 2147483647
        System.out.println(Integer.MIN_VALUE); // -2147483648
    }
}

等到Integer对象

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        Integer i1 = Integer.valueOf(100);
        System.out.println(i1);
        Integer i2 = Integer.valueOf("100");
        System.out.println(i2);
    }
}

字符串分割

package math;

import java.util.Arrays;

public class Demo {
   
   
    public static void main(String[] args) {
   
   
        String s = "18 69 52 31 99 26 1 52";
        String arr [] = s.split(" ");
        int [] arrInt = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
   
   
            arrInt[i] = Integer.parseInt(arr[i]);
        }
        Arrays.sort(arrInt);
        System.out.println(Arrays.toString(arrInt)); // [1, 18, 26, 31, 52, 52, 69, 99]
         StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arrInt.length; i++) {
   
   
            if (i == arr.length - 1) {
   
   
                stringBuilder.append(arrInt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值