Bài giảng Phát triển phần mềm nguồn mở: Bài 4 - Nguyễn Hữu Thể
Số trang: 32
Loại file: pdf
Dung lượng: 971.35 KB
Lượt xem: 23
Lượt tải: 0
Xem trước 4 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài 4 trình bày về lập trình hướng đối tượng "object oriented programming". Nội dung chính trong chương này gồm có: Class, visibility, properties & methods, Getter & getter, create objects, constructor, destructor, inheritance, abstract class, interfaces, autoloading classes, anonymous functions, closures, namespace. Mời các bạn cùng tham khảo.
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển phần mềm nguồn mở: Bài 4 - Nguyễn Hữu Thể PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ OBJECT ORIENTED PROGRAMMING Nguyễn Hữu Thể Content 1. Class 2. Visibility 3. Properties & Methods 4. Getter & Setter 5. Create objects 6. Constructor 7. Destructor 8. Inheritance 9. Abstract class 10. Interfaces 11. Autoloading classes 12. Anonymous functions 13. Closures 14. Namespace 2 Class class ClassName { // Properties // Methods } 3 Visibility (public, private, protected) Three levels: • public • private • protected By default, all class members are public. 4 Properties & Methods class Person { private $name; //public, protected private $age; public function show(){ echo $this->name . ' is ' . $this->age . ' years old!'; } } 5 Create objects (Create a new instance of the class) Using the new keyword: new ClassName() For example: $person = new PerSon(); $person2 = new PerSon(); 6 Getter & Setter PHP JAVA class Person class Person { { private $name; private String name; private $age; private int age; public function getName(){ public String getName(){ return $this->name; return name; } } public function setName($name){ public void setName(String name){ $this->name = $name; this.name = name; } } public function getAge(){ public int getAge(){ return $this->age; return age; } } public function setAge($age){ public void setAge(int age){ $this->age = $age; this.age = age; } } } } 7 class Person{ Ex: Person.php private $name; private $age; public function getName(){ return $this->name; } public function setName($name){ $this->name = $name; } public function getAge(){ return $this->age; } public function setAge($age){ $this->age = $age; } public function show(){ echo $this->name . ' is ' . $this->age . ' years old!'; } $p = new Person(); } $p->setName('Nguyễn Văn A'); $p->setAge(18); echo $p->getName() . ' is ' . $p->getAge() . ' years old.'; //echo '{$p->getName()} is {$p->getAge()} years old.'; $p->show(); 8 __set() method class SetName { public function __set($variable, $value) { // echo $variable; echo 'My ' . $variable . ' is ' . $value . '\n'; } } $obj = new SetName (); $obj->Name = ‘Tom'; $obj->Name = ‘Jerry'; My Name is Tom My Name is Jerry 9 __get() method class GetName { public $type = 'chocolate'; public $choctype = array ( 'milk' => 0, 'dark' => 1, 'plain' => 2 ); public function wrap() { echo 'Its a wrap'; } public function __get($index) { echo '$choctype property with index of: ' . $index . ''; return $this->choctype [$index]; } } $candy = new GetName (); // set a non existant property echo 'Value of property is: ' . $candy->milk; $choctype property with index of: milk 10 Value of property is: 0 Constructor public function __construct() { //... } public function __construct($name, $age) { //... } 11 Constructor class Person{ private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function show(){ echo $this->name . ' is ' . $this->age . ' years old!'; } } $p = new Person('Nguyễn Trần Lê', 18); $p->show(); 12 Destructor public function __destruct() { //... } Example: public function __destruct() { echo 'Bye bye!'; //... } Using: $p = new Person('Nguyễn Trần Lê', 18); unset( $p ); // or exit: call __destruct() object $p->show(); // Object not found 13 Inheritance class ParentClass { public function myMethod() { // Method code here } } class ChildClass extends ParentClass { public function myMethod() { // For ChildClass objects, this method is called ...
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển phần mềm nguồn mở: Bài 4 - Nguyễn Hữu Thể PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ OBJECT ORIENTED PROGRAMMING Nguyễn Hữu Thể Content 1. Class 2. Visibility 3. Properties & Methods 4. Getter & Setter 5. Create objects 6. Constructor 7. Destructor 8. Inheritance 9. Abstract class 10. Interfaces 11. Autoloading classes 12. Anonymous functions 13. Closures 14. Namespace 2 Class class ClassName { // Properties // Methods } 3 Visibility (public, private, protected) Three levels: • public • private • protected By default, all class members are public. 4 Properties & Methods class Person { private $name; //public, protected private $age; public function show(){ echo $this->name . ' is ' . $this->age . ' years old!'; } } 5 Create objects (Create a new instance of the class) Using the new keyword: new ClassName() For example: $person = new PerSon(); $person2 = new PerSon(); 6 Getter & Setter PHP JAVA class Person class Person { { private $name; private String name; private $age; private int age; public function getName(){ public String getName(){ return $this->name; return name; } } public function setName($name){ public void setName(String name){ $this->name = $name; this.name = name; } } public function getAge(){ public int getAge(){ return $this->age; return age; } } public function setAge($age){ public void setAge(int age){ $this->age = $age; this.age = age; } } } } 7 class Person{ Ex: Person.php private $name; private $age; public function getName(){ return $this->name; } public function setName($name){ $this->name = $name; } public function getAge(){ return $this->age; } public function setAge($age){ $this->age = $age; } public function show(){ echo $this->name . ' is ' . $this->age . ' years old!'; } $p = new Person(); } $p->setName('Nguyễn Văn A'); $p->setAge(18); echo $p->getName() . ' is ' . $p->getAge() . ' years old.'; //echo '{$p->getName()} is {$p->getAge()} years old.'; $p->show(); 8 __set() method class SetName { public function __set($variable, $value) { // echo $variable; echo 'My ' . $variable . ' is ' . $value . '\n'; } } $obj = new SetName (); $obj->Name = ‘Tom'; $obj->Name = ‘Jerry'; My Name is Tom My Name is Jerry 9 __get() method class GetName { public $type = 'chocolate'; public $choctype = array ( 'milk' => 0, 'dark' => 1, 'plain' => 2 ); public function wrap() { echo 'Its a wrap'; } public function __get($index) { echo '$choctype property with index of: ' . $index . ''; return $this->choctype [$index]; } } $candy = new GetName (); // set a non existant property echo 'Value of property is: ' . $candy->milk; $choctype property with index of: milk 10 Value of property is: 0 Constructor public function __construct() { //... } public function __construct($name, $age) { //... } 11 Constructor class Person{ private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function show(){ echo $this->name . ' is ' . $this->age . ' years old!'; } } $p = new Person('Nguyễn Trần Lê', 18); $p->show(); 12 Destructor public function __destruct() { //... } Example: public function __destruct() { echo 'Bye bye!'; //... } Using: $p = new Person('Nguyễn Trần Lê', 18); unset( $p ); // or exit: call __destruct() object $p->show(); // Object not found 13 Inheritance class ParentClass { public function myMethod() { // Method code here } } class ChildClass extends ParentClass { public function myMethod() { // For ChildClass objects, this method is called ...
Tìm kiếm theo từ khóa liên quan:
Phần mềm nguồn mở Phát triển phần mềm nguồn mở Object oriented programming Lập trình hướng đối tượng Create objects Autoloading classesGợi ý tài liệu liên quan:
-
183 trang 318 0 0
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 276 0 0 -
101 trang 200 1 0
-
'Phần mềm tự do và phần mềm nguồn mở' Free and Open Source Software – Asia-Pacific Consultation
5 trang 134 0 0 -
14 trang 134 0 0
-
Giáo trình lập trình hướng đối tượng - Lê Thị Mỹ Hạnh ĐH Đà Nẵng
165 trang 112 0 0 -
Giáo trình Lập trình Windows 1 - Trường CĐN Đà Lạt
117 trang 96 0 0 -
Giáo trình Phân tích, thiết kế hướng đối tượng với UML: Phần 1 - Trường ĐH Công nghiệp Quảng Ninh
111 trang 95 0 0 -
Xây dựng hệ thống tích hợp liên tục nội bộ sử dụng công cụ nguồn mở Jenkins và Gitlab
11 trang 88 0 0 -
265 trang 80 0 0