批改状态:未批改
老师批语:
问答:什么是类,什么是对象,举例说明:
答:从字面意思理解,类所囊括的范围比对象要宽泛,也可以简单的理解为对象时被包含在类里的,任何一个对象都可以在相对应类里找到,同样的,使用类里的方法,可以创建出各种符合类的规范的对象。比如:羽毛球运动,如果把整个运动看成一个类的话,这个类里包含了5个项目,也就是5个对象,男女单,男女双打,混双。如果把某个单项看成一个类的话,以男单为例,这个类里有各个国家的选手,他们就是男单里面的类,而把这些运动员按照国籍区分为不同国籍的对象,这就叫方法。
<?php
class wb_Player
{
private $name;//私有属性创建:姓名
private $age;//私有属性创建:年龄
private $mobile;//私有属性创建:手机号
private $club;//私有属性创建:所属俱乐部
private $score;//私有属性创建:积分
private $rank; //私有属性创建:排名
private $event;//私有属性创建:单项
public function __construct($name,$age,$mobile,$club,$score,$rank,$event)//使用__construct声明构造方法,使上述所创建的属性仅在本对象内部使用。
{
$this->name = $name;
$this->age = $age;
$this->club = $club;
$this->mobile = $mobile;
$this->score = $score;
$this->rank = $rank;
$this->event = $event;
}
//创建对外访问的接口,采用魔术方法,自动调用。
public function __get($name)
{
return $this->$name;
}
//设置器
public function __set($name,$value)
{
$this->$name = $value;
}
}点击 "运行实例" 按钮查看在线实例
<?php
class badPlayer
{
private $name;
private $contury;
private $skill;
public function __construct($name,$contury,$skill)
{
$this->name = $name;
$this->contury = $contury;
$this->skill = $skill;
}
public function __set($name,$value)
{
$this->$name = $value;
}
public function __get($name)
{
return $this->$name;
}
}点击 "运行实例" 按钮查看在线实例
<?php
class girlFriend1
{
private $name;
Private $age;
private $stature;
private $weight;
public function __construct($name,$age,array $stature,float $weight){
$this->name = $name;
$this->age = $aeg;
$this->stature = $stature;
$this->weight = $weight;
}
public function __get($name){
return $this->$name;
}
public function __set($name,$value){
$this->$name = $value;
}
}点击 "运行实例" 按钮查看在线实例
<?php
/**
*1. 问答: 什么类,什么是对象,举例说明;
* 2. 编程: 参考object/demo3.php,自定义类与实例化,要求必须将属性私有化,通过公共接口__set()和get()进行访问(必须写5遍以上)
* 3. 编程: MySQL常用的增删改查语句(CURD),每个语句必须写10遍以上;
* 4. 编程: 数据库的连接与检测(至少写5遍以上,写到吐为止),将连接参数写到独立的配置文件,要求配置参数必须用数组来实现,并在连接脚本中正确引用。
* 5. 要求,代码中的每一行必须要有注释
* 6. 手写: 与MySQLil连接相关的几个属性和方法的名称,参数,与功能和用法
* mysql类, $mysqli->errno, $mysqli->error, $mysqli->select_db(), $mysqli->set_charset(), 以上2个属性与2个方法,要求达到背诵级别
*/
require 'object/class/badPlayer.php';//获取已经创建的类
$player1 = new badPlayer('林丹','China','Smash');
echo $player1->name,'<br>';
$player2 = new badPlayer('李宗伟','马来西亚','弹簧腿');
echo $player2->skill.'<hr>';
require 'object/class/girlFriend1.php';
$girlFriend1 = new girlFriend1('高圆圆',34,[95,87,92],98);
echo '我女朋友的体重是:'.$girlFriend1->weight.'斤<hr>';
echo '胸围'.$girlFriend1->stature[0];
require 'object/class/wb_Player.php';
$wb_Player1 = new wb_Player('高圆圆',35,13000000000,'WB Club',8830,2,'混双');
$wb_Player2 = new wb_Player('李宗伟',35,18888888888,'WB Club',8233,2,'男单');
$wb_Player3 = new wb_Player('林丹',35,13000000000,'WB Club',2122,5,'男单');
$wb_Player4 = new wb_Player('谌龙',28,13000000000,'WB Club',2230,1,'男单');
$wb_Player5 = new wb_Player('戴资颖',22,13000000000,'WB Club',8980,1,'女单');
$wbplayer = [$wb_Player1,$wb_Player2,$wb_Player3,$wb_Player4,$wb_Player5];
echo '<pre>';
foreach ($wbplayer as $value){
echo var_export($wbplayer).'<br>';
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号