java通过方法修改字段值

 1、直接通过方法进行修改字段值

public class methods1 {
    public static void main(String[] args) {
        Person ming = new Person();
        ming.setAge(12);
        ming.setName("xiao");
        ming.setSex("男");
        System.out.println(ming.getName() + ", " + ming.getAge() + "," + ming.getSex());
    }
}
class Person{
    private int age;
    private String name;
    private String sex;

    public int getAge() {
        return this.age;
    }

    public String getName() {
        return this.name;
    }
    public String getSex(){
        return this.sex;
    }

    public void setAge(int age) {
        if(age < 0 || age > 100){
            throw new IllegalArgumentException("无效值");
        }
        this.age = age;
    }

    public void setName(String name) {
        if(name == null || name.isEmpty()){
            throw new IllegalArgumentException("请输入姓名");
        }
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
// 小明, 12,男

2、通过方法嵌套修改字段值

public class object {
    public static void main(String[] args){
        Human ming = new Human();
        ming.setBirth(2008);
        System.out.println("小明的年纪为:" + ming.getAge());
    }
}

class Human{
    private String name;
    private int age;
    private int birth;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return backAge(2021);
    }

    public void setBirth(int birth) {
        this.birth = birth;
    }
    public int backAge(int data){
        return data - this.birth;
    }
}
//  小明的年纪为:13

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以通过自定义注解来修改字段。首先,我们需要定义一个注解。可以使用@interface关键字来定义注解。例如,假设我们要定义一个名为@CustomAnnotation的注解,该注解用于修改字段。 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface CustomAnnotation { String value() default ""; } ``` 在注解内部,使用了@Retention和@Target元注解来指定注解的保留策略和注解的作用目标。在此例中,我们设置了@Retention(RetentionPolicy.RUNTIME),表示该注解在运行时可见。@Target(ElementType.FIELD)表示该注解可以用于字段上。 接下来,我们可以将@CustomAnnotation应用到字段上,并使用反射机制获取字段修改。 ```java import java.lang.reflect.Field; public class DemoClass { @CustomAnnotation("Hello World") private String value; public void setValueUsingAnnotation() throws IllegalAccessException { Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(CustomAnnotation.class)) { CustomAnnotation annotation = field.getAnnotation(CustomAnnotation.class); field.setAccessible(true); field.set(this, annotation.value()); } } } public static void main(String[] args) throws IllegalAccessException { DemoClass demo = new DemoClass(); System.out.println("Before modification: " + demo.value); demo.setValueUsingAnnotation(); System.out.println("After modification: " + demo.value); } } ``` 在上述示例中,我们定义了一个名为DemoClass的类,并在其中声明了一个私有字段value。我们将@CustomAnnotation应用到该字段上,并在注解中设置了一个字符串。 在setValueUsingAnnotation方法中,使用反射机制获取类的所有字段。然后,检查每个字段是否应用了@CustomAnnotation注解。如果有,则使用Field类的setAccessible方法来设置字段可访问,并使用Field类的set方法将注解中的赋给字段。 在main方法中,我们创建了DemoClass的实例,并输出修改前后的字段。 通过运行上述代码,我们可以看到输出结果中,字段在应用自定义注解后发生了变化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值