美文网首页
模块化编程2

模块化编程2

作者: 多啦A梦的时光机_648d | 来源:发表于2020-04-09 13:54 被阅读0次

孟德尔遗传定律

class Pea:
    def __init__(self,genotype):   ##初始化实例对象
        self.genotype = genotype   ##将实例化对象参数赋值给内部属性
    def get_phenotype(self):  ##创建一个方法(或者叫函数)
        if 'G' in self.genotype:  ##self.genotype调用的是内部属性
            return 'yellow'
        else:
            return 'green'
    def create_offspring(self,other):  ##创建一个方法,包含一个外部赋值(为green)
        offspring = []
        new_genotype = ''
        for haplo1 in self.genotype:   ##self.genotype调用的是内部属性
            for haplo2 in other.genotype:  
                new_genotype = haplo1 + haplo2
                offspring.append(Pea(new_genotype))
        return offspring
    def __repr__(self):
        return self.get_phenotype() + ' [%s] ' % self.genotype
yellow = Pea('GG')   ##实例化对象
green = Pea('gg')
f1 = yellow.create_offspring(green)
print(f1)

1.创建类

类定义了其所代表事物的特征和行为,但不包含特定的数据,当在这一类中赋予特定的具体数据时则称之为实例。使用class来创建一个新类,class之后为类的名称并以冒号结尾。下面一整个缩进的代码块都属于该类。

2.构造函数init

构造函数init(注意init前后各有两条下划线)具有初始化的作用,也就是当该类被实例化的时候就会执行该函数。我们可以把要先初始化的属性放到这个函数里面。

class Pea:
    def __init__(self, genotype):
        self.genotype = genotype

比如,通过赋值self.genotype = genotype使每个豌豆都有其特定的基因型(也可以称之为属性),init方法的第一个参数永远是self,表示创建的类实例本身,因此,在init方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。

这里self就是指类本身,self.genotype就是Pea类的属性变量,是Pea类所有。而genotype是外部传来的参数,不是Pea类所自带的。所以self.genotype = genotype的意思就是把外部传来的参数genotype的值赋值给Pea类的属性变量self.genotype。

3.创建实例(实例化对象)

yellow = Pea("GG")
print(yellow.genotype)

GG

创建实例时与调用函数类似,将构造函数所需要的参数genotype作为参数传递。self只是告诉类,自己由哪个实例调用。本例中yellow是调用Pea类的实例(变量),GG则为yellow实例中genotype的具体值。

4.类以属性的形式包含数据

实例内的数据储存在属性中。可以通过.来读取(如yellow.genotype),属性也可以像变量一样动态地改变,例如重新赋值更新yellow.genotype = 'Gg'

5.类包含的方法

类中的函数称之为方法,在Pea类中包含了计算表型的方法:

    def get_phenotype(self):
        if 'G' in self.genotype:
            return 'yellow'
        else:
            return 'green'

get_phenotype()方法使用了genotype属性中的数据,self则指实例本身。

6.repr方法打印类和实例

打印从类中创建的对象时,Python通常会显示这样的信息:

<__main__.Pea object at 0x00846570>

这里给出的信息很不充分,这时候就可以在类中添加一种称为repr()的特殊方法来打印对象:

    def __repr__(self):
        return self.get_phenotype() + ' [%s] ' % self.genotype

[yellow [Gg] , yellow [Gg] , yellow [Gg] , yellow [Gg] ]

上面的方法会返回一个包含实例基因型和表型的字符串。除self参数以外repr()方法不需要其他参数。打印Pea实例时,repr()会被自动调用。编写repr()方法时,只需要包括你认为必要的信息即可。

7.创建子类

一个类的属性和方法可以由其他类继承。被继承的类称为父类,继承的类称为子类。比如想要豌豆包含注释,则可以定义一个从Pea类继承的CommentedPea类:

class CommentedPea(Pea):

    def __init__(self, genotype, comment):
        Pea.__init__(self, genotype)  ##__init__()调用需要的父类属性和方法
        self.comment = comment

    def __repr__(self):
        return '%s [%s] (%s)' % (self.get_phenotype(),
        self.genotype, self.comment)

yellow1 = CommentedPea('GG', 'homozygote')
yellow2 = CommentedPea('Gg', 'heterozygote')
print(yellow1)

GG [GG] homozygote

除了新加入的,CommentedPea类与Pea类的方法和属性一样,注意init()方法用于调用父方法。

或者用super()方法

class CommentedPea(Pea):
    def __init__(self,genotype,comment):
#        Pea.__init__(self,genotype)
        super().__init__(genotype)   ##super()方法继承父类的属性方法
        self.comment =comment

相关文章

网友评论

      本文标题:模块化编程2

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