跟强哥学Java 源码 – 迷途通

跟强哥学Java 源码

12月 4, 2022                      👁️ 42

B站地址:

https://www.bilibili.com/video/BV13841157jV

第10期 实现线程的三种方式

// 实现线程的三种方式:
// 1、继承Thread类
// 2、实现Runnable接口
// 3、实现Callable接口

public class Thread1001 {
    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        thread1.start();

        Thread2 thread2 = new Thread2();
        new Thread(thread2).start();
    }
}

//创建线程方式一:继承Thread类,重写run()方法,调用start()开启线程
class Thread1 extends Thread{
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Thread----:"+i);
            try {
                Thread.sleep(100);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

//创建线程方式二:实现Runnable接口
class Thread2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("run:" + i);

            try {
                Thread.sleep(100);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

第9期 简单工厂模式

// 简单工厂模式(Simple Factory Pattern): 又称为静态工厂方法(Static Factory Method)模式,
// 它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。
// 简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
// 优点:实现对象的创建和对象的使用分离。
// 缺点:不够灵活,需要添加实例,就要修改工厂类的判断逻辑。

public class SimpleFactory {
    public static Course createCourse(String type){
        if(type.equals("Java"))
            return new JavaCourse();
        else
            return new PythonCourse();
    }

    public static void main(String[] args) {
        Course javaCourse = SimpleFactory.createCourse("Java");
        javaCourse.teach();
        Course pythonCourse = SimpleFactory.createCourse("Python");
        pythonCourse.teach();
    }
}

abstract class Course{
    public abstract void teach();
}

class JavaCourse extends Course{
    public void teach(){
        System.out.println("Java");
    }
}

class PythonCourse extends Course{
    public void teach(){
        System.out.println("Python");
    }
}

第8期 单例模式

// Ensure a class has only one instance,and provide a global point of access to it.
// 确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
// 优点:提供唯一实例,节约资源
// 缺点:扩展困难

public class Singleton {
    public static void main(String[] args) {
        Single s1 = Single.getInstance();
        s1.count();

        Single s2 = Single.getInstance();
        s2.count();
    }
}

class Single{
    private int total = 0;
    private static Single single = new Single();
    private Single(){

    }

    public static Single getInstance(){
        return single;
    }

    public void count(){
        total++;
        System.out.println("总数为:" + total);
    }
}

第7期 JDBC操作MySql,驱动下载安装

// 1.JDBC Java Database Connectivity
// 2.JDBC定义统一接口,数据库供应商实现接口。
// 3.驱动的导入
// https://dev.mysql.com/downloads/connector/j/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBC1001 {
    public static void main(String[] args) throws Exception {
        String strPwd = "123456";

        // 1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");//Mysql 5以后可以省略

        // 2.获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root",strPwd);

        // 3.获取执行对象
        Statement statement = conn.createStatement();

        // 4.执行Sql语句
        String strSql = "select * from db";
        ResultSet rs = statement.executeQuery(strSql);

        // 5.处理结果
        while (rs.next()){
            System.out.println(rs.getString("User"));
        }

        // 6.关闭数据连接
        conn.close();
    }
}

第6期 GUI设计swing,swt,Hello World

// java UI设计 swing,swt
// swt包的下载和引入
// https://www.eclipse.org/swt
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Swing1001 {

    private static void createAndShowGUI(){
// 美化
        JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("强哥Java");
        frame.setLocationRelativeTo(null);// 居中
        frame.setSize(350,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label = new JLabel("Hello");
        frame.getContentPane().add(label);

        JButton button = new JButton("Hit me");
        frame.getContentPane().add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"Hi","提示",JOptionPane.INFORMATION_MESSAGE);
            }
        });

//frame.pack();
        frame.setLayout(new FlowLayout(FlowLayout.LEADING,20,20));
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

// swt Hello World
import org.eclipse.swt.SWT;
        import org.eclipse.swt.widgets.Display;
        import org.eclipse.swt.widgets.Shell;
        import org.eclipse.swt.widgets.Text;

public class Swt1001 {

    public static void main(String[] args){
        Display display = new Display();
        Shell shell = new Shell(display);

        Text text = new Text(shell,SWT.NONE);
        text.setText("强哥Java");
        text.pack();

        shell.pack();
        shell.open();
        while (!shell.isDisposed()){
            if(!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

第5期 数组、数据容器、增删改查|遍历|排序

// 数据容器的增删改查,重点之一

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        int[] arrInt = new int[3];
        arrInt[0] = 123;
        arrInt[1] = 456;
        arrInt[2] = 789;

        System.out.println(arrInt[0]);

        ArrayList<String> listStr = new ArrayList<>();
// 增
        listStr.add("强哥");
        listStr.add("java");
        listStr.add("python");

// 删
//listStr.remove(2);

// 改
        listStr.set(2,"C++");

// 查
        int iIndex = listStr.indexOf("强哥");
        System.out.println(iIndex);
        System.out.println(listStr.get(iIndex));

// 遍历
        for (int i=0;i<listStr.size();i++){
            System.out.println(listStr.get(i));
        }

// 排序
        Collections.sort(listStr);

        Iterator<String> iterator = listStr.iterator();
        while (iterator.hasNext()){
            System.out.println("遍历" + iterator.next());
        }
    }
}

第4期 IO输入输出,文件读写 Scanner,next(),nextInt(),System.out.println

// 输入输出println
// Scanner,next(),nextInt(),
// 写入文件BufferedWriter,FileWriter
// try catch
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入姓名:");
        String str = scanner.next();

        System.out.println("请输入年龄:");
        int age = scanner.nextInt();

        System.out.println(str);
        System.out.println(age);

        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
            bw.write(str);
            bw.close();
        }catch (IOException e){
            System.out.println(e);
        }

    }
}

第3期 类|接口、封装|继承|多态 super this

【修正口误】: 一个类有多个父类,这叫多重继承,不叫多态。
多态:主要指由于继承,父类和子类调用同一个函数,可以执行不同的操作。

// class类,封装,继承,多态
// super,this
// extends
// interface,implements
// 构造函数
public class Main {
    public static void main(String[] args) {

        System.out.println("Hello world!");

        Sun sun = new Sun("强哥");
        sun.YangGang();

        Child child = new Child();
        child.YangGang();
        child.YinRou();
    }
}

class HelloWorld{

}

class Father{
    String name;
    Father(String name){
        this.name = name;
    }
    protected void YangGang(){
        System.out.println("阳刚");
    }
}

class Mother{
    protected void YinRou(){
        System.out.println("阴柔");
    }
}

class Sun extends Father{
    Sun(String name){
        super(name);
    }
    protected void YangGang(){
        super.YangGang();
        System.out.println("儿子的阳刚");
    }
}

interface Dad{
    void YangGang();
}

interface Mum{
    void YinRou();
}

class Child implements Dad,Mum{
    public void YangGang(){

    }

    public void YinRou(){

    }
}

第2期 包、数据类型及转换

package com.tianqiweiqi;
// 1.包是为了解决命名冲突的问题。
// 2.数据类型与转换
import java.util.*;
//import java.lang.*;
import java.io.*;
import java.sql.*;

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

        System.out.println("Hello world!");

        int i = 123;
        double d = 456.789;
        String str = "888";

        i = (int)d;
        i = Integer.parseInt(str);
        i = Integer.valueOf(str);

        d = i;
        d = Double.parseDouble(str);
        d = Double.valueOf(str);

        str = String.valueOf(i);
        str = String.valueOf(d);
        str = Integer.toString(i);
        str = Double.toString(d);

    }
}

第1期 Hello World

// 1.jdk,jre的安装oracle下载
// 2.环境变量的设置
// 3.javac HelloWorld.java java HelloWorld.class
// 4.IDE java就用IDEA Python用VsCode C#和C++推荐用Visual Studio
// 5.如果是public类,需要与文件名一致。

// JDK下载地址:https://www.oracle.com/java/technologies/downloads/

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

发表回复

您的电子邮箱地址不会被公开。