微信扫一扫打赏支持

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."\n";
    }
}
$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}岁,我在自由自在的飞翔"."\n";
    }
}
$monkey = new Bird("大飞猴",13);
$monkey->say();

?>

 

 

 

 

 

 

 

 

 

 
posted @ 2020-06-26 00:07  范仁义  阅读(143)  评论(0编辑  收藏  举报