• Java入门


    Java导入包

    • import 主要用于导入在使用类前准备好了
    • Import 关键字可以多次使用,导入多个包
    • Import 关键字可以多次使用,导入多个类
    • 如果同一个包中需要大量的类,那么可以使用通配符进行导入
    • 如果Import了不同包,相同名称的类,那么还是需要在使用时增加包名。
    import java.util.Date;
    import java.sql.*;
    
    public class Main {
        public static void main(String[] args) {
            java.lang.String name = "测试";
            System.out.println(name);// 输出字符串
            
            // TODO 系统的中的 Date类
            Date date = new Date();
            System.out.println(date);// 输出日期-Sun Sep 03 22:32:40 GMT+08:00 2023
    
            // TODO 如果使用 sql 中也有Date需要写全
            java.sql.Date sqlDate=new java.sql.Date(12012);
            System.out.println(sqlDate);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    继承与构造方法

    使用extends关键字可以继承父类

    如果要获取父类的方法或名称使用super关键字

    如果父类中有构造方法,在子类中需要对其实现,否则报错

    构造方法与C#类似,方法名等于类名

    public class _02_构造方法 {
        public static void main(String[] args) {
            Child2 c1 = new Child2();
            Child2 c2 = new Child2();
            Child2 c3 = new Child2();
        }
    }
    
    class Parent2 {
        String username = "父类";
    
        Parent2(String name) {
            username = name;
            System.out.println("父类。。。。");
        }
    }
    
    // 实现父类的构造函数
    class Child2 extends Parent2 {
    
        Child2() {
            super("新的子类名");
            System.out.println("子类。。。");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    方法重载

    一个类中,不能重复声明相同方法,也不能声明相同属性

    在这里相同的方法指的是方法名,参数列表相同和返回类型无关

    如果方法名相同,但是参数列表(个数,顺序,类型)不相同,会认为是不同的方法,只不过是名称一样

    这个操作在Java和C#中被称为方法重载

    构造方法也存在方法的重载,重载需要传入的参数不一致

    /* 方法的重载
     * 方法名相同-参数参数不同
     * */
    class User {
        void login(String account, String password) {
            System.out.println("正好,密码登录");
        }
    
        void login(int tel) {
            System.out.println("手机验证码登录");
        }
    
        void login(String wx) {
            System.out.println("微信登录");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    方法重载-this

    如果方法参数较多,可以重复调用类中方法

    判断那个函数被调用,可以看名字颜色,如果是高亮状态表示被调用

    class User1 {
        User1() {
            this("名字");
        }
    
        User1(String name) {
            this(name, "男");
        }
    
        User1(String name, String sex) {
            System.out.println(name + sex);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    方法重载-顺序

    • 如果传入参数是byte类型
    • 会自动选择到byte类型的方法中
    • 如果没有byte会转入到short类型中
    • 如果没有short会转入到int,原因是转换成byte无法转换成char
    • byte==>8
    • short==>16
    • char==>16
    • int==>32
    public class _05_方法重载_顺序 {
        public static void main(String[] args) {
            byte b = 10;
            User2 user2 = new User2(b);
        }
    }
    
    
    class User2 {
    
        User2(byte a) {
            System.out.println("bbb");
        }
    
        User2(short a) {
            System.out.println("sss");
        }
    
        User2(char a) {
            System.out.println("ccc");
        }
    
        User2(int a) {
            System.out.println("iii");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    方法重载-多态

    • 多态其实就是约束了对象的使用场景
    • 方法重载,方法名相同,多数列表不同(个数、顺序、类型)
    • 如果转换类型没找到会找父类,如BBB的父类是AAA
    • AAA的父类是object,如果object中也没有这个类型则会报错
    public class _06_方法重载_多态 {
        public static void main(String[] args) {
            AAA aaa1 = new AAA();
            test(aaa1);// aaa
    
            AAA aaa2 = new BBB();
            test(aaa2);// aaa
        }
    
        static void test(AAA aaa) {
            System.out.println("aaa");
        }
    
        static void test(BBB bbb) {
            System.out.println("bbb");
        }
    }
    
    
    class AAA {
    }
    
    class BBB extends AAA {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    方法的重写

    • 方法的重写:父类对象的方法其实主要体现通用性,无法在特殊场合下使用
    • 如果子类对象需要在特殊场合下使用,那么就需要重写方法逻辑,这个操作在Java中称之为方法的重写
    • 这里的重写并不意味着方法被覆盖,只是在当前场合不使用,如果使用super关键字还可以继续访问父类中的方法
    • 方法的重写要求,子类的方法,方法名相同,返回类型相同,参数列表相同
    public class _07_方法重写 {
        public static void main(String[] args) {
            Children children = new Children();
            children.test();
        }
    }
    
    
    class Parent3 {
        String name = "父类";
    
    
        void test() {
            System.out.println("父类中....");
        }
    }
    
    class Children extends Parent3 {
        String name = "子类";
    
        // 方法的重写
        void test() {
            // 也可以调用父类中的方法
            super.test();
            System.out.println("子类中....");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    小练习

    • 一个对象使用什么方法,取决于子引用变量类型
    • 一个对象使用什么属性,取决于引用变量的类型
    • 一个对象的方法具体的使用是需要看具体对象的
    • 一个对象的属性具体的使用是不需要看具体的对象的,属性在哪里声明在哪里使用
    public class _08_方法重写_小案例 {
        public static void main(String[] args) {
    
            // 根据类型推测,调用的是CCC的类中
            CCC ccc = new CCC();
            System.out.println(ccc.sum());// 20
    
            /*
             * 如果DDD中有sum函数
             * 如果将DDD转化为CCC的类型
             * 如果DDD中没有sum函数,则会调用CCC的中函数
             * */
            CCC ddd = new DDD();
            System.out.println(ddd.sum());// 40
        }
    }
    
    
    class CCC {
        int i = 10;
    
        int sum() {
            return i + 10;
        }
    }
    
    
    class DDD extends CCC {
        int i = 20;
    
        int sum() {
            return i + 20;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    使用内部类

    public class _09_内部类 {
        public static void main(String[] args) {
            // 如果调用内部类
            OuterClass outerClass = new OuterClass();
            OuterClass.InnerClass innerClass = outerClass.new InnerClass();
        }
    }
    
    
    class OuterClass {
        public class InnerClass {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    单例模式

    public class _10_单例模式 {
        public static void main(String[] args) {
    
        }
    }
    
    
    class User4 {
        private static User4 user4 = null;
    
        private User4() {
        }
    
        // 如果user4中为空,则进行赋值这样只需要调用一次就可以无限创建
        public static User4 getInstance() {
            if (user4 == null) {
                user4 = new User4();
            }
    
            return user4;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    final修饰

    public class _11_final不可变 {
        public static void main(String[] args) {
            final String name = "不可变的名字";
    
            User5 user5 = new User5("名字");
            System.out.println(user5.name);
        }
    }
    
    
    class User5 {
        //   public final String name = "初始化名字";// 使用final修饰,需要手动初始化
        public final String name;
    
        // 可以使用构造函数进行初始化
        public User5(String name) {
            this.name = name;
        }
    
        // `final`的方法不可以被子类重写
        public final void test() {
    
        }
    
        // ·final`可以修饰参数名
        public void test(final String name) {
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    抽象类

    • 如果一个类中含有抽象方法,那么这个类是抽象类
    • 如果一个类是抽象类,它的方法不一定是抽象方法
    • 抽象类无法直接构建对象,但是可以通过子类间接构建对象
    • 如果抽象类中有抽象方法,那么子类继承抽象类,需要重写抽象方法,讲方法补充完整
    • 抽象方法不可以使用final同时使用,包括类名也是
    public class _12_抽象类 {
        public static void main(String[] args) {
            Chinese chinese = new Chinese();
        }
    }
    
    
    abstract class Person {
        public abstract void eat();
    
        public void test() {
    
        }
    }
    
    // 需要实现父类中方法-抽象类中
    class Chinese extends Person {
    
        @Override// 看情况是否需要重写
        public void eat() {
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    枚举

    • 枚举是一个特殊的类,其中包含了一组特定的对象,这些对象不会发生改变,一般都是用大写表示符
    • 枚举使用enum关键字使用
    • 枚举类会将对象放置在最前面,那么和后面的语法需要使用分号隔开
    public class _13_枚举类型 {
        public static void main(String[] args) {
            System.out.println(City.BEIJING.name);
            System.out.println(City.BEIJING.code);
        }
    }
    
    
    enum City {
        BEIJING("北京", 1001),
        SHANGHAI("上海", 1002);
    
        City(String name, int code) {
            this.code = code;
            this.name = name;
        }
    
        public String name;
        public int code;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    排序

    冒泡排序

    public class Main {
        public static void main(String[] args) {
            int[] nums = {1, 4, 6, 5, 3, 0, 2};
    
            for (int i = 0; i < nums.length; i++) {
                for (int j = 0; j < nums.length - i - 1; j++) {
                    int num1 = nums[j];
                    int num2 = nums[j + 1];
    
                    if (num1 > num2) {
                        nums[j] = num2;
                        nums[j + 1] = num1;
                    }
                }
            }
    
            for (int num : nums) {
                System.out.println(num);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    选择排序

    public class 选择排序 {
        public static void main(String[] atgs) {
            int[] nums = {1, 4, 6, 5, 3, 0, 2};
    
            for (int j = 0; j < nums.length; j++) {
                int maxIndex = 0;
                for (int i = 1; i < nums.length - j; i++) {
                    if (nums[i] > nums[maxIndex]) {
                        maxIndex = i;
                    }
                }
    
                int num1 = nums[nums.length - j - 1];
                int num2 = nums[maxIndex];
    
                nums[maxIndex] = num1;
                nums[nums.length-j - 1] = num2;
            }
    
    
            for (int num : nums) {
                System.out.println(num);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    二分查找

    public class _02_二分查找 {
        public static void main(String[] args) {
            int[] nums = {1, 4, 6, 5, 3, 0, 2};
    
            int targetNum = 4;
            int start = 0;
            int end = nums.length - 1;
            int mid = 0;
    
            while (start <= end) {
                mid = (start + end) / 2;
                if (nums[mid] == targetNum) break;
                else if (nums[mid] > targetNum) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
    
            System.out.println("数据在" + mid + "位置");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    日期类

    import javax.swing.*;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main {
        public static void main(String[] args) throws ParseException {
            Date date = new Date();
            System.out.println(date);
    
            // 使用java自带日期格式化
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSS");
            System.out.println(simpleDateFormat.format(date));
    
            // 将字符串转为日期
            String dateString = "2023年09月04日 14:43:41.755";// 格式必须符合 simpleDateFormat 格式
            Date parseString = simpleDateFormat.parse(dateString);
            System.out.println(parseString);
    
            // 根据时间构成时间对象
            date.setTime(System.currentTimeMillis());
    
            // 获取时间戳
            date.getTime();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    日历类

    import java.util.Calendar;
    import java.util.Date;
    
    public class _01_日历类 {
        public static void main(String[] args) {
            Calendar instance = Calendar.getInstance();
            System.out.println(instance);
    
            System.out.println(instance.get(Calendar.YEAR));
            System.out.println(instance.get(Calendar.MONTH));// 获取月份需要+1
            System.out.println(instance.get(Calendar.DATE));
    
            instance.setTime(new Date());
            instance.add(Calendar.YEAR, 1);// 去上一年
            instance.add(Calendar.YEAR, -1);// 去年
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    win10,WSL的Ubuntu配python3.7手记
    java下标索引异常
    (附源码)python房屋租赁管理系统 毕业设计 745613
    Apache Pulsar工作原理
    【强化学习篇】on-policy 和 off-policy 的区别
    政安晨:【Keras机器学习示例演绎】(十六)—— 用于图像分类的混合增强
    Axure8.0教程:身高标尺(动态面板)
    鲜花销售管理系统,鲜花商城管理系统,鲜花网站毕业设计
    Go语言中的锁与管道的运用
    西农大 C plus
  • 原文地址:https://blog.csdn.net/weixin_46533577/article/details/132671157