php类的继承

<?php
/*
 * 类的继承(extends):
 * 指可以创建一个新的类,该类继承(extends) 了父类的属性和方法,并且可以添加自己的属性和方法
*/
class Animal {
    public string $name = 'neko';
    protected int $age = 20;
    private string $birth = '2020-02-11';
}

class Dog extends Animal {
    public function setAge(int $age): void
    {
        $this->age = $age;
        echo $this->age;
    }
}

//var_dump(new Animal());
$dog = new Dog();
echo $dog->name . "\n";
$dog->setAge(2);

给TA打赏
共{{data.count}}人
人已打赏
日常笔记

class类里的static静态方法

2024-7-20 15:58:10

日常笔记

类方法属性重写和final关键字

2024-7-21 16:14:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索