美文网首页
面向对象编程

面向对象编程

作者: Treehl | 来源:发表于2018-05-30 08:13 被阅读0次

面向对象编程

参考廖雪峰的Python教程

面向对象编程-------Object Oriented Programming(OOP)

Python面向对象编程三大特点:

  • 数据封装
  • 继承
  • 多态
  • 鸭子类型

1、数据封装

这里我们定义一个Cat类,每个实例都可以拥有name和age属性,我们可以调用内部方法来访问这些数据,这样就把数据封装起来了,这些封装数据的函数本身是与Cat类相关联的,我们称之为“类的方法”

# -*- coding:utf-8 -*-
class Cat(object):
    """数据封装
    1、每个Cat实例都有name、age属性,我们可以通过函数来访问这些数据
    2、__init__()方法:根据Cat类创建实例时都会调用它,并初始化实例的属性
    3、self就是实例本身
    """
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def play(self):
        print('%s is %s years old and he is playing with another cat!' % (self.name, self.age))


# 创建实例
c = Cat('fat', 2)
# 实例调用play方法
c.play()



>>> fat is 2 years old and he is playing with another cat!


封装的另外一个好处就是可以给Cat类再增加一个新的方法


  def sleep(self):
        print('%s is tired, he want to sleep!' % (self.name.title(),))



>>> Fat is tired, he want to sleep!

2、继承

继承的好处时子类获得父类所有的功能,这里先定义一个Animal类

# -*- coding:utf-8 -*-
class Animal(object):
    def run(self):
        print('Animal is running!')


a = Animal()
a.run()

>>> Animal is running!

这里再定义Cat类和Dog类,它们都继承自Animal,Animal是Cat和Dog的父类(超类或基类),而Cat和Dog是Animal的子类。Cat和Dog继承Animal后自动获得了Animal类的run方法

# -*- coding:utf-8 -*-
class Animal(object):
    def run(self):
        print('Animal is running!')


class Cat(Animal):
    pass


class Dog(Animal):
    pass


c = Cat()
d = Dog()
c.run()
d.run()

这里我们给子类增加run方法,当子类和父类都存在run方法时,子类覆盖掉父类方法,所以输出是
Cat is running!
Dog is running!

# -*- coding:utf-8 -*-
class Animal(object):
    def run(self):
        print('Animal is running!')


class Cat(Animal):
    def run(self):
        print('Cat is running!')


class Dog(Animal):
    def run(self):
        print('Dog is running!')


c = Cat()
d = Dog()
c.run()
d.run()

>>> Cat is running!
    Dog is running!


3、多态

当我们定义class的时候就定义了一种数据类型,这里用isinstance()函数来判断对象是否是已知的类型


a = list()
b = Animal()
c = Cat()

print(isinstance(a, list))
print(isinstance(b, Animal))
print(isinstance(c, Cat))


>>> True
    True
    True

判断都为True,这里可以试下isinstance(c, Animal)是什么结果,Cat的实例c继承自Animal,所以c也是Animal类型


print(isinstance(c, Animal))

>>> True

在继承关系中,如果一个实例的数据类型是某个子类,那它的数据类型也可以被看做是父类。但是,反过来就不行

Cat可以被看成Animal,但Animal不能被看成Cat

isinstance(c, Cat)


b = Animal()

print(isinstance(b, Cat))

>>> False


再编写一个函数来来理解多态,每当我们传入实例,就打印出各个类相应的结果


def run_twice(animal):
    animal.run()
    animal.run()


run_twice(Animal())
run_twice(Cat())
run_twice(Dog())



>>> Animal is running!
    Animal is running!
    Cat is running!
    Cat is running!
    Dog is running!
    Dog is running!


这里我们再增加Lion子类

class Lion(Animal):
    def run(self):
        print('Lion is running!')


>>> Lion is running!
    Lion is running!

Animal类型有run方法,只要传入的任意类型是animal类或者子类就会自动调用自己的run方法

鸭子类型

  • 一个对象只要“看起来像鸭子,走起路来像鸭子”,那它就可以被看做是鸭子。也可以这样理解,并不关心对象是什么类型,到底是不是鸭子,只关心行为

我们再增加一个子类,它不是animal类型,只要它有一个run()方法就可以了(只关心它的行为,而不关心它到底是不是animal)

class Timer(object):
    def run(self):
        print('Start........')

>>> Start........
    Start........

完整代码


# -*- coding:utf-8 -*-
class Animal(object):
    def run(self):
        print('Animal is running!')


class Cat(Animal):
    def run(self):
        print('Cat is running!')


class Dog(Animal):
    def run(self):
        print('Dog is running!')


class Lion(Animal):
    def run(self):
        print('Lion is running!')


class Timer(object):
    def run(self):
        print('Start........')


a = list()
b = Animal()
c = Cat()


def run_twice(fish):
    fish.run()
    fish.run()


run_twice(Animal())
run_twice(Cat())
run_twice(Dog())
run_twice(Lion())
run_twice(Timer())


print(isinstance(a, list))
print(isinstance(b, Animal))
print(isinstance(c, Cat))

print(isinstance(c, Animal))

print(isinstance(b, Cat))


欢迎访问Treehl的博客
Github

相关文章

  • 面向对象_初识

    目录 面向对象编程介绍 类与对象介绍 私有属性与私有方法 面向对象编程 1. 面向对象编程介绍 面向对象编程:Ob...

  • 谈谈面向对象编程

    何为面向对象编程 面向对象编程简介 面向对象编程(Object-oriented Programming,缩写:O...

  • 面向对象基础

    面向对象编程包括: 面向对象的分析(OOA) 面向对象的设计(OOD) 面向对象的编程实现(OOP) 面向对象思想...

  • python-day14

    一、面向对象编程 编程思想:1.面向对象编程 --> 算法,逻辑2.函数式编程 --> 函数3.面向对象编程 ...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • 面向对象浅析

    ### 面向对象编程和面向对象编程语言 面向对象编程的英文缩写是 OOP,全称是 Object Oriented ...

  • 2017-08-14

    面向对象编程用对象的思想去写代码,就是面向对象编程-面向过程-面向对象面向对象编程的特点1.抽象 抽取一样的东西...

  • 面向对象编程,类和对象

    面向对象编程 Java是面向对象的一门编程语言,所以余姚使用者具备面向对象编程的思想。 那么,什么是面向对象编程呢...

  • Swift和OC的区别

    一、编程范式 Swift可以面向协议编程、面向函数编程、面向对象编程。 OC主要是面向对象编程。 二、类型安全 S...

网友评论

      本文标题:面向对象编程

      本文链接:https://www.haomeiwen.com/subject/igazjftx.html