JAVA SE

這裏寫自定義目錄標題

一、準備工作

java的歷史版本

在這裏插入圖片描述

jdk、jre、jvm

jdk:java development kit【java開發工具包】
jre:java runtime environmen【java運行環境】
jvm:java virtual machine【Java虛擬機】
在這裏插入圖片描述

IDE

integrated development environment:集成開發環境
是整合代碼的編寫、運行、分析、調試等一系列功能的開發軟件
src目錄 是source的縮寫;程序的後綴是.java
out目錄 java程序的輸出目錄,存放.class的字節碼文件,生成字節碼文件的過程叫做編譯

編譯運行的過程

在這裏插入圖片描述

IDEA的基本配置

在這裏插入圖片描述

IDEA的快捷鍵

在這裏插入圖片描述
ctrl b 或者 ctrl 左擊:查看源代碼
ctrl d 複製(默認是一行)(否者複製選中內容)
ctrl y 刪除
ctrl n 通過類名 定位文件
ctrl f 在當前類中查找 esc可以關閉查找
ctrl / 註釋
ctrl alt l 格式化代碼
ctrl alt ⬅ 上一次編輯的地方
ctrl alt ➡ 下一次編輯的地方
alt shift 上下箭頭 移動行
main方法 psvm
輸出 sout
alt 回車 導包
alt insert 構造、getter、setter
alt 回車 重寫的快捷鍵
iter 增強for的快捷鍵

java編碼規範

在這裏插入圖片描述

二、基本程序結構

三種註釋

/**
 * 文檔註釋
 */
public class HelloWorld {
    /**
     * 文檔註釋
     */
    public static void main(String[] args) {
        /*
        多行註釋
         */
        System.out.println("2222222");
        System.out.println("2222222");
        
        //單行註釋
        System.out.println("2222222");
    }
}

常量

字面值常量

字符串常量、整數常量、小數常量、字符常量、布爾常量、空常量

自定義常量

數據類型

在這裏插入圖片描述

類型轉換:默認和強制轉換

命名方式

在這裏插入圖片描述

運算符

在這裏插入圖片描述

ASCII表

在這裏插入圖片描述

複製運算符

在這裏插入圖片描述
在這裏插入圖片描述

三元運算符

Scanner的用法

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        System.out.println(i);
    }

switch語句

在這裏插入圖片描述

do while 循環語句

在這裏插入圖片描述

break 和 continue

三、方法

方法、方法重載

四、數組

數組的定義

在這裏插入圖片描述
在這裏插入圖片描述
格式三是格式二的簡化

數組的初始化:動態、靜態

在這裏插入圖片描述

java的內存分配:方法區、堆、棧

在這裏插入圖片描述

參數傳遞

基本類型作爲參數傳遞,傳遞的是 值【按值傳遞】
引用類型作爲參數傳遞,傳遞的是 地址值【按引用傳遞】

五、類和對象

定義類

一個類包括:成員變量成員方法

創建類的內存圖解

在這裏插入圖片描述

對象作爲參數的內存圖解

在這裏插入圖片描述

成員變量和局部變量的區別

  • 內存中的位置:成員變量:堆內存;局部變量:棧內存
  • 生命週期:成員變量:和對象的創建消失同步;局部變量:隨着方法的調用而存在,隨着方法調用完畢而消失
  • 就近原則,除非用this關鍵字

封裝(類和對象都有這種思想)

封裝的好處:

  • 安全性
  • 複用性
  • 將複雜簡單化

private和public

private一般用來形容成員變量
public一般用來形容成員方法

this關鍵字

表示當前對象的引用

構造方法(可以重載)

  • 未提供構造方法,系統默認給你一個無參構造方法
  • 已提供了,系統就不再提供
  • 構造方法可以重載

六、繼承

繼承的要點

子類只能繼承父類非私有成員(成員變量、成員方法)

繼承的優點

  • 功能複用
  • 便於擴展新功能
  • 結構清晰,簡化
  • 易於維護

繼承的缺點

  • 打破了封裝性
  • 高耦合性

程序設計的追求

高內聚、低耦合

就近原則,super關鍵字

先找局部變量
沒有的話,再找本類的成員變量
沒有的話,再去父類的成員位置找

繼承中成員變量使用圖解

在這裏插入圖片描述
在這裏插入圖片描述

在子類方法中調用父類方法 super

public class Fu {
    public void fuMethod(){
        System.out.println("父類方法");
    }
}
public class Zi extends  Fu {
    public void ziMethod(){
        super.fuMethod();
        System.out.println("子類方法");
    }
}
public class Test {
    public static void main(String[] args) {
        Zi zi = new Zi();
        zi.ziMethod();
    }
}

結果:

父類方法
子類方法

子父類構造方法的繼承問題

【如果一個類沒有構造方法,默認給一個無參構造方法】
子類構造方法第一行默認給一個super()
注意:
如果父類沒有無參構造方法,子類構造方法如果不特殊處理,默認第一行super(),會報錯。解決措施:子類構造方法第一行,我們寫super(參數)訪問父類代參構造;或者給父類增加無參構造。

方法重寫(Override)

public class Fu {
    public void method(){
        System.out.println("父類方法");
    }
}
public class Zi extends Fu {
    @Override
    public void method() {
        System.out.println("子類中的重寫方法");
    }
}
public class Test {
    public static void main(String[] args) {
        Zi zi = new Zi();
        zi.method();
    }
}

結果:

子類中的重寫方法

複寫、覆蓋
方法名,參數列表,返回值都需要相同
父類私有方法無法重寫。
子類方法的訪問權限不能小於父類方法。【必須合乎父類的要求】【子類不能比父類拋出更大的異常】

  • 補充:四大權限修飾符:private,不寫,protected,public

使用場景

  • 擴展
  • 父類過時

訪問權限符

在這裏插入圖片描述

方法重寫和方法重載的關係

在這裏插入圖片描述

繼承的特點:

類只能單繼承,但可以多重繼承
子類不可以繼承父類的構造方法,只可以調用父類的構造方法
接口可以多繼承:接口A extends 接口B,接口C,…

七、多態

多態的實現步驟

  • 要有繼承(或實現)關係
  • 要有方法重寫
  • 父類引用指向子類對象

編譯看左,運行看右(對於非靜態成員方法來說)

編譯看左,運行看左(對於成員變量,靜態成員方法來說)

public class Animal {
    String name = "Animal";
}

public class Dog extends Animal {
    String name = "Dog";
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
        Animal b = new Animal();
        Animal c = new Dog();
        System.out.println(a.name);//Dog
        System.out.println(b.name);//Animal
        System.out.println(c.name);//Animal
    }
}

instanceof關鍵字,父子類類型轉換

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

public class Dog extends Animal {

    @Override
    void eat() {
        System.out.println("啃骨頭");
    }

    void watch() {
        System.out.println("看家");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal an = new Dog();
        if (an instanceof Dog) {
            ((Dog) an).watch();
        }
    }
}

抽象方法

有抽象方法的類一定是抽象類。
在這裏插入圖片描述

final關鍵字

修飾:方法、類、變量
不能與abstrat共用
注意:
final修飾變量的時候:如果變量是基本類型,值不能改變;如果變量是引用類型,地址值不能改變,但是屬性值可以發生變化。

八、接口

public interface Smoking {
    public abstract void smoking();
}

public class Teacher implements Smoking {

    @Override
    public void smoking() {
        System.out.println("吸菸有害");
    }
}

類與類:

  • 繼承關係,單繼承,可多層繼承

類與接口:

  • 實現關係,可以單實現,也可以多實現

接口與接口:

  • 繼承關係,可以單繼承,也可以多繼承

接口中的變量和方法

變量:
【public】【static】【final】變量名 = 數據值;
方法:
抽象方法:【public】【abstract】返回值類型 方法名(參數列表);
默認方法:【public】default 返回值類型 方法名(參數列表){方法體}
靜態方法:
私有方法:普通私有方法、靜態私有方法
接口中沒有構造方法。
在這裏插入圖片描述

九、常用API

在這裏插入圖片描述

toString

輸出語句直接打印對象,默認調用了該對象的toString()方法。

public class Person {
}

public class Test {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p);//March.Day12.Person@2d98a335
        System.out.println(p.toString());//March.Day12.Person@2d98a335
    }
}

Scanner類

在這裏插入圖片描述

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

        System.out.println("輸入一行");
        String str = s.nextLine();
        System.out.println("str = " + str);

        System.out.println("輸入一個整數");
        int num = s.nextInt();
        System.out.println("num = " + num);

    }
}

運行結果:

輸入一行
4654fdf dfsf 5456
str = 4654fdf dfsf 5456
輸入一個整數
15
num = 15

String類

在這裏插入圖片描述
在這裏插入圖片描述

Date類、Calendar類

在這裏插入圖片描述

    public static void main(String[] args) {
        //創建,採用當前系統默認時間
        Date date = new Date();
        System.out.println(date);//Fri Mar 13 15:07:33 CST 2020
        //獲取毫秒值
        long time = date.getTime();
        System.out.println(time);//1584083348709
        //創建了一個指定時間
        Date date2 = new Date(1584083348709L);
        System.out.println(date2);
    }
    public static void main(String[] args) {
        //Calendar是抽象類,用getInstance獲取對象
        Calendar c = Calendar.getInstance();
        System.out.println(c);
        int year = c.get(Calendar.YEAR);
        System.out.println("year:" + year);
        System.out.println("-----------------");
        c.set(Calendar.YEAR, 8888);
        int year2 = c.get(Calendar.YEAR);
        System.out.println("year:" + year2);
        //重置年月日
        c.set(2018, 10, 15);
        System.out.println("YEAR:" + c.get(Calendar.YEAR) + " MONTH:" + c.get(Calendar.MONTH) + " DAY:" + c.get(Calendar.DATE));
    }

在這裏插入圖片描述

包裝類

在這裏插入圖片描述

   public static void main(String[] args) {
        int i = Integer.parseInt("15624");
        System.out.println(i);
        double d = Double.parseDouble("256.213");
        System.out.println(d);
    }

jdk5之後可以自動拆箱,自動裝箱

十、集合

迭代方式:

在這裏插入圖片描述

集合和數組的區別

在這裏插入圖片描述

增強for循環、迭代器

iterable接口下都可以用迭代器
除了以上,包括數組都可以用 增強for循環

列表迭代器

list<>接口中有下面的方法,返回一個列表迭代器的實例。

ListIterator<E> listIterator()     
返回此列表元素的列表迭代器(按適當順序)。 
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("sfd");
        list.add("b");
        list.add("seee");
        list.add("lll");
        System.out.println(list);
        ListIterator lt = list.listIterator();
        while (lt.hasNext()) {
            String str = (String) lt.next();
            if (str == "b") {
                lt.add("Java");
            }
        }
        System.out.println(list);
    }
[sfd, b, seee, lll]
[sfd, b, Java, seee, lll]

Collections工具類

在這裏插入圖片描述

HashSet<>原理

底層是 哈希表 = 數組 + 鏈表/紅黑樹
前提:存儲的元素必須重寫hashCode方法和equals方法 set.add(元素) 首先調用元素的hashCode方法,計算哈希值,如果set中沒有,就直接添加;如果有的話,就調用元素的equals方法和哈希值相同的元素相比較,如果返回false就認定元素不相同,就會把元素存儲到集合中,如果返回true就丟棄
所以:存儲自定義類型,必須重寫equals方法和hashcode方法。

十一、IO流

IO流的分類

在這裏插入圖片描述
在這裏插入圖片描述

File類

在這裏插入圖片描述
在這裏插入圖片描述

file對象的三種構造方法

    public static void main(String[] args) {
        //構造方法1
        File file1 = new File("E:\\abc\\1.txt");
        //構造方法2
        File file2 = new File("E:\\abc\\", "1.txt");
        //構造方法3
        File file3 = new File("E:\\abc");
        File file4 = new File(file4, "1.txt");
    }

創建文件:

File file6 = new File("E:\\abc\\2.txt");
file6.createNewFile();

創建文件夾:

File file7 = new File("e:\\abc\\mason");
file7.mkdir();

在這裏插入圖片描述

FileReader

    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("abc/1.txt");
        int c;
        while ((c = fileReader.read()) != -1) {
            System.out.println(c);
        }
        fileReader.close();
    }

文件內容是:abcdefg 傳入字符數組進行讀取。

    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("abc/1.txt");
        char[] arr = new char[3];
        int len = fileReader.read(arr);
        System.out.println(len);
        System.out.println(arr);
        System.out.println("-----------");
        int len2 = fileReader.read(arr);
        System.out.println(len2);
        System.out.println(arr);
        System.out.println("------------");
        int len3 = fileReader.read(arr);
        System.out.println(len3);
        System.out.println(arr);
        System.out.println("------------");
        int len4 = fileReader.read(arr);
        System.out.println(len4);
        System.out.println(arr);
    }

輸出結果:

3
abc
-----------
3
def
------------
1
gef
------------
-1
gef
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("abc/1.txt");
        char[] arr = new char[3];
        int len;
        while((len = fileReader.read(arr))!=-1){
            String s = new String(arr, 0, len);
            System.out.println(s);
        }
    }
    輸出結果:
    abc
	def
	g

FileWriter

當目的地文件不存在,會自動創建

完成兩個文件的內容複製

    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("abc/1.txt");
        FileWriter fileWriter = new FileWriter("abc/2.txt");
        char[] c = new char[1024];
        int len;
        while ((len = fileReader.read(c)) != -1) {
            fileWriter.write(c, 0, len);
        }
        fileReader.close();
        fileWriter.close();
    }

BufferedWriter和BufferReader底層是字符數組

將一個文件內容複製到另一個文件的代碼。

方案一:

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("abc/1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("abc/2.txt"));
        int len;
        while((len = br.read())!=-1){
            bw.write(len);
        }
        bw.close();
        br.close();
    }

方案二:

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("abc/1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("abc/2.txt"));
        String tmp;
        while((tmp = br.readLine())!=null){
            bw.write(tmp);
            bw.newLine();
        }
        bw.close();
        br.close();
    }

InputStream OutputStream(文件複製三種方法)

    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("abc/奇怪的知識增加了.jpg");
        FileOutputStream fos = new FileOutputStream("abc/lalala.jpg");
        int len;
        while ((len = fis.read()) != -1) {
            fos.write(len);
        }
        fos.close();
        fis.close();
    }
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("abc/奇怪的知識增加了.jpg");
        FileOutputStream fos = new FileOutputStream("abc/lalala.jpg");
        byte[] c = new byte[1048];
        int len;
        while ((len = fis.read(c)) != -1) {
            fos.write(c, 0, len);
        }
        fos.close();
        fis.close();
    }
   public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("abc/奇怪的知識增加了.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("abc/lalala.jpg"));
        int len;
        while ((len = bis.read()) != -1) {
            bos.write(len);
        }
        bos.close();
        bis.close();
    }

字節流,字符流的使用場景

拷貝純文本文件使用字符流,拷貝其他(圖片,音頻,視頻等)使用字節流。

十二、反射

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
類加載器將 字節碼 文件加載到內存中。
那什麼時候會加載類呢?【將字節碼文件加載到內存中】
【一個類的字節碼文件加載到內存中,只會被加載一次】
在這裏插入圖片描述

Class對象的獲取方式

在這裏插入圖片描述
在這裏插入圖片描述
第三種方法,有一個小竅門:可以快速獲取類的正名。
在這裏插入圖片描述

    public static void main(String[] args) throws ClassNotFoundException {
        Student s = new Student();
        Class clazz1 = s.getClass();//對象使用getClass方法
        Class clazz2 = Student.class;//類名.class
        Class clazz3 = Class.forName("March.Day14.Student");//Class.forName("...")
        System.out.println(clazz1 == clazz2);//true
        System.out.println(clazz2 == clazz3);//true
    }

反射方式獲取構造方法

【最終目的是創建一個對象】
在這裏插入圖片描述

public class Student {
    public Student() {
    }

    public Student(String name) {
        System.out.println("這是學生類的有參公共構造函數");
    }

    private Student(int age) {

    }
}
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class clazz = Class.forName("March.Day14.Student");//Class.forName("...")
        Constructor constructor = clazz.getConstructor();
        System.out.println(constructor);
        System.out.println(constructor.getName());//Constructor類中getName方法
        System.out.println("------------------------------------");

        Constructor constructor2 = clazz.getConstructor(String.class);
        System.out.println(constructor2);
        Student stu = (Student) constructor2.newInstance("張三");
        System.out.println(stu);
        System.out.println("------------------------------------------");

        Constructor constructor3 = clazz.getDeclaredConstructor(int.class);
        System.out.println(constructor3);
        System.out.println("----------------------------------------");

        Constructor[] constructors = clazz.getConstructors();
        for (Constructor con : constructors) {
            System.out.println(constructor);
        }
    }

運行結果:

public March.Day14.Student()
March.Day14.Student
------------------------------------
public March.Day14.Student(java.lang.String)
這是學生類的有參公共構造函數
March.Day14.Student@2d98a335
------------------------------------------
private March.Day14.Student(int)
----------------------------------------
public March.Day14.Student()
public March.Day14.Student()

反射方式獲取成員方法 並使用

在這裏插入圖片描述

開啓暴力反射

開啓暴力反射,否則私有方法的method對象不能invoke

method3.setAccessible(true);

代碼舉例一 獲取公共方法、私有方法

Studet類:

public class Student {
    public void show1() {
        System.out.println("公共的空參方法");
    }

    public void show2(int a) {
        System.out.println("公共的有參方法,你輸入的參數是" + a);
    }

    private int show3(int a, int b) {
        System.out.println("私有的帶參方法");
        return a + b;
    }
}
    public static void main(String[] args) throws Exception {
        Class clazz = Student.class;//獲取class對象
        Constructor constructor = clazz.getConstructor();//構造器
        Student student = (Student) constructor.newInstance();//創建對象
        Method method1 = clazz.getMethod("show1");//獲取Method對象
        System.out.println(method1);//打印這個Method對象
        System.out.println(method1.getName());//打印方法名稱
        method1.invoke(student);//使用對象
        System.out.println("------------------------------------");
        Method method2 = clazz.getMethod("show2", int.class);
        method2.invoke(student, 56);
        System.out.println("-----------------------------------");
        Method method3 = clazz.getDeclaredMethod("show3", int.class, int.class);
        method3.setAccessible(true);//開啓暴力反射,否則私有方法的method對象不能invoke
        int sum = (int) method3.invoke(student, 4, 6);
        System.out.println(sum);
        System.out.println("------------------------------------");
        Method[] methods = clazz.getMethods();//獲取所有的非私有Method對象
        for (Method method : methods) {
            System.out.println(method);
        }
    }

運行結果:

public void March.Day14.Student.show1()
show1
公共的空參方法
------------------------------------
公共的有參方法,你輸入的參數是56
-----------------------------------
私有的帶參方法
10
------------------------------------
public void March.Day14.Student.show1()
public void March.Day14.Student.show2(int)
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

代碼舉例二 獲取setter方法

public class Student {
    String name;
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("March.Day14.Student");//獲取class對象
        Constructor constructor = clazz.getConstructor();//獲取constructor對象
        Student stu = (Student) constructor.newInstance();//創造對象
        Method setName = clazz.getMethod("setName", String.class);//獲取method對象
        setName.invoke(stu, "mason");//使用方法
        System.out.println(stu);//打印結果Student{name='mason'}
    }

通過反射獲取成員變量並使用

在這裏插入圖片描述
Student類:

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

代碼演示:
私有成員變量,如果想改變,需要設置【age.setAccessible(true);】暴力反射

  public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("March.Day14.Student");//獲取class對象
        Constructor constructor = clazz.getConstructor();//獲取constructor對象
        Student stu = (Student) constructor.newInstance();//創造對象
        Field name = clazz.getField("name");
        name.set(stu, "mason");
        Field age = clazz.getDeclaredField("age");
        age.setAccessible(true);
        age.set(stu, 15);
        System.out.println(stu);//Student{name='mason', age=15}
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章