三分钟搞懂Java修饰符:你不知道的用法和技巧!

Hello,大家好!今天我们来聊一聊Java中的修饰符。这些小东西在Java编程中可是非常重要的。

别担心,虽然听起来有点复杂,但掌握它们其实很简单。接下来,用三分钟时间带你搞懂Java修饰符!

什么是修饰符?

修饰符是一些关键字,用来修饰类、方法、变量等,使它们具有特定的访问权限或行为。

在Java中,修饰符分为两大类:访问修饰符和非访问修饰符。

访问修饰符

  1. public:公开的,任何地方都可以访问。

  2. protected:受保护的,同一个包或者子类可以访问。

  3. default(默认的,不写修饰符):包级私有的,同一个包内可以访问。

  4. private:私有的,只有本类可以访问。

非访问修饰符

  1. static:静态的,属于类而不是实例。

  2. final:最终的,不能被修改或继承。

  3. abstract:抽象的,不能实例化,需要子类实现。

  4. synchronized:同步的,常用于线程控制。

访问修饰符实例

Public和Private的使用

public class AccessModifierExample {
    public String publicVar = "我是public的";
    private String privateVar = "我是private的";

    public void showPublic() {
        System.out.println(publicVar);
    }

    private void showPrivate() {
        System.out.println(privateVar);
    }

    public static void main(String[] args) {
        AccessModifierExample example = new AccessModifierExample();
        example.showPublic();
        // example.showPrivate(); // 这行会报错,因为showPrivate是私有的
    }
}

运行结果:

我是public的

在这个例子中,publicVarshowPublic是公开的,所以可以在类外访问,而privateVarshowPrivate是私有的,只能在类内访问。

非访问修饰符实例

Static的使用

public class StaticExample {
    public static int staticVar = 42;

    public static void staticMethod() {
        System.out.println("我是静态方法");
    }

    public static void main(String[] args) {
        System.out.println(StaticExample.staticVar);
        StaticExample.staticMethod();
    }
}

运行结果:

42
我是静态方法

static修饰符使得变量和方法属于类,而不是类的实例。所以,可以直接通过类名访问。

Final的使用

public class FinalExample {
    public final int finalVar = 100;

    public final void finalMethod() {
        System.out.println("我是最终方法");
    }

    public static void main(String[] args) {
        FinalExample example = new FinalExample();
        System.out.println(example.finalVar);
        example.finalMethod();
    }
}

运行结果:

100
我是最终方法

final修饰符使得变量的值不可改变,方法不可被重写。

Synchronized的使用

public class SynchronizedExample {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }

    public static void main(String[] args) {
        SynchronizedExample example = new SynchronizedExample();

        Thread t1 = new Thread(example::increment);
        Thread t2 = new Thread(example::increment);

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("最终计数: " + example.getCount());
    }
}

运行结果可能为:

最终计数: 2

synchronized确保同一时间只有一个线程可以访问方法,避免线程安全问题。

总结

今天我们快速了解了Java中的各种修饰符及其用法。修饰符虽然简单,但在实际开发中却有着非常重要的作用。

希望通过今天的学习,你能更好地使用它们,提高代码的安全性和可维护性。关注订阅,下次见,Happy Coding!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,@Mapper注解是Mybatis框架中用于标识数据访问层接口的注解,用于告诉Spring容器将该接口类实例化并注入到其他Bean中。其使用步骤如下: 1. 在Spring Boot项目中引入Mybatis和Mybatis-Spring的依赖 2. 在配置文件中配置数据源和Mybatis的相关属性 3. 创建一个数据访问层接口,使用@Mapper注解标识该接口 4. 在该数据访问层接口中定义需要操作的数据库方法 5. 在Service或Controller中注入该数据访问层接口的实例,并调用其中的方法 下面是一个示例: 1. 在pom.xml中添加Mybatis和Mybatis-Spring的依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> ``` 2. 在application.properties中配置数据源和Mybatis的相关属性: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 创建一个数据访问层接口UserMapper,使用@Mapper注解标识该接口: ```java @Mapper public interface UserMapper { User selectByPrimaryKey(Integer id); int insert(User record); int updateByPrimaryKey(User record); int deleteByPrimaryKey(Integer id); } ``` 4. 在mapper目录下创建UserMapper.xml,定义需要操作的数据库方法: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select * from user where id = #{id,jdbcType=INTEGER} </select> <insert id="insert" parameterType="com.example.demo.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into user (username, password) values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKey" parameterType="com.example.demo.entity.User"> update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where id = #{id,jdbcType=INTEGER} </delete> </mapper> ``` 5. 在Service或Controller中注入UserMapper的实例,并调用其中的方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } @Override public int insert(User user) { return userMapper.insert(user); } @Override public int updateByPrimaryKey(User user) { return userMapper.updateByPrimaryKey(user); } @Override public int deleteByPrimaryKey(Integer id) { return userMapper.deleteByPrimaryKey(id); } } ``` 这就是使用@Mapper注解的基本步骤,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值