当前位置:网站首页>「PHP基础知识」PHP中对象的使用
「PHP基础知识」PHP中对象的使用
2022-08-02 03:16:00 【夜晚回家】
功能要求
定义一个Animal类,其中包括$name、$age、$weight、$sex属性和run()和eat()方法,在run()方法中输出“Haha. I can run!”,在eat()方法中输出“Haha. I can eat!”。创建一个Animal类的对象,并分别调用run()和eat()方法将两个方法中的输出语句的信息显示在页面中。
实例代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中对象的使用</title>
</head>
<body>
<?php
class Animal
{
public $name; //动物名称
public $age; //动物年龄
public $weight; //动物体重
public $sex; //动物性别
public function run()//方法:会跑
{
echo "Haha. I can run!<br />";
}
public function eat()//方法:能吃
{
echo "Haha. I can eat!<br />";
}
}
$dog = new Animal();
$dog->run();//输出:Haha. I can run!
$dog->eat();//输出:Haha. I can eat!
?>
</body>运行结果
知识说明
目前编程语言用到的方法有面向过程和面向对象两种。在PHP中,用户可以使用者两种方法来调用方法。
对象是一种高级的数据类型。任何事物都可以被看作一个对象。一个对象由部分属性值和方法构成,属性表明对象的一种状态,方法通常是用来实现功能的。
边栏推荐
- 5.合宙Air32F103_LCD_key
- 【C语言万字长文】 宏定义 结构体 共用体 内存对齐知识点总结
- Heao Technology Network Interview (with reference answers)
- 7-44 基于词频的文件相似度 (30 分)
- Good Key, Bad Key (thinking, temporary exchange, classic method)
- TRICK second bullet
- rem adaptation
- Webshell upload method
- TRICK第二弹
- Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
猜你喜欢

每日练习------有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

青蛙跳台阶:我如何得知它是一道斐波那契数列题?——应用题破题“三板斧”

STM32——LCD—TFTLCD原理与配置介绍

二维数组实战项目--------《三子棋》

Scaffolding installation

I will give you a chance to interview in a big factory. Can you interview?Come in and see!

MySQL8.0.28安装教程

MySQL8.0.26安装配置教程(windows 64位)

MySQL中的存储过程(详细篇)

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
随机推荐
Kubernetes 基本概念
WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use
Double Strings (don't always forget substr)
考虑饱和的多智能体系统数据驱动双向一致性
TRICK第二弹
MySQL8.0.28 installation tutorial
MySQL中的存储过程(详细篇)
5.合宙Air32F103_LCD_key
线性代数学习笔记1:何为线性代数
Redis安装,基本命令,持久化方式,集群
PHP WebShell Free Kill
Using WebShell to get Shell Skills
什么是轮式里程计
深度自编码网络的集成学习ICPS入侵检测模型
Keil development environment installation tutorial
三维数字孪生引擎与实景互动,案例解析
day11--shell脚本
LeetCode:第304场周赛【总结】
TRICK second bullet
(forwarded) HashCode summary (2)