java 中静态内部 class 的范围

Scope of static inner class in java

今天发现一件很奇怪的事。我有以下 class 和静态内部 class.

public class PDto {

private Agreement agreement = new Agreement();

    public static class Agreement{
        public String agreementName;
        public String agreementDescription;
        public String currency;
    }

    public Agreement getAgreement() {
        return agreement;
    }

    public void setAgreement(Agreement agreement) {
        this.agreement = agreement;
    }
}

另一个 class ClassA 具有以下方法:-

private Agreement createBillingAgreement(PDto payment) {

    PDto.Agreement billingAgreement = payment.getAgreement();
    Agreement agreement = new Agreement();
    agreement.setName(billingAgreement.agreementName);
    agreement.setDescription(billingAgreement.agreementDescription);
    billingAgreement.agreementName = "Changed agreeement Name" ;
}

Class B 调用 Class A

方法的代码
classBService.createBillingAgreement(payment);
System.out.println("Changed billing agreement name : " + payment.getAgreement().agreementName);

当我从 class ClassB 打印协议名称时,我得到了在 class A 的 createBillingAgreement 方法中设置的值。这是怎么回事可能。

一个static-inner class用于同时访问class静态。例如:

public class Parent{
    public static class Child{
        public void aMethod(String s){
            System.out.println("Hi!" + s);
        }
    }
}

public class AnotherClass{
    public void AnotherMethod(){
        Parent.Child.aMethod("From AnotherClass");
    }
}

输出:

嗨!来自另一个班级

或者您的情况:

PTdo.Agreement

        ^

这是静态的class