python3xiaobaike_2020/chapter7/7-3 python3小白课:了解实例方法.md
2025-04-20 23:22:14 +08:00

26 lines
951 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# python3小白课了解实例方法
首先我们来看一下例子:
```python
# coding:utf-8
class Student:
"""这个地方可以定义类的帮助文档"""
# 定义类变量
sex = "男男男"
# 定义构造函数
def __init__(self, name, age=18, sex="男"):
# 实例的变量,区别于类变量
self.name = name
self.age = age
self.sex = sex
# 定义一个其他方法
def info(self):
print(Student.sex)
print("姓名:{},年龄:{},性别:{}".format(self.name, self.age, self.sex))
s = Student("小白")
# 调用实例方法
s.info()
```
如上代码我们可以看到在类之中我们可以定义一些这个类的实例能做的一些事情比如这个info方法就是每一个实例拥有的方法实例创建之后就可以调用这个实例的实例方法了你也可以定义实现一些其他的事情是不是很简单呢