Blogger Information
Blog 35
fans 3
comment 0
visits 25295
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月3日作业 OOP编程
随风
Original
799 people have browsed it

手抄课堂毕节


编程代码,抄写课堂案例

demo1

`<?php

//1、创建类
class Demo1{
//2、添加类成员
// 类成员分为
// 1、属性 对应外面的变量
// 2、方法 对应外面的函数

public $a = ‘张三’;
public function getInfo()
{
$obj = new Demo1();
return $obj -> a . ‘hello’;
}

}

//3、访问类成员
$obj = new Demo1();
echo $obj ->a;
echo ‘<br>‘;
echo $obj -> getInfo();
echo ‘<br>‘;
echo $obj ->a;
echo ‘<br>‘;

class Demo2{
//2、添加类成员
// 类成员分为
// 1、属性 对应外面的变量
// 2、方法 对应外面的函数

  1. public $a = '李四';
  2. public function getInfo1()
  3. {
  4. echo $this ->a;
  5. }

}
$obj=new Demo2();
echo $obj->getInfo1();
`

Demo2

`<?php

//1

class Demo2{
public $ab = ‘李四’;
public $bb =’讲师’;

  1. public function getAb()

{
return $this -> ab ;
}
public function getBb()
{
return $this -> bb;

}

}

$obj = new Demo2();
echo $obj->getAb() . ‘<br>‘;
echo $obj->getBb() . ‘<br>‘;

`

Demo3

`<?php

//1
class Demo3{

//2

  1. public $aa;
  2. public $bb;
  3. public function __construct($aa,$bb)
  4. {
  5. $this ->aa =$aa;
  6. $this ->bb =$bb;
  7. }
  8. public function getInfo()
  9. {
  10. return '我是: ' . $this->aa . $this->bb;
  11. }

}

//3
$obj =new Demo3(‘李四’,’讲师’);
echo $obj->getInfo();
echo ‘<hr>‘;

class Demo4{

//2

  1. public $aa;
  2. public $bb;
  3. public function __construct($aa,$bb)
  4. {
  5. $this ->aa =$aa;
  6. $this ->bb =$bb;
  7. echo $this->getInfo();
  8. }
  9. public function getInfo()
  10. {
  11. return '我是: ' . $this->aa . $this->bb;
  12. }

}

//3
new Demo4(‘李四1’,’讲师’);
//echo $obj->getInfo();`

demo4

`<?php
//1
$_GET[‘username’]=’admin’;
echo $_GET[‘username’];
echo ‘<hr>‘;

class Demo4{
// 2
public $aa;
public $bb;

  1. public function __construct($aa,$bb)
  2. {
  3. $this -> aa = $aa;
  4. $this -> bb = $bb;
  5. }
  6. public function getInfo(){
  7. return '我是: ' . $this->aa . $this->bb;
  8. }

// public function get($name)
// {
// $username = $_GET[‘username’] ?? ‘’;
// if(isset($username)&& $username === ‘admin’){
// return isset($this->$name) ? $this -> $name: ‘属性未定义’;
// }else{
// return ‘无权访问’;
// }
// }
public function
get($name)
{
// 仅允许用户名是’admin’的用户访问,其它访问返回: 无权访问
$username = $_GET[‘username’] ?? ‘’;
if (isset($username) && $username === ‘admin’) {
return isset($this->$name) ? $this->$name : ‘属性未定义’;
} else {
return ‘无权访问’;
}
}

}

$obj = new Demo4(‘www.php.cn’,’讲师’);
echo $obj->aa;
echo ‘<br>‘;
echo $obj->getInfo();
echo ‘<br>‘;
echo $obj->name;

`

Demo5

`<?php

class Demo5
{
public $aa;
public $bb;

  1. public function __construct($aa,$bb)
  2. {
  3. $this ->aa =$aa;
  4. $this ->bb =$bb;
  5. }
  6. public function getInfo(){
  7. return '我是: ' . $this->aa . '讲师: ' . $this->bb;
  8. }

}

class Demo6 extends Demo5
{
private $cc;
public function construct($aa, $bb,$cc)
{
parent::
construct($aa, $bb);
$this -> cc =$cc;
}

  1. public function getInfo()
  2. {
  3. return parent::getInfo() . 'aaa' . $this->cc; // TODO: Change the autogenerated stub
  4. }

}
$stu = new Demo5(‘gzg’,’aa’);
echo $stu -> getInfo();
echo ‘<hr>‘;

$obj = new Demo6(‘hyx’,’老师’,’php’);
echo $obj -> getInfo();`

Demo6

`<?php

trait Test
{
public function getInfo()
{
return ‘我是: ‘ . $this->aa . ‘讲师: ‘ . $this->bb;
}
}

class Demo6
{
use Test;
public $aa;
public $bb;
public function __construct($aa,$bb)
{
$this->aa =$aa;
$this -> bb = $bb;
}
}

class Demo7 extends Demo6 {
private $cc;

  1. public function __construct($aa, $bb,$cc)
  2. {
  3. parent::__construct($aa, $bb);
  4. $this->cc =$cc;
  5. }

}

$obj = new Demo6(‘hyx’,’111’);
echo $obj -> getInfo();
echo ‘<hr>‘;
$sub= new Demo7(‘gzg’,’222’,’abx’);
echo $sub -> getInfo();

`

demo7

`<?php

interface iDemo
{
public function getInfo();
public function hello();
}
//1
class Demo7 implements iDemo
{
// 2
public $aa;
public $bb;

public function __construct($aa,$bb)
{
$this ->aa =$aa;
$this -> bb =$bb;
}

public function getInfo()
{
return ‘我是: ‘ . $this->aa . ‘讲师: ‘ . $this->bb;
}

  1. public function hello()
  2. {
  3. return 'Hello 大家晚上吃饱了吗?';
  4. }

}

//3
$obj = new Demo7(‘hyx’,’111’ );
echo $obj ->getInfo() .’<br>‘;
echo $obj -> hello() .’<br>‘;`

Demo8

`<?php

abstract class Demo
{
abstract public function getInfo();
public function hello()
{
return ‘Hello 大家晚上吃饱了吗?’;
}
}

  1. class Demo8 extends Demo
  2. {
  3. public $aa;
  4. public $bb;
  5. public function __construct($aa,$bb)
  6. {
  7. $this->aa =$aa;
  8. $this ->bb =$bb ;
  9. }
  10. public function getInfo()
  11. {
  12. return '我是: ' . $this->aa . '讲师: ' . $this->bb;
  13. // TODO: Implement getInfo() method.
  14. }
  15. }
  16. $obj = new Demo8('hyx','111');

echo $obj->getInfo() . ‘<br>‘;
echo $obj->hello() . ‘<br>‘;`

手抄






总结

学习了 OOP编程,可以听懂,在简单案例中也能使用。希望在后面的案例中能学会灵活应用。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:听懂已经很不容易了, 毕竟我们是天天上课, 大家很辛苦
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post