markdown 学习 - 面向对象的编程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 学习 - 面向对象的编程相关的知识,希望对你有一定的参考价值。

# OOP

## House Analogy
* Object: The house, itself, is the object.
* Class: The blueprints for the house.
* Properties: Color, shape, number of windows, etc.
* Methods: Build house, heat house, sell house, actions, etc.
* Instantiation: The construction.

## Car
* Object: The car.
* Class: The model of car
* Properties: Color, engine size, wheels
* Methods: Start car, drive car, turn, etc
* Instantiation: Create car.

## Examples

```php
ini_set( 'display_errors', '1' );

// Public - Can access from class, child class, or on the variable assignment after instantiaion.
// Protected - Can access only from class or child class.
// Private - Can access only from class.
// Final - Cannot override in child class.
// Static - Can call method or variable without having to instantiate class. Older method, we use namespaces now. // MyClass::$variable;

class MyClass {
	protected $var = 'I like OOP';
	static $testing = '12345';

	public function __construct($text) {
		$this->var = $text;
	}

	protected function my_function() {
		echo $this->var;
	}
}

class NewClass extends MyClass {
	public function __construct($any) { // Pass in our variables if any.
		parent::__construct($any); // Run the parent constructor.
	}

	public function my_function() {
		echo 'Override';
	}

	public function test() {
		echo $this->var;
	}
}

$new = new NewClass('protected');

echo MyClass::$testing;
```

以上是关于markdown 学习 - 面向对象的编程的主要内容,如果未能解决你的问题,请参考以下文章

python学习笔记:面向对象编程类

python学习笔记:面向对象编程类

python学习笔记:面向对象编程类

python学习笔记8--面向对象编程

python学习笔记(十九)面向对象编程,类

python面向对象编程学习