{"id":728,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u5c01\u88c5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/728.html","title":{"rendered":"Java \u5c01\u88c5"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java \u5c01\u88c5<\/h1>\n<\/p><\/div>\n
\n Java\u5c01\u88c5<\/strong>\u662f\u5c06\u4ee3\u7801\u548c\u6570\u636e\u5305\u88c5\u5230\u4e00\u4e2a\u5355\u5143\u4e2d\u7684
\n \u8fc7\u7a0b<\/em>\uff0c\u7528\u4e8e\u4f8b\u5982\uff0c\u6df7\u5408\u4e86\u51e0\u79cd\u836f\u7269\u7684\u80f6\u56ca\u3002\n <\/div>\n

Java \u5c01\u88c5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u7c7b\u7684\u6240\u6709\u6570\u636e\u6210\u5458\u8bbe\u4e3a\u79c1\u6709\u6765\u5728Java\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b8c\u5168\u5c01\u88c5\u7684\u7c7b\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528setter\u548cgetter\u65b9\u6cd5\u6765\u8bbe\u7f6e\u548c\u83b7\u53d6\u5176\u4e2d\u7684\u6570\u636e\u3002\n <\/div>\n
\n Java Bean <\/strong>\u7c7b\u662f\u5b8c\u5168\u5c01\u88c5\u7684\u7c7b\u7684\u793a\u4f8b\u3002\n <\/div>\n

Java\u5c01\u88c5\u7684\u4f18\u52bf<\/h3>\n
\n \u901a\u8fc7\u4ec5\u63d0\u4f9bsetter\u6216getter\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5c06\u7c7b\u8bbe\u7f6e\u4e3a
\n \u53ea\u8bfb\u6216\u53ea\u5199<\/strong>\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u60a8\u53ef\u4ee5\u8df3\u8fc7getter\u6216setter\u65b9\u6cd5\u3002\n <\/div>\n
\n \u5b83\u4e3a\u60a8\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u7684
\n \u63a7\u5236<\/strong>\u3002\u5047\u8bbe\u60a8\u8981\u8bbe\u7f6e\u7684id\u503c\u4ec5\u5e94\u5927\u4e8e100\uff0c\u60a8\u53ef\u4ee5\u5728setter\u65b9\u6cd5\u4e2d\u7f16\u5199\u903b\u8f91\u3002\u60a8\u53ef\u4ee5\u7f16\u5199\u903b\u8f91\u4ee5\u514d\u5728setter\u65b9\u6cd5\u4e2d\u5b58\u50a8\u8d1f\u6570\u3002\n <\/div>\n
\n \u8fd9\u662f\u5728Java\u4e2d\u5b9e\u73b0
\n \u6570\u636e\u9690\u85cf<\/strong>\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5176\u4ed6\u7c7b\u5c06\u65e0\u6cd5\u8bbf\u95ee\u6570\u636e\u901a\u8fc7\u79c1\u6709\u6570\u636e\u6210\u5458\u3002\n <\/div>\n
\n \u5c01\u88c5\u7c7b
\n \u6613\u4e8e\u6d4b\u8bd5<\/strong>\u3002\u56e0\u6b64\uff0c\u6700\u597d\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002\n <\/div>\n
\n \u6807\u51c6\u7684IDE\u63d0\u4f9b\u4e86\u751f\u6210\u5438\u6c14\u5242\u548c\u5438\u6c14\u5242\u7684\u8bbe\u65bd\u3002\u56e0\u6b64\uff0c\u7528Java\u521b\u5efa\u5c01\u88c5\u7684\u7c7b\u65e2\u8f7b\u677e\u53c8\u5feb\u901f\u3002\n <\/div>\n

<\/p>\n

Java\u5c01\u88c5\u7684\u7b80\u5355\u793a\u4f8b<\/h3>\n
\n \u8ba9\u6211\u4eec\u770b\u770b\u5c01\u88c5\u7684\u7b80\u5355\u793a\u4f8b\uff0c\u5b83\u7684setter\u548cgetter\u65b9\u6cd5\u53ea\u6709\u4e00\u4e2a\u5b57\u6bb5\u3002\n <\/div>\n
\n \u6587\u4ef6: Student.java\n <\/div>\n
\n
 \/\/A Java class <\/span>which is a fully encapsulated class.
\/\/It has a private <\/span>data member and getter and setter methods.package <\/span>com.lidihuo;
public <\/span>class <\/span>Student{
    \/\/private <\/span>data memberprivate <\/span>String name;
    \/\/getter method for <\/span>namepublic <\/span>String getName(){
        return <\/span>name;
    }
    \/\/setter method for <\/span>namepublic void <\/span>setName(String name){
    this<\/span>.name=name}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6587\u4ef6: Test.java\n <\/div>\n
\n
 \/\/A Java class <\/span>to test the encapsulated class.package <\/span>com.lidihuo;
class <\/span>Test{
    public <\/span>static void <\/span>main(String[] args){
        \/\/creating instance of the encapsulated classStudent s=new Student();
        \/\/setting value in the name members.setName<\/span>(\"vijay\"<\/span>);
        \/\/getting value of the name memberSystem.out.println<\/span>(s.getName<\/span>());
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Compile By: javac -d . Test.javaRun By: java com.lidihuo.Test
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 vijay
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u53ea\u8bfb\u7c7b<\/h3>\n
\n
 \/\/A Java class <\/span>which has only getter methods.public <\/span>class <\/span>Student{
    \/\/private <\/span>data memberprivate <\/span>String college=\"AKG\"<\/span>;
    \/\/getter method for <\/span>collegepublic <\/span>String getCollege(){
        return <\/span>college;
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u60a8\u4e0d\u80fd\u66f4\u6539\u5b66\u9662\u6570\u636e\u6210\u5458\" AKG\"\u7684\u503c\u3002\n <\/div>\n
\n
 s.setCollege<\/span>(\"KITE\"<\/span>);
\/\/will render compile time error
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4ec5\u5199\u7c7b<\/h3>\n
\n
 \/\/A Java class <\/span>which has only setter methods.public <\/span>class <\/span>Student{
    \/\/private <\/span>data memberprivate <\/span>String college;
    \/\/getter method for <\/span>collegepublic void <\/span>setCollege(String college){
        this<\/span>.college=college;
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u60a8\u65e0\u6cd5\u83b7\u5f97\u5b66\u9662\u7684\u503c\uff0c\u53ea\u80fd\u66f4\u6539\u5b66\u9662\u6570\u636e\u6210\u5458\u7684\u503c\u3002\n <\/div>\n
\n
 System.out.println<\/span>(s.getCollege<\/span>());
\/\/Compile Time Error, because there is no such methodSystem.out.println<\/span>(s.college);
\/\/Compile Time Error, because the college data member is private.
\/\/So, it cant be accessed from outside the class
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u53e6\u4e00\u4e2aJava\u5c01\u88c5\u793a\u4f8b<\/h3>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u53e6\u4e00\u4e2a\u5c01\u88c5\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u53ea\u6709\u56db\u4e2a\u5e26\u6709setter\u548cgetter\u65b9\u6cd5\u7684\u5b57\u6bb5\u3002\n <\/div>\n
\n \u6587\u4ef6: Account.java\n <\/div>\n
\n
 \/\/A Account class <\/span>which is a fully encapsulated class.
\/\/It has a private <\/span>data member and getter and setter methods.class <\/span>Account {
    \/\/private <\/span>data membersprivate <\/span>long <\/span>acc_no;
    private <\/span>String name,email;
    private <\/span>float<\/span> amount;
    \/\/public <\/span>getter and setter methodspublic <\/span>long <\/span>getAcc_no() {
        return <\/span>acc_no;
    }
    public void <\/span>setAcc_no(long <\/span>acc_no) {
        this<\/span>.acc_no = acc_no;
    }
    public <\/span>String getName() {
        return <\/span>name;
    }
    public void <\/span>setName(String name) {
        this<\/span>.name = name;
    }
    public <\/span>String getEmail() {
        return <\/span>email;
    }
    public void <\/span>setEmail(String email) {
        this<\/span>.email = email;
    }
    public <\/span>float<\/span> getAmount() {
        return <\/span>amount;
    }
    public void <\/span>setAmount(float<\/span> amount) {
        this<\/span>.amount = amount;
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6587\u4ef6: TestAccount.java\n <\/div>\n
\n
 \/\/A Java class <\/span>to test the encapsulated class <\/span>Account.public <\/span>class <\/span>TestEncapsulation {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/creating instance of Account class Account acc=new Account();
        \/\/setting values through setter methods acc.setAcc_no<\/span>(7560504000L);
        acc.setName<\/span>(\"Sonoo Jaiswal\"<\/span>);
        acc.setEmail<\/span>(\"sonoojaiswal@lidihuo.com\"<\/span>);
        acc.setAmount<\/span>(500000f);
        \/\/getting values through getter methods System.out.println<\/span>(acc.getAcc_no<\/span>()+\" \"<\/span><\/span><\/span>+acc.getName<\/span>()+\" \"+acc.getEmail<\/span>()+\" \"+acc.getAmount<\/span>());
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 7560504000 Sonoo Jaiswal sonoojaiswal@lidihuo.com 500000.0
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u5c01\u88c5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-728","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/728"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=728"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/728\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}