Python_day_07_Object-Oriented Programming

Process
Oriented: Function Oriented:
Object Oriented:

1. Define a class

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

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

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

2. Data properties of the class

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

3. Class methods

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. The three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

1) Package

Encapsulation actually encapsulates data somewhere, and then calls the content or data encapsulated somewhere;
encapsulates data
calls encapsulated data
directly through the object;
indirectly through 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) Inheritance

Inheritance
parent class and subclass; base class and derived class;
note: the attribute name and method name of the class are different and the same;
suggestion:
noun for attribute name; eg: name, age, weight;
verb for method name; 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

New and Classic Classes

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

multiple inheritance

For new-style classes, the multiple inheritance algorithm is breadth-first

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(),只有新式能用

In the case of multiple inheritance of classic classes, the inheritance algorithm is depth-first

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) Polymorphism

When the parent class and the child class have the same method, the method of the child class is called first;

**例子
# 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




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326042476&siteId=291194637