javase

基础语法

  1. 条件结构
    if else

  2. 循环结构
    for循环,while循环, do while循环

  3. 数组声明与初始化

  4. 类与对象

    实体类: 说明,实际开发中,一般不会在entity中放入方法,只会说明实体类的私有属性
    @Data
    public class User {
    
        private String name;
    
        private Integer age;
    
        public void eat(){
            System.out.println(this.name + ":吃起来");
        }
    }
    @Test
    void test() {
        User user = new User();
        user.setName("二狗");
        user.eat();
    }
     输出:二狗:吃起来 
    
  5. 构造方法
    实际开发中,使用lombok注解@Data,会重写entity中tostring、get、set方法,并带有无参数构造方法

  6. 继承

@Data
public class SubUser extends User{

    private String addr;

    public void getSubUserAddr(){
        System.out.println(this.getName() + ":地址为--" + this.addr);
    }
}
@Test
void test() {
    SubUser subUser = new SubUser();
    subUser.setName("子用户");
    subUser.eat();
    subUser.setAddr("成都");
    subUser.getSubUserAddr();
}
    输出
    子用户:吃起来
    子用户:地址为--成都
  1. 接口
接口定义方法,实现接口的类,必须重写接口中定义的方法
接口:
public interface Person {
    public void getIdNumber();
}
// 实现接口的类PersonClassTest1,必须重写接口中定义的方法
public class PersonClassTest1 implements Person {
    @Override
    public void getIdNumber() {
        System.out.println("获取用户的身份证号码");
    }
}
  1. 方法重载
public class TestClass {
    public Integer add1(int a,int b){
        return a+b;
    }

    public Integer add2(int a,int b,int c){
        return a+b+c;
    }
}

@Test
void test() {
    TestClass testClass = new TestClass();
    Integer result1 = testClass.add1(1, 2);
    Integer result2 = testClass.add2(1, 2,3);
    System.out.println("r1:" +  result1 + "...r2:" + result2 ) ;    
}

方法重载:方法名相同,返回值相同,但形参列表不同(包括数量和类型)

  1. 方法重写

    重写是指:子类对父类方法新的实现。

@Data
public class SubUser extends User{

    @Override
    public void eat() {
        System.out.println("子类重写父类方法");
    }
}

@Test
void test() {
    SubUser subUser = new SubUser();
    subUser.eat();
}
输出:子类重写父类方法
  1. 多态允许一个对象表现为多种类型
@Data
public class User {
    public void eat(){
        System.out.println("吃起来了");
    }
}

@Data
public class SubUser extends User{
    @Override
    public void eat() {
        System.out.println("子类重写父类方法");
    }
}

@Data
public class Sub1User extends User{
    @Override
    public void eat() {
        System.out.println("sub1吃上了");
    }
}

@Test
void test() {
    User user1 = new User();
    User user2 = new SubUser();
    User user3 = new Sub1User();

    user1.eat();    // 分别调用不同的方法吃
    user2.eat();
    user3.eat();
}
  1. 封装
封装通过隐藏类信息实现。使用private变量限制直接访问,提供公共方法来调整内部状态。

鉴于,继承需要public修饰的属性和方法才能被子类继承,所以不满足封装的目标要求。故,在java中很少使用extend来表述类之间的关系。多用接口,使用implements来重写接口中方法
  1. 静态变量和静态方法
// 静态变量和方法属于类本身,而不属于单一对象。可以直接用类名.方法进行调用。
public class TestClass {

    public static final double PI = 3.14159;

    public static double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        double circumference = TestClass.PI * 10;
        System.out.println(circumference);

        double result = TestClass.add(TestClass.PI,5.5);
        System.out.println(result);
    }
}
  1. 内部类

内部类是定义在另一个类中的类。提供从内部访问外部类成员的权限。用于封装逻辑相关的类。

public class TestClass {
    
    class ClassInner{
        void printInner(){
            System.out.println("内部类输出文本");
        }
    }

    void printOut(){
        ClassInner classInner = new ClassInner();
        classInner.printInner();
    }

    public static void main(String[] args) {
        TestClass testClass = new TestClass();
        testClass.printOut();
    }
}

进阶概念

  1. 泛型

泛型提供一种创建类型安全的代码方式,允许类、接口和方法在实现中使用类型参数。这样可避免类型转换错误

  • 泛型的本质是为了将类型参数化
  • 泛型的出现就是为了统一集合当中数据类型的
  1. 集合框架
  • Collection 接口
    1. List 接口
    • ArrayList
      优点: 底层数据结构是数组,查询快,增删慢。
      缺点: 线程不安全,效率高
    • Vector
      优点: 底层数据结构是数组,查询快,增删慢。
      缺点: 线程安全,效率低
    • LinkedList
      优点: 底层数据结构是链表,查询慢,增删快。
      缺点: 线程不安全,效率高
    1. Set 接口
    • HashSet
      底层数据结构是哈希表。(无序,唯一)
      依赖两个方法:hashCode()和equals()保证数位唯一性
    • LinkedHashSet
      底层数据结构是链表和哈希表。(FIFO插入有序,唯一)
      1.由链表保证元素有序
      2.由哈希表保证元素唯一
    • TreeSet
      底层数据结构是红黑树。(有序,唯一)
      1. 如何保证元素排序的呢?
        自然排序
        比较器排序
        2.如何保证元素唯一性的呢?
        根据比较的返回值是否是0来决定
    1. Queue(不常见) 接口
    • PriorityQueue
  • Map 接口
    1. HashMap 无序
    2. TreeMap 有序
    3. HashTable 无序
  1. 异常处理
@Test
void test() {
    try {
        int result = 5 / 0;
    } catch (Exception e) {
        // todo
        System.out.println(e.getMessage());
    } finally {
       // todo
    }
}
  1. 文件I/O

    参考博客:
    IO流详细介绍

示例:可参考项目中的导入导出

  1. 多线程
    实现异步操作的方式:
  • 继承Thread类
public class MyThreadTest extends Thread{
    public void run() {
        // 线程执行的代码
        for (int i = 0; i < 1000; i++) {
            System.out.println("异步:" + i);
        }
    }

    public static void main(String[] args) {
        MyThreadTest myThreadTest = new MyThreadTest();
        myThreadTest.start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("主线程:" + i);

        }
    }
}
  • 实现Runnable接口
public class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("异步:" + i);
        }
    }

    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        new Thread(runnable).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("主线程:" + i);
        }
    }
}
  • 实现Callable接口
    实现Callable接口,并且call()方法可以返回结果或者抛出异常。
    使用ExecutorService来提交Callable任务,并得到一个Future对象,可以通过这个对象获取执行的结果。
public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        Integer count = 0;
        for (int i = 0; i < 1000; i++) {
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<Integer> future = executor.submit(new MyCallable());
        try {
            Integer result = future.get(); // 获取执行结果
            System.out.println("result:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 使用线程池
// 创建线程池工具类
public class ThreadUtil {

    private final static ExecutorService pool = Executors.newFixedThreadPool(10);

    public static void runAsync(Runnable thread){
        pool.execute(thread);
    }
}

@Test
void test() {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.println("异步:" + i);
            }
        }
    };
    ThreadUtil.runAsync(runnable);

    for (int i = 0; i < 1000; i++) {
        System.out.println("主线程:" + i);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值