Python_day_07_面向对象编程

面向过程:
面向函数:
面向对象:

1.定义一个类

def 函数名 ( 形参 ):
    函数执行的内容
类的格式
class 类名 ( 父类 ):
    类的内容

类名后面有括号的类,称为新式类;
括号里面的内容是父类的名称;程序中,所有类的父类都是 object;

class Animals(object):
    pass
print Animals
结果为
<class '__main__.Animals'>

2.类的数据属性

class Animals(object):
# 类的数据属性
    name = "fentiao"
    age = 12
print Animals
# 访问类的数据属性
print Animals.name
print Animals.age
结果为
<class '__main__.Animals'>
fentiao
12

3.类的方法

class Animals(object):
    name = "fentiao"
    age = 12
    weight = 10
# 类的方法 ==== 函数
# 在类中定义的函数叫做方法 ;
# 类的方法中, python 解释器要求第一个形参必须是 self ;与 java 中的 this 类似;
# self 实质上是类实例化后的对象本身 ;
    def eat(self):
        print "eating......"
        print self
# 类的实例化产生的就是对象; 把抽象的类创造出实际存在的事物;
# object: 对象
fentiao = Animals()
print fentiao
# 调用类的方法
fentiao.eat()
结果为
<__main__.Animals object at 0x2c2f490>
eating......
<__main__.Animals object at 0x2c2f490>

4.面向对象的三大特性: 封装, 继承, 多态

1)封装

封装实际上是把数据封装到某个地方, 以后再去调用被封装在某处的内容或者数据;
封装数据
调用封装数据
通过对象直接调用;
通过self间接调用

**例子:
class Animals(object):

# 构造方法
# 当类实例化时会自动调用 __init__ 构造方法 ;
# name, age, weight 是必选参数,当实例化是必须要传参,否则报错 ;
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
    # eat 方法
    def eat(self):
        print "%s eating......" % (self.name)
        self.weight += 2
    # drink 方法
    def drink(self):
        print "%s is drinking....." % (self.name)
        self.weight += 1
    # 对象可以实例化多个;

fentiao = Animals("fentiao", 5, 12)
tom = Animals("tom", 3, 5)
# self 实质上就是类的实例化对象
print fentiao.name
print fentiao.age
print fentiao.weight
fentiao.eat()
print fentiao.weight
fentiao.drink()
print fentiao.weight
tom.drink()
print tom.weight

结果为
fentiao
5
12
fentiao eating......
14
fentiao is drinking.....
15
tom is drinking.....
6

2)继承

继承
父类和子类; 基类和派生类;
注意: 类的属性名和方法名不同相同;
建议:
属性名用名词;eg:name, age, weight;
方法名建议用动词; eg: eat, drink, get_weight;

**例子:
# Animals 是父类 / 基类 ;
class Animals(object):
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
    def eat(self):
        print "%s eating......" % (self.name)
        self.weight += 2
    def drink(self):
        print "%s is drinking....." % (self.name)
        self.weight += 1
    def get_weight(self):
        pass
# Dog 是 Animal 的子类 / 派生类 ;
class Dog(Animals):
    # 类里面的方法第一个参数必须是 self
    def jiao(self):
        print "%s wang wang ......." % (self.name)
# Cat 是 Animal 的子类 / 派生类 ;
class Cat(Animals):
    def jiao(self):
        print "%s miao miao miao........" % (self.name)
tom = Dog("tom", 12, 10)
tom.eat()
tom.jiao()
结果为
tom eating......
tom wang wang .......


重写父类的构造函数
    父类名.__init__(self,形参)
    super(自己类的名称, self).__init__(形参)
        不需要明确告诉父类的名称;
        如果父类改变,只需修改class语句后面的继承关系即可;

**例子:
# Animals 是父类 / 基类 ;
class Animals(object):
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
    def eat(self):
        print "%s eating......" % (self.name)
        self.weight += 2
# Dog 是 Animal 的子类 / 派生类 ;
class Dog(Animals):
    # name, age, weight, dogid
    def __init__(self, name, age, weight, dogid):
        # 第一种重写父类构造方法 ;
        # self.name = name
        # self.age = age
        # self.weight = weight
        # 第二种重写父类构造方法 :
        # 让 Dog 的父类执行它的 __init__ 方法 ;
        # Animals.__init__(self, name, age, weight)
        # self.dogid = dogid
        # 第三种重写父类构造函数的方法 ;
        super(Dog, self).__init__(name, age, weight)
        self.dogid = dogid
# Cat 是 Animal 的子类 / 派生类 ;
class Cat(Animals):
    # name, age, weight, food
    def __init__(self, name, age, weight, food):
        self.name = name
        self.age = age
        self.weight = weight
        self.food = food
        # Animals.__init__(self, name, age, weight)
        # super(Cat, self).__init__(name, age, weight)
        # self.food = food


tom = Dog("tom", 3, 10, '001')
print tom.dogid
harry = Cat("harry", 2, 5, "fish")
print harry.food
print tom.dogid
结果为
001
fish

新式类和经典类

    python2.x里面支持经典类和新式类;
    python3.x里面仅支持新式类;
# - 经典类
class Book1:
    pass
# - 新式类
class Book2(object):
    pass

多重继承

对于新式类来说, 多重继承的算法是广度优先

class D(object):
    def test(self):
        print "D test"
class C(D):
    pass
    # def test(self):
    #     print "C test"
class B(D):
    pass
    # def test(self):
    #     print "B test"
class A(B,C):
    pass
    # def test(self):
    #     print "A test"
a = D()
a.test()
print A.mro()   ##继承算法查看方式A.mro(),只有新式能用

经典类多继承时, 继承算法是深度优先

class D:
    def test(self):
        print "D test"
class C(D):
    pass
    # def test(self):
    #     print "C test"
class B(D):
    pass
    # def test(self):
    #
        # print "B test"
        # A 继承 B 和 C ;
class A(B,C):
    pass
    # def test(self):
    #
        # print "A test"
# A - B - D
#   - C - D

a = A()
a.test()

3)多态

当父类和子类有相同的方法时, 调用优先执行子类的方法;

**例子
# Animals 是父类 / 基类 ;
class Animals(object):
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight
    def eat(self):
        print "%s eating......" % (self.name)
        self.weight += 2
class Cat(Animals):
    def eat(self):
        print "%s eating......" % (self.name)
        self.weight += 1
class Dog(Animals):
    def eat(self):
        print "%s eating......" % (self.name)
        self.weight += 3
tom = Dog("tom", 12, 12)
tom.eat()
print tom.weight

结果为
tom eating......
15




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页