关注公众号:Python互助小组
一起学习,共同进步
里面有不定期的赠书活动哦
面向对象编程是最有效的软件编写方法之一。在面向对象编程中,你编写表示现实世界中的事物和情景的类,并基于这些类来创建对象。编写类时,你定义一大类对象都有的通用行为。基于类创建对象时,每个对象都自动具备这种通用行为,然后可根据需要赋予每个对象独特的个性。
根据类来创建对象被称为实例化,这让你能够使用类的实例。
什么是类:
用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。
什么是对象:
通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法。
什么是实例化:
创建一个类的实例,类的具体对象。
还有:
class ClassTest():
def __init__(self, name, age):
self.name = name
self.age = age
def introduction(self):
print('大家好,我是' + self.name + ',我今年' + self.age + ' 岁,希望大家好好学习,天天向上')
def study(self):
print(self.name + '正在学习Python')
根据约定,在Python中,首字母大写的名称指的是类。这个类定义中的括号是空的,因为我们要从空白创建这个类。
类中的函数称为方法;你前面学到的有关函数的一切都适用于方法,就目前而言,唯一重要
的差别是调用方法的方式。
__init__
__ init__()是一个特殊的方法,每当你根据Dog类创建新实例时,Python都会自动运行它。
__ init__()定义成了包含三个形参:self、name和age。在这个方法的定义中,形参self必不可少,还必须位于其他形参的前面。Python调用这个__init__()方法来创建Dog实例时,将自动传入实参self。
每个与类相关联的方法调用都自动传递实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。当我们根据类创建实例时,不需要给self赋值
class ClassTest():
def __init__(self, name, age):
self.name = name
self.age = age
def introduction(self):
print('大家好,我是' + self.name + ',我今年' + self.age + ' 岁,希望大家好好学习,天天向上')
def study(self):
print(self.name + '正在学习Python')
test = ClassTest('小组长', 24)
print('大家好,我是' + test.name + ',我今年' + test.age + ' 岁,希望大家好好学习,天天向上')
print(self.name + '正在学习Python')
最后的运行结果会是什么呢?
正确的代码是怎么样的呢?好记性不如烂笔头。写写吧
class ClassTest():
def __init__(self, name, age):
self.name = name
self.age = age
def introduction(self):
print('大家好,我是', self.name, ',我今年', self.age, '岁,希望大家好好学习,天天向上')
def study(self):
print(self.name + '正在学习Python')
test = ClassTest('小组长', 24)
test1 = ClassTest('小组长', 25)
print('大家好,我是', test.name, ',我今年', test.age, '岁,希望大家好好学习,天天向上')
print(test.name + '正在学习Python')
那我们调用里面的方法是不是也就很简单了呢?
class ClassTest():
def __init__(self, name, age):
self.name = name
self.age = age
def introduction(self):
print('大家好,我是', self.name, ',我今年', self.age, '岁,希望大家好好学习,天天向上')
def study(self):
print(self.name + '正在学习Python')
test = ClassTest('小组长', 24)
print('大家好,我是', test.name, ',我今年', test.age, '岁,希望大家好好学习,天天向上')
print(test.name + '正在学习Python')
test.introduction()
test.study()
class ClassTest():
def __init__(self, name, age):
self.name = name
self.age = age
def introduction(self):
print('大家好,我是', self.name, ',我今年', self.age, '岁,希望大家好好学习,天天向上')
def study(self):
print(self.name + '正在学习Python')
test = ClassTest('小组长', 24)
test = ClassTest('小组长', 25)
print('大家好,我是', test.name, ',我今年', test.age, '岁,希望大家好好学习,天天向上')
print(test.name + '正在学习Python')
test.introduction()
test.study()
会输出什么?
如果想要创建多个对象正确的示例是什么?
class ClassTest():
def __init__(self, name, age):
self.name = name
self.age = age
def introduction(self):
print('大家好,我是', self.name, ',我今年', self.age, '岁,希望大家好好学习,天天向上')
def study(self):
print(self.name + '正在学习Python')
test = ClassTest('小组长', 24)
test1 = ClassTest('小组长', 25)
print('大家好,我是', test.name, ',我今年', test.age, '岁,希望大家好好学习,天天向上')
print(test.name + '正在学习Python')
test.introduction()
test.study()
print('大家好,我是', test1.name, ',我今年', test1.age, '岁,希望大家好好学习,天天向上')
print(test1.name + '正在学习Python')
test1.introduction()
test1.study()
关注公众号:Python互助小组
一起学习,共同进步
里面有不定期的赠书活动哦