python面向对象基础

类变量:所有对象公用的属性

析构函数:

在实例释放、销毁的时候执行的,通常用于做一些收尾函数,比如关文件

私有属性:

在属性名前加两个下划线就是私有属性了

私有方法:

在方法名前加两个下划线就是私有方法了

继承

多继承当中,py2经典类是按深度优先来继承的,新式类是按广度优先来继承的

py3 经典类和新式类都是同一按广度优先来继承的

# Author:dancheng
#class People:    经典类
class People(object):    #新式类
    def __init__(self, name, age):
        self.name = name
        self.age = age

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

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

    def talk(self):
        print("%s is talking..." % self.name)

class Relation(object):
    def __init__(self, n1, n2):
        print('init in relation')
    def mak_friends(self, p1):
        print("%s is making friends with %s" %(self.name, p1.name))

class Man(People, Relation):
    def __init__(self, name, age, money):
        #People.__init__(self, name, age)
        super(Man, self).__init__(name, age)
        self.money = money
        print('%s 一出生2就有钱了%s money' %(self.name, self.money))

    def piao(self):
        print('%s is piaoing ..... 20s ' % self.name)

    def sleep(self):
        People.sleep(self)
        print('man is sleeping')

class Woman(People):
    def get_birth(self):
        print('%s is born a baby...' % self.name)

    def sleep(self):
        print('a Woman is sleeping....')

w1 = Woman('dan', 11)
# w1.get_birth()
# w1.sleep()

m1 = Man('dancheng', 12, 10)
# m1.eat()
# m1.piao()
# m1.sleep()
m1.mak_friends(w1)


继承实例讲解:

描述一个学校讲师和学员的关系

# Author:dancheng
class School(object):
    def __init__(self, name, addr):
        self.name = name
        self.addr = addr
        self.students = []
        self.teachers = []
    def enroll(self, stu_obj):
        print('为学员%s 办理注册手续' %stu_obj.name)
        self.students.append(stu_obj)
    def hire(self, staff_obj):
        print('为老师%s 办理入学手续' % staff_obj.name)
        self.teachers.append(staff_obj)

class SchoolMember(object):
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
    def tell(self):
        pass

class Teacher(SchoolMember):
    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 teachering course [%s]' % (self.name, self.course))

class Student(SchoolMember):
    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, amount):
        print('%s has paid tution for $%s' %(self.name, amount))

school = School('dancheng', '吉林建筑大学')
t1 = Teacher('dancheng', 21, 'F', 10000000, 'web')
t2 = Teacher('xinyu', 22, 'M', 100000, 'java')

s1 = Student('baolei', 22, 'F', 100, 'python')
s2 = Student('sun', 10, 'M', 10002, 'python')

#t1.tell()
#t2.tell()
school.hire(t1)
school.enroll(s1)
school.enroll(s2)
school.teachers[0].teach()

for stu in school.students:
    stu.pay_tuition(4000)

多态

一种接口,多种实现。

允许将子类类型的指针赋值给父类类型的指针

# Author:dancheng
class Animal(object):
    def __init__(self, name):  # Constructor of the class
        self.name = name
    def talk(self):  # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")

class Cat(Animal):
    def talk(self):
        print('%s: 喵喵喵!' % self.name)

class Dog(Animal):
    def talk(self):
        print('%s: 汪!汪!汪!' % self.name)

def func(obj):  # 一个接口,多种形态
    obj.talk()

c1 = Cat('小晴')
d1 = Dog('李磊')

func(c1)
func(d1)

静态方法

只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性

class Dog(object):
    def __init__(self, name):
        self.name = name
    @staticmethod   #实际上和类没有什么关系,用类名调用
    def eat():
        print('%s is eating' %('ss'))

d = Dog("hhh")
d.eat()
Dog.eat()

类方法

只能访问类变量,不能访问实例变量

# Author:dancheng
class Dog(object):
    name = 'dancheng'
    @classmethod
    def eat(self):
        print('%s is eating' %(self.name))
d = Dog()
d.eat()

属性方法

把一个方法变成一个静态属性

1

# Author:dancheng
class Dog(object):
    name = 'dancheng'
    @property        #attribute
    def eat(self):
        print('%s is eating' %(self.name))

d = Dog()
d.eat

2

# Author:dancheng
class Dog(object):
    def __init__(self, name):
        self.name = name
        self.__food = None

    @property        #attribute
    def eat(self, food):
        print('%s is eating %s' %(self.name, food))

    #定义一个属性
    @property  # attribute
    def eat(self):
        print("%s is eating %s" % (self.name, self.__food))
    #修改属性
    @eat.setter
    def eat(self, food):
        print("set to food:", food)
        self.__food = food
    #删除属性
    @eat.deleter
    def eat(self):
        del self.__food
        print("删完了")

d = Dog('dog')
d.eat = '包子'
del d.eat










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值