温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java Parameter参数怎么设置

发布时间:2026-10-04 08:08:32 来源:亿速云 阅读:93 作者:小樊 栏目:编程语言

在 Java 里,“Parameter(参数)”出现在多个场景,设置方式取决于你指的是哪一种。下面按常见情况说明:


1️⃣ 方法参数(Method Parameter)

最常见,用于定义和调用方法。

定义方法参数

public void printName(String name, int age) {
    System.out.println(name + " " + age);
}

设置(传入)参数

printName("Tom", 18);

✅ 特点:

  • 值传递(基本类型传值,对象传引用)
  • 参数数量、类型、顺序必须匹配

2️⃣ 可变参数(Varargs)

public void printAll(String... items) {
    for (String s : items) {
        System.out.println(s);
    }
}

调用:

printAll("A", "B", "C");

3️⃣ 注解参数(Annotation Parameter)

@MyAnnotation(value = "test", count = 3)

注解定义:

@interface MyAnnotation {
    String value();
    int count() default 0;
}

4️⃣ 命令行参数(main 方法参数)

public static void main(String[] args) {
    for (String arg : args) {
        System.out.println(arg);
    }
}

运行:

java Main hello world

5️⃣ JDBC 中的 Parameter(PreparedStatement)

PreparedStatement ps = conn.prepareStatement(
    "SELECT * FROM user WHERE id = ?"
);
ps.setInt(1, 1001);  // 设置参数

6️⃣ 反射中的 Parameter

Method method = obj.getClass().getMethod("test", String.class);
Parameter[] params = method.getParameters();

可设置:

method.setAccessible(true);

7️⃣ HTTP / Spring 中的参数

Controller 参数

@GetMapping("/user")
public String getUser(@RequestParam("id") int id) {
    return "id=" + id;
}

✅ 总结

场景 设置方式
方法调用 直接传值
注解 key = value
main 命令行
JDBC setXxx()
Spring 注解绑定

如果你能具体说一下:

  • 是方法参数?
  • 数据库连接?
  • Spring / MyBatis?
  • 还是某个框架里的 Parameter?

我可以给你更精确的示例。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手