python、js、php区别---7、面向对象

python、js、php区别---7、面向对象

一、总结

一句话总结:

python、js、php面向对象的逻辑都是一样的,具体实现因为语言的不同而略有区别,比如python中继承用的是圆括号,比如class Bird(Animal):
"""
需求:
创建Animal类(name属性,say方法)
创建Animal类的子类Bird类(age属性,say方法)
"""
class Animal:
    def __init__(self,name):
        self.name = name
        pass
    def say(self):
        print("我是{}".format(self.name))

animal1 = Animal("大动物")
animal1.say()

class Bird(Animal):
    def __init__(self,name,age):
        # Animal.__init__(self,name)
        # super(Bird,self).__init__(name)
        super().__init__(name) 
        self.age = age
        pass
    def say(self):
        print("我是{},我今年{}岁,我在自由自在的飞翔".format(self.name,self.age))

monkey=Bird('大飞猴',15);
monkey.say();

二、面向对象

博客对应课程的视频位置:7、面向对象
https://www.fanrenyi.com/video/33/302

1、python

"""
需求:
创建Animal类(name属性,say方法)
创建Animal类的子类Bird类(age属性,say方法)
"""
class Animal:
    def __init__(self,name):
        self.name = name
        pass
    def say(self):
        print("我是{}".format(self.name))

animal1 = Animal("大动物")
animal1.say()

class Bird(Animal):
    def __init__(self,name,age):
        # Animal.__init__(self,name)
        # super(Bird,self).__init__(name)
        super().__init__(name) 
        self.age = age
        pass
    def say(self):
        print("我是{},我今年{}岁,我在自由自在的飞翔".format(self.name,self.age))

monkey=Bird('大飞猴',15);
monkey.say();

2、js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*
        js面向对象
        */
        class Animal {
            constructor(name) {
                this.name = name;
            }
            say() {
                console.log('我是'+this.name);
            }
        }
        let animal1=new Animal('大动物');
        animal1.say();

        class Bird extends Animal {
            constructor(name, age) {
                super(name);
                this.age = age;
            }
            say() {
                console.log('我是'+this.name+','+this.age+'岁,我在自由自在的飞翔!');
            }
        }
        let monkey=new Bird('大飞猴',15);
        monkey.say();
    </script>
</body>
</html>

3、php

<?php
/*
php面向对象
*/
class Animal{
    function __construct($name) {
        $this->name = $name;
    }
    function say(){
        echo "我是".$this->name."
";
    }
}
$animal1 = new Animal("大动物");
$animal1->say();

class Bird extends Animal{
    function __construct($name,$age) {
        parent::__construct($name);
        $this->age = $age;
    }
    function say(){
        echo "我是{$this->name},我今年{$this->age}岁,我在自由自在的飞翔"."
";
    }
}
$monkey = new Bird("大飞猴",13);
$monkey->say();

?>
 
我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站: fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
博主25岁,前端后端算法大数据人工智能都有兴趣。
大家有啥都可以加博主联系方式(qq404006308,微信fan404006308)互相交流。工作、生活、心境,可以互相启迪。
聊技术,交朋友,修心境,qq404006308,微信fan404006308
26岁,真心找女朋友,非诚勿扰,微信fan404006308,qq404006308
人工智能群:939687837

作者相关推荐

原文地址:https://www.cnblogs.com/Renyi-Fan/p/13180058.html