您当前的位置: 首页 >  Python

知其黑、受其白

暂无认证

  • 0浏览

    0关注

    1250博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

python、JavaScript、PHP 面向对象

知其黑、受其白 发布时间:2022-04-11 12:38:53 ,浏览量:0

阅读目录
  • python
  • JavaScript
  • PHP

python、js、php 面向对象的逻辑都是一样的,具体实现因为语言的不同而略有区别,比如python 中继承用的是圆括号,比如 class Bird(Animal):

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();
JavaScript



    
    
    Document



    // 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();



在这里插入图片描述

PHP
            
关注
打赏
1665558895
查看更多评论
0.0454s