您的位置:首页 > 编程语言 > Python开发

python类

2020-02-02 20:42 344 查看

 

类,面向对象一个很重要的载体。类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同。

类的定义 类的定义是通过关键字class,class后面紧跟着的是类名,通常是大写开头,紧跟着的是(object),表示该类是从哪个类继承下来的,如果没有合适的继承类,就用object,它是所有类最终都会继承的类。
class Student(object):
pass
类里面一般都是由很多函数组成,函数的第一个参数默认都是self。 如果需要全局变量就在类的内部直接定义。 类的内部在调用函数或者调用变量的时候,必须self.函数名或者self.变量名,self代表类实例化以后的个体。
class Student(object):
name = "yangjian"
def hello(self):
print("my name is {0}".format(self.name))

 

类的实例化 实例化类的首字母小写作为实例,然后类实例化。 student = Student()
class Student(object):
name = "yangjian"
def hello(self):
print("my name is {0}".format(self.name))
student = Student()

结果:
hello yangjian

 

构造器 类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制写进去。通过一个__init__()方法,在创建实例的时候就把name,score等属性绑定上去。
class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score
student = Student("yangjian",90)
__init__的第一个参数永远是self,表示创建的实例本身,在__init__()方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。为sel绑定了属性之后,在创建实例的时候就不能传入空的参数了,需要传入与__init__()方法相匹配的参数。 构造器,就是类在初始化的时候,首先必须要执行的函数。
class A(object):
def __init__(self,name):
self.name = name
print("init calss A")
def hello(self):
print("hello {0}".format(self.name))
a = A("yangjian")
a.hello()
# 结果
init calss A # 初始化的时候执行的
hello yangjian

 

数据封装 在上面的Student类中,每个实例都拥有各自的name和score,可以通过函数来访问这些数据,如打印一个学生的姓名和成绩。既然实例本身就拥有这些数据,我们可以直接在Student类的内部定义访问数据的函数,这样就把数据封装起来了,我们称这样的函数为类的方法。
class Student(object):

def __init__(self, name, score):
self.name = name
self.score = score

def print_score(self):
print('%s: %s' % (self.name, self.score))
student = Student("yangjian",90)
student.print_score()
结果:
yangjian:90

 

继承 我们在定义一个class的时候,可以从某个现有的class继承,新的class成为子类,被继承的类称为父类。
class Animal(object):
def __init__(self):
print("你现在正在初始化一个Animal")
def run(self):
print("Animal can run.")
class Bird(Animal):
def fly(self):
print("Bird can fly.")
class Cat(Animal):
def jiao(self):
print("miao miao miao.")
#animal = Animal()
cat = Cat()
# 结果
你现在正在初始化一个Animal

在上面这个例子中,在实例化Cat这个类的时候,Cat类继承了Animal这个类,所以Cat类会拥有Animal类的所有属性和方法,所以出现的结果是Animal类里面的初始化函数的内容。

 

重写

在子类中,对父类中的发放进行重写。

class Animal(object):
def __init__(self):
print("你现在正在初始化一个Animal.")
def run(self):
print("Animal can run.")
class Bird(Animal):
def fly(self):
print("Bird can fly.")
class Cat(Animal):
def __init__(self):
print("我是一只猫.")
def jiao(self):
print("miao miao miao.")
animal = Animal()
cat = Cat()
# 结果
你现在正在初始化一个Animal
我是一只猫.

上面这个例子,实例化Cat的时候,先去子类Cat类里面去找初始化函数,找到了就是用Cat类自己的初始化函数,如果Cat类里面没有定义初始化函数,就去父类里面去找。父类中有一个初始化函数和一个run方法,子类对继承自父类的方法进行了重写,于是就有了自己的初始化方法和一个jiao方法。

 

多继承 一个类可以继承多个类。
class Animal(object):
def __init__(self):
print("你现在正在初始化一个Animal")
def run(self):
print("Animal can run.")
class Bird(Animal):
def __init__(self):
print("我是一只鸟.")
def fly(self):
print("Bird can fly.")
class Cat(Animal):
def __init__(self):
print("我是一只猫.")
def jiao(self):
print("miao miao miao.")
class Bianyi(Bird,Cat):  # 多继承,如果父类中都有该方法,那么先继承谁就用谁的方法,即Bird写在前面就先继承Bird
pass
animal = Animal()
cat = Cat()
binyi = Bianyi()

# 结果
你现在正在初始化一个Animal
我是一只猫.
我是一只鸟.

 

私有变量 有时候我们不希望类里面的变量被外界随便访问,只需要在变量前面加上两个下划线就可以了。
class DbArgs(object):
# 只有类本身才可以调用
__host = str("1.1.1.1")
__port = str()
__username = str()
__password = str()
__dbname = str()

# 任何人可以调用
name = "ajing"

# 只能实例自己调用
_host = "asdlfjasdl"
def getHost(self):
return self.__host

def setHost(self, host):
self.__host = host

dbArgs = DbArgs()
print(dbArgs.getHost())  # 类调用getHost()方法
dbArgs.name = "就是要改你,怎么的"  # 这个name不是私有变量,谁都可以调用
print(dbArgs.name)
print(dbArgs._host) # 实例直接调用

结果:
1.1.1.1
就是要改你,怎么的
asdlfjasdl

 

练习

把之前的求阶乘封装成类

class JinCinCount(object):
def __init__(self, n):
self.n = n
def jc(self, n):
result = 1
if n == 0:
return result
else:
for i in range(1, n+1):
result *= i
return result
def count(self):
count = 0
for i in range(0, int(self.n) + 1):
count += self.jc(i)
print("count = {0}".format(count))

def main():
n = input("Please inpurt a number: ")
# 实例化类
jinCinCount = JinCinCount(int(n))
# 调用类方法
jinCinCount.count()

if __name__ == "__main__":
main()

 

转载于:https://www.cnblogs.com/yangjian319/p/8893551.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
aeyfq26848 发布了0 篇原创文章 · 获赞 0 · 访问量 49 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: