{"id":660,"date":"2020-07-22T13:29:24","date_gmt":"2020-07-22T06:29:24","guid":{"rendered":"https:\/\/www.marketenterprise.vn\/blog\/?p=660"},"modified":"2021-12-27T19:59:09","modified_gmt":"2021-12-27T12:59:09","slug":"tinh-da-hinh-trong-oop","status":"publish","type":"post","link":"https:\/\/www.marketenterprise.vn\/blog\/tinh-da-hinh-trong-oop.html","title":{"rendered":"T\u00ednh \u0111a h\u00ecnh (Polymorphism) trong l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng"},"content":{"rendered":"<h4><\/h4>\n<h2 class=\"article-content__title\">4 \u0111\u0103\u0323c ti\u0301nh cu\u0309a l\u00e2\u0323p tri\u0300nh h\u01b0\u01a1\u0301ng \u0111\u00f4\u0301i t\u01b0\u01a1\u0323ng (Object Oriented Programming)<\/h2>\n<ol>\n<li id=\"_tinh-dong-goi-encapsulation-0\"><a href=\"https:\/\/www.marketenterprise.vn\/blog\/tinh-dong-goi-trong-lap-trinh-huong-doi-tuong.html\"><strong>Ti\u0301nh \u0111o\u0301ng go\u0301i (Encapsulation)<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.marketenterprise.vn\/blog\/tinh-ke-thua-inheritance-trong-lap-trinh-huong-doi-tuong.html\"><strong>T\u00ednh k\u1ebf th\u1eeba (Inheritance)<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.marketenterprise.vn\/blog\/tinh-da-hinh-polymorphism-trong-lap-trinh-huong-doi-tuong.html\"><strong>T\u00ednh \u0111a h\u00ecnh (Polymorphism)<\/strong><\/a><\/li>\n<li><strong>T\u00ednh tr\u1eebu t\u01b0\u1ee3ng (Abstraction)<br \/>\n<\/strong><\/li>\n<\/ol>\n<p><strong>Trong b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta c\u00f9ng nhau t\u00ecm hi\u1ec3u v\u1ec1 &#8220;T\u00ednh \u0111a h\u00ecnh (Polymorphism)&#8221;<\/strong><\/p>\n<h2>\u0110\u1ecbnh ngh\u0129a t\u00ednh \u0111a h\u00ecnh<\/h2>\n<p><span style=\"font-weight: 400;\">T\u00ednh \u0111a h\u00ecnh (<\/span><i><span style=\"font-weight: 400;\">Polymorphism<\/span><\/i><span style=\"font-weight: 400;\">) l\u00e0 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t t\u00e1c v\u1ee5 theo nhi\u1ec1u c\u00e1ch kh\u00e1c nhau.<\/span><\/p>\n<h2>C\u00f4ng d\u1ee5ng c\u1ee7a t\u00ednh \u0111a h\u00ecnh<\/h2>\n<p><span style=\"font-weight: 400;\">K\u1ef9 thu\u1eadt \u0111a h\u00ecnh trong c\u00e1c ng\u00f4n ng\u1eef h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng gi\u00fap cho ch\u00fang ta gia t\u0103ng kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng c\u00e1c \u0111o\u1ea1n m\u00e3 (code) \u0111\u01b0\u1ee3c vi\u1ebft m\u1ed9t c\u00e1ch t\u1ed5ng qu\u00e1t v\u00e0 c\u00f3 th\u1ec3 thay \u0111\u1ed5i c\u00e1c \u1ee9ng x\u1eed m\u1ed9t c\u00e1ch linh ho\u1ea1t t\u00f9y theo \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n<h2>C\u00e1ch s\u1eed d\u1ee5ng<\/h2>\n<p><span style=\"font-weight: 400;\">Trong l\u1eadp tr\u00ecnh s\u1ebd c\u00f3 2 k\u1ef9 thu\u1eadt \u0111\u1ec3 ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed t\u00ednh \u0111a h\u00ecnh \u0111\u00f3 l\u00e0 <\/span><span style=\"font-weight: 400;\">Override v\u00e0 Overloading.<\/span><\/p>\n<h3>Override (Ghi \u0111\u00e8)<\/h3>\n<p><i><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/i><span style=\"font-weight: 400;\"> Trong m\u1ed9t c\u00f4ng ty s\u1ebd c\u00f3 c\u00e1c <\/span><b>nh\u00e2n vi\u00ean<\/b><span style=\"font-weight: 400;\"> nh\u01b0 l\u1eadp tr\u00ecnh vi\u00ean, k\u1ebf to\u00e1n, nh\u00e2n vi\u00ean sale. M\u1ed7i <\/span><b>nh\u00e2n vi\u00ean<\/b><span style=\"font-weight: 400;\"> s\u1ebd c\u00f3 nh\u1eefng c\u00e1ch t\u00ednh l\u01b0\u01a1ng kh\u00e1c nhau.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">D\u1ef1a tr\u00ean v\u00ed d\u1ee5, ch\u00fang ta s\u1ebd c\u00f3 class <\/span><b>Staff<\/b><span style=\"font-weight: 400;\"> \u0111\u1ea1i di\u1ec7n cho nh\u00e2n vi\u00ean v\u00e0 c\u00f3 thu\u1ed9c t\u00ednh <\/span><span style=\"font-weight: 400;\">salary l\u00e0 m\u1ee9c l\u01b0\u01a1ng c\u01a1 b\u1ea3n<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class Staff {\r\n   public int salary = 3;\r\n\r\n   public int countSalary() {\r\n       return this.salary;\r\n   }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Ti\u1ebfp theo ch\u00fang ta c\u00f3 <\/span><i><span style=\"font-weight: 400;\">class<\/span><\/i> <b>Developer<\/b><span style=\"font-weight: 400;\"> k\u1ebf th\u1eeba <\/span><i><span style=\"font-weight: 400;\">class<\/span><\/i> <b>Staff<\/b> <span style=\"font-weight: 400;\">v\u00e0 s\u1ebd ghi \u0111\u00e8 l\u00ean ph\u01b0\u01a1ng th\u1ee9c t\u00ednh l\u01b0\u01a1ng.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class Developer extends Staff {\r\n   public int rank = 4;\r\n  \r\n   @Override\r\n   public int countSalary() {\r\n       return this.salary * this.rank;\r\n   }\r\n}\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Nh\u00e2n vi\u00ean k\u1ebf to\u00e1n c\u00f3 th\u00eam tr\u1ee3 c\u1ea5p n\u00ean m\u1ed7i l\u1ea7n t\u00ednh l\u01b0\u01a1ng s\u1ebd \u0111\u01b0\u1ee3c c\u1ed9ng th\u00eam. N\u00ean ta c\u00f3 class <\/span><b>Accountant<\/b><span style=\"font-weight: 400;\"> kh\u00e1c v\u1edbi class <\/span><b>Developer.<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class Accountant extends Staff {\r\n   public int rank = 3;\r\n   public int allowance = 3;\r\n\r\n   @Override\r\n   public int countSalary() {\r\n       return (this.salary * this.rank) + this.allowance;\r\n   }\r\n}<\/pre>\n<h3><\/h3>\n<h3>Overloading (N\u1ea1p ch\u1ed3ng)<\/h3>\n<p><span style=\"font-weight: 400;\">Quay l\u1ea1i v\u00ed d\u1ee5 tr\u00ean, <\/span><span style=\"font-weight: 400;\">trong qu\u00e1 tr\u00ecnh l\u00e0m vi\u1ec7c, s\u1ebd c\u00f3 l\u00fac l\u1eadp tr\u00ecnh vi\u00ean ph\u1ea3i t\u0103ng ca, v\u00ec v\u1eady kho\u1ea3n ti\u1ec1n t\u0103ng ca n\u00e0y s\u1ebd \u0111\u01b0\u1ee3c c\u1ed9ng th\u00eam v\u00e0o l\u01b0\u01a1ng. N\u1ebfu kh\u00f4ng t\u0103ng ca, th\u00ec l\u01b0\u01a1ng c\u1ee7a l\u1eadp tr\u00ecnh vi\u00ean s\u1ebd \u0111\u01b0\u1ee3c t\u00ednh theo c\u00f4ng th\u1ee9c nh\u01b0 b\u00ecnh th\u01b0\u1eddng.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class Developer extends Staff {\r\n   public int rank = 4;\r\n  \r\n   @Overload\r\n   public int countSalary() {\r\n       return this.salary * this.rank;\r\n   }\r\n\r\n   @Overload\r\n   public int countSalary(int overtime_money) {\r\n       return (this.salary * this.rank) + overtime_money;\r\n   }\r\n}<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">public class Demo {\r\n \r\n    public static void main(String[] args) { \r\n        Developer developer = new Developer();\r\n \r\n        System.out.println(\"Basic salary:\" + developer.countSalary());\r\n        System.out.println(\"Salary with overtime money: \" + developer.countSalary(2));\r\n    }\r\n}<\/pre>\n<p><i><span style=\"font-weight: 400;\">Output<\/span><\/i><b>:<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Basic salary: 12\r\nSalary with overtime money: 14\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Khi ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh ch\u00fang ta s\u1ebd th\u1ea5y d\u1ef1a theo tham s\u1ed1 \u0111\u1ea7u v\u00e0o ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd \u0111i\u1ec1u h\u01b0\u1edbng t\u1edbi ph\u01b0\u01a1ng th\u1ee9c t\u01b0\u01a1ng \u1ee9ng.<\/span><\/p>\n<h2>T\u00e0i li\u1ec7u tham kh\u1ea3o.<\/h2>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming<\/span><\/a><\/p>\n<p><a href=\"https:\/\/stackify.com\/oop-concept-polymorphism\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/stackify.com\/oop-concept-polymorphism\/<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>T\u00ednh \u0111a h\u00ecnh l\u00e0 m\u1ed9t trong b\u1ed1n t\u00ednh ch\u1ea5t c\u01a1 b\u1ea3n c\u1ee7a l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng.<\/p>\n<p>Trong b\u00e0i vi\u1ebft n\u00e0y ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 t\u00ednh \u0111a h\u00ecnh nh\u00e9.<\/p>\n","protected":false},"author":16,"featured_media":661,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[34,22],"class_list":["post-660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-marketenterprise-vietnam","tag-technical"],"_links":{"self":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":0,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media\/661"}],"wp:attachment":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}