類的相關

類的靜態屬性(實例變量)html

類的動態屬性(類的功能方法)python

類變量:先找實例變量,再找類變量。不能經過實例變量修改類變量,無實例變量有類變量是個實例私有化屬性的過程。linux

self傳入的是實例名app

構造函數:def __init(self,name,age):....ide

析構函數:def __del__(self):....函數

私有屬性:__life ,則不能經過實例名.__life訪問工具

私有方法:__die() ,則不能經過實例名.__life()調用ui

子類重構時必須先重寫父類的參數,新式類與經典類:spa

多繼承是從左到右的繼承父類(的構造函數)code

# class People:  # 經典類
class People(object):  # 新式類
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.friends = []

    def eat(self):
        print('%s is eating' % self.name)

    def sleep(self):
        print('%s is sleeping ' % self.name)


class Relation(object):
    def makefriends(self, obj):
        print('%s is making friend with %s' % (self.name, obj.name))
        self.friends.append(obj.name)


class Man(People, Relation):
    # def __init__(self, name, age, money):
    #     People.__init__(self, name, age)  # 經典類
    #     Relation.__init__(self)  # 經典類
    #     super(Man, self).__init__(name, age) # 新式類
    #     self.money = money

    # def get_money(self):
    #     print('%s was born with %s money' % (self.name, self.money))
    pass


class Women(People, Relation):
    pass


# m1 = Man('chenronghua', 22, 10)
m1 = Man('chenronghu', 22)
# m1.get_money()
w1 = Women('niuhangyang', 20)

m1.makefriends(w1)
w1.name = '三炮'
print(m1.friends[0])
View Code

 py2經典類的繼承方式是深度優先,py2中新式類繼承方式是廣度優先

py3中經典類和新式類都是廣度優先來繼承

小練手

# -*- coding: utf-8 -*-
class School(object):
    def __init__(self, name, addr):
        self.name = name
        self.addr = addr
        self.students = []
        self.staffs = []

    def enroll_stu(self, stu_obj):
        print('給學員%s辦理註冊!' % stu_obj.name)
        self.students.append(stu_obj)

    def hire_teacher(self, tea_obj):
        self.staffs.append(tea_obj)
        print('僱傭新講師%s' % tea_obj.name)


class SchoolMenber(object):
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    def tell(self):
        pass


class Teacher(SchoolMenber):
    def __init__(self, name, age, sex, salary, course):
        super(Teacher, self).__init__(name, age, sex)
        self.salary = salary
        self.course = course

    def tell(self):
        print('''
        ------ info of Teacher : %s -----
        Name : %s 
        Age : %s 
        Sex : %s
        Salary : %s 
        Course : %s
        ''' % (self.name, self.name, self.age, self.sex, self.salary, self.course))

    def teach(self):
        print('%s is teaching course [%s]' % (self.name, self.course))


class Student(SchoolMenber):
    def __init__(self, name, age, sex, stu_id, grade):
        super(Student, self).__init__(name, age, sex)
        self.stu_id = stu_id
        self.grade = grade

    def tell(self):
        print('''
        -------  info of student :%s ------
        Name : %s
        Age : %s
        Sex : %s
        stu_id : %s
        grade : %s 
        ''' % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))

    def pay_tuition(self, money):
        print('%s is paying %s money' % (self.name, money))


sch1 = School('old boy ', '沙河')
t1 = Teacher('alex_py', 22, 'F', 10000, 'python')
t2 = Teacher('alex_linux', 32, 'MF', 3000, 'linux')
s1 = Student('chenronghua', '16', 'F', '00001', 'py')
s2 = Student('xuliangwei', '19', 'M', '00002', 'go')
t1.tell()
s1.tell()
sch1.hire_teacher(t1)
sch1.enroll_stu(s1)
sch1.enroll_stu(s2)
print(sch1.students)
print(sch1.staffs)
sch1.staffs[0].teach()

for stu in sch1.students:
    stu.pay_tuition(2000)
View Code

統計實例了多少個實例對象

# -*- coding: utf-8 -*-
class Stu(object):
    count = 0

    def __init__(self, name, age):
        self.name = name
        self.age = age
        Stu.count += 1


s1 = Stu('alex', 24)
s2 = Stu('eric', 22)
print(s1.count)
print(s2.count)
print(Stu.count)
print(s1.__dict__)  # 沒有s1本身的屬性,是類的屬性
print(s2.__dict__)
View Code

 多態性(一種接口,多種實現)

# -*- coding: utf-8 -*-
import abc


class Animal(metaclass=abc.ABCMeta):  # 同一類事物:動物
    @abc.abstractmethod
    def talk(self):
        pass


class People(Animal):  # 動物的形態之一:人
    def talk(self):
        print('say hello')


class Dog(Animal):  # 動物的形態之二:狗
    def talk(self):
        print('say wangwang')


class Pig(Animal):  # 動物的形態之三:豬
    def talk(self):
        print('say aoao')


def func(Animal):
    Animal.talk()


per1 = People()
pig1 = Pig()
d1 = Dog()
func(per1)
func(pig1)
func(d1)
View Code

鴨子類型,看着像,有差很少像的屬性,差很少的使用方法,那就是同一種東東

 

property的使用:把某個帶函數值的方法變成了類的屬性

# -*- coding: utf-8 -*-
class People(object):
    def __init__(self, name, weight, height):
        self.name = name
        self.weight = weight
        self.height = height

    @property
    def bmi(self):
        return self.weight / (self.height ** 2)


p1 = People('alex', 75, 1.81)
print(p1.name)
print(p1.bmi)
View Code

 假裝屬性的修改和刪除

# -*- coding: utf-8 -*-
class People(object):
    def __init__(self, name):
        self.__name = name

    @property
    def name(self):
        print('getter')
        return self.__name

    @name.setter
    def name(self, val):
        print('setter')
        if not isinstance(val, str):
            print('名字必須是字符串類型')
            return
        self.__name = val

    @name.deleter
    def name(self):
        print('deleter')
        print('不容許刪除!')
View Code

類的綁定方法與非綁定方法

# -*- coding: utf-8 -*-

'''
在類內部定義的函數,分爲兩大類:
    一:綁定方法:綁定給誰,就應該由誰來調用,誰來調用就回把調用者看成第一個參數自動傳入
        綁定到對象的方法:在類內定義的沒有被任何裝飾器修飾的

        綁定到類的方法:在類內定義的被裝飾器classmethod修飾的方法

    二:非綁定方法:沒有自動傳值這麼一說了,就類中定義的一個普通工具,對象和類均可以使用
        非綁定方法:不與類或者對象綁定
        在類的內部定義了一個跟類無關的函數
'''


class Foo:
    def __init__(self, name):
        self.name = name

    def tell(self):
        print('名字是%s' % self.name)

    @classmethod
    def func(cls):  # cls=Foo
        print(cls)

    @staticmethod
    def func1(x, y):
        print(x + y)


f = Foo('egon')
print(Foo.tell)
Foo.tell(f)  # 類用這個方法的話,必須把實例名傳入
print(f.tell)
f.tell()  # tell就是綁定到對象的方法讓實例去用的

# print(Foo.func)
# Foo.func()

# print(Foo.func1)
# print(f.func1)
View Code

 內置方法
https://www.luffycity.com/python-book/di-5-zhang-mian-xiang-dui-xiang-bian-cheng-she-ji-yu-kai-fa/512-mian-xiang-dui-xiang-shi-zhan.html

 

關於一些題目:

https://www.cnblogs.com/huang-yc/p/9012822.html

相關文章
相關標籤/搜索