{"id":303,"date":"2020-11-26T04:39:05","date_gmt":"2020-11-26T04:39:05","guid":{"rendered":"https:\/\/vukieuanh.com\/?p=303"},"modified":"2020-12-15T07:46:20","modified_gmt":"2020-12-15T07:46:20","slug":"tinh-bao-goi-trong-java","status":"publish","type":"post","link":"https:\/\/vukieuanh.net\/index.php\/2020\/11\/26\/tinh-bao-goi-trong-java\/","title":{"rendered":"T\u00ednh bao g\u00f3i trong Java"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">T\u00ednh bao g\u00f3i (Encapsulation) l\u00e0 m\u1ed9t trong 4 t\u00ednh ch\u1ea5t c\u1ee7a l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng. N\u00f3 l\u00e0 c\u01a1 ch\u1ebf cho ph\u00e9p \u1ea9n nh\u1eefng th\u00f4ng tin kh\u00f4ng li\u00ean quan v\u00e0 hi\u1ec3n th\u1ecb nh\u1eefng th\u00f4ng tin c\u00f3 li\u00ean quan.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong Java \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o \u0111\u01b0\u1ee3c t\u00ednh bao g\u00f3i, ngta d\u00f9ng access modifier private cho c\u00e1c thu\u1ed9c t\u00ednh. C\u00e1c method thao t\u00e1c v\u1edbi c\u00e1c thu\u1ed9c t\u00ednh \u1ea5y ng\u01b0\u1eddi ta d\u00f9ng getter v\u00e0 setter \u0111\u1ec3 l\u1ea5y ra thu\u1ed9c t\u00ednh v\u00e0 \u0111\u1eb7t l\u1ea1i gi\u00e1 tr\u1ecb cho n\u00f3. C\u00e1c method n\u00e0y th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng access modifer public. V\u00ed d\u1ee5: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Student {\n    private String name;\n \n    public String getName() {\n        return name;\n    }\n \n    public void setName(String name) {\n        this.name = name;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Qua \u0111\u00e2y ch\u00fang ta c\u00f3 th\u1ec3 t\u1ea1o ra m\u1ed9t l\u1edbp ch\u1ec9 ghi, ch\u1ec9 c\u00f3 setter ho\u1eb7c ch\u1ec9 \u0111\u1ecdc, ch\u1ec9 c\u00f3 getter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u1eady l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o bao \u0111\u00f3ng v\u1edbi c\u00e1c tr\u01b0\u1eddng d\u1eef li\u1ec7u l\u00e0 tham chi\u1ebfu. V\u00ed d\u1ee5 ta c\u00f3 l\u1edbp StudentManager nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class StudentManager {\n    private List&lt;Student> studentList;\n\n    public List&lt;Student> getStudentList() {\n        return studentList;\n    }\n\n    public void setStudentList(List&lt;Student> studentList) {\n        this.studentList = studentList;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ph\u01b0\u01a1ng th\u1ee9c getStudent tr\u1ea3 v\u1ec1 cho ch\u01b0\u01a1ng tr\u00ecnh studentList c\u1ee7a StudentManager, v\u00ec \u0111\u00e2y l\u00e0 m\u1ed9t bi\u1ebfn tham chi\u1ebfu n\u00ean trong v\u00ed d\u1ee5 sau t\u1ea5t c\u1ea3 c\u00e1c thao t\u00e1c tr\u00ean bi\u1ebfn students1 s\u1ebd thay \u0111\u1ed5i c\u00e1c gi\u00e1 tr\u1ecb tr\u00ean thu\u1ed9c t\u00ednh students c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng manager. \u0110i\u1ec1u n\u00e0y s\u1ebd kh\u00f4ng \u0111\u1ea3m b\u1ea3o \u0111\u01b0\u1ee3c t\u00ednh bao g\u00f3i.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        StudentManager manager = new StudentManager();\n        List&lt;Student> students = new ArrayList&lt;>();\n        students.add(new Student(\"Nam\"));\n        students.add(new Student(\"Minh\"));\n        manager.setStudentList(students);\n\n        List&lt;Student> students1 = manager.getStudentList();\n        students1.add(new Student(\"Hoang\"));\n        System.out.println(manager.getStudentList().size());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c in ra s\u1ebd l\u00e0 3, c\u00f3 ngh\u0129a l\u00e0 ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 d\u00f9ng bi\u1ebfn students1 \u0111\u1ec3 thay \u0111\u1ed5i thu\u1ed9c t\u00ednh c\u1ee7a bi\u1ebfn manager.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ec v\u1eady v\u1edbi c\u00e1c thu\u1ed9c t\u00ednh d\u1ea1ng tham chi\u1ebfu, ph\u01b0\u01a1ng th\u1ee9c getter c\u1ee7a n\u00f3 ph\u1ea3i l\u00e0m 3 b\u01b0\u1edbc nh\u01b0 sau \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u tr\u1ea3 v\u1ec1 \u0111\u00fang l\u00e0 gi\u00e1 tr\u1ecb hi\u1ec7n t\u1ea1i, tuy nhi\u00ean kh\u00f4ng tr\u1ea3 v\u1ec1 tham chi\u1ebfu c\u1ee7a n\u00f3:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B\u01b0\u1edbc 1: t\u1ea1o ra 1 bi\u1ebfn tham chi\u1ebfu m\u1edbi<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B\u01b0\u1edbc 2: clone to\u00e0n b\u1ed9 d\u1eef li\u1ec7u c\u1ee7a tham chi\u1ebfu c\u0169 sang bi\u1ebfn m\u1edbi<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B\u01b0\u1edbc 3: tr\u1ea3 v\u1ec1 bi\u1ebfn tham chi\u1ebfu m\u1edbi<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ta s\u1ebd s\u1eeda l\u1ea1i StudentManager nh\u01b0 sau: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class StudentManager {\n    private List&lt;Student> studentList;\n\n    public List&lt;Student> getStudentList() {\n\/\/t\u1ea1o ra 1 bi\u1ebfn tham chi\u1ebfu m\u1edbi students\n        List&lt;Student> students =  new ArrayList&lt;>();\n\/\/clone d\u1eef li\u1ec7u c\u1ee7a studentList sang cho bi\u1ebfn students\n        for (int i = 0; i &lt; this.studentList.size(); i++) {\n            students.add(this.studentList.get(i));\n        }\n\/\/tr\u1ea3 v\u1ec1 bi\u1ebfn tham chi\u1ebfu m\u1edbi\n        return students;\n\/\/        return studentList;\n    }\n\n    public void setStudentList(List&lt;Student> studentList) {\n        this.studentList = studentList;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Sau khi s\u1eeda l\u1ea1i ph\u01b0\u01a1ng th\u1ee9c getStudentList th\u00ec ch\u01b0\u01a1ng tr\u00ecnh tr\u00ean s\u1ebd tr\u1ea3 v\u1ec1 2. V\u00e0 ch\u00fang ta \u0111\u00e3 \u0111\u1ea3m b\u1ea3o \u0111\u01b0\u1ee3c t\u00ednh bao g\u00f3i c\u1ee7a thu\u1ed9c t\u00ednh tham chi\u1ebfu. Oh yea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>T\u00ednh bao g\u00f3i (Encapsulation) l\u00e0 m\u1ed9t trong 4 t\u00ednh ch\u1ea5t c\u1ee7a l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng. N\u00f3 l\u00e0 c\u01a1 ch\u1ebf cho ph\u00e9p \u1ea9n nh\u1eefng th\u00f4ng tin kh\u00f4ng li\u00ean quan v\u00e0 hi\u1ec3n th\u1ecb nh\u1eefng th\u00f4ng tin c\u00f3 li\u00ean quan. Trong Java \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o \u0111\u01b0\u1ee3c t\u00ednh bao g\u00f3i, ngta d\u00f9ng access modifier private cho c\u00e1c thu\u1ed9c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":4,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts\/303\/revisions\/374"}],"wp:attachment":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}