{"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
<\/p>\n
<\/p>\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}]}}