码迷,mamicode.com
首页 > 编程语言 > 详细

【python】初识python

时间:2017-04-13 09:05:50      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:name   div   class   bin   tin   继承   self   odi   int   

【类1:类】

#!/usr/bin/python
# -*- coding:UTF-8 -*-

class Dog():
    #construct
    #类变量可以不用显式定义在类的头部 但是最好不要这样做
    #类变量不显式区分public 和 private, 隐式规则:变量前缀是__,则表示该变量是private变量,否则为public
    def __init__(self, name, age):
        self.name = name
        self.age = age 
        
    def sit(self):
        print(self.name.title() + " is now sitting")

    def roll_over(self):
        print(self.name.title() + " rolled over")


dog = Dog("dabai", 10) 
dog.sit()
dog.roll_over();
print dog.name

【类2:继承】

#!/usr/bin/python
# -*- coding:UTF-8 -*-

class Animal():
    def __init__(self, animal_type, name):
        self.animal_type = animal_type;
        self.name = name;

class Dog(Animal):
    def __init__(self, name, age):
        self.name = name
        #python2的语法
        #python3的语法: super().__init__(param1, param2, ...)
        Animal.__init__(self, "reptiles", name)
        self.age = age 
        self.animal_type = "reptiles"
    
    def sit(self):
        print(self.name.title() + " is now sitting")

    def roll_over(self):
        print(self.name.title() + " rolled over")

dog = Dog("dabai", 10)
dog.sit()
dog.roll_over();
print dog.name
print dog.animal_type

 

【python】初识python

标签:name   div   class   bin   tin   继承   self   odi   int   

原文地址:http://www.cnblogs.com/helww/p/6702142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!