Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
aabbccgithub
Python24
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python24
/
00Python
/
day09
/
basic04.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
59 lines (46 loc) · 1.78 KB
master
Breadcrumbs
Python24
/
00Python
/
day09
/
basic04.py
Copy path
Top
File metadata and controls
Code
Blame
59 lines (46 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
python中的类和对象, 所有的类的祖先都是object和java是一样的
1- 前两种叫做经典类存在python 2.x中, 最后一种叫做新式类
2- python 2中类并没有父类,自己就是父类
3- 而python3中得class Person(object)存在父类,可以使用根类的方法
"""
class
Person
:
pass
class
Person
():
pass
class
Person
(
object
):
pass
"""
1- 实例方法的第一个形参是self自身,表示调用该方法的对象,谁调用谁就是self
2- 给一个**对象**添加属性,并不是添加到类中的,有点奇怪
3- 类的方法中,通过self获取对象的属性self.name
4- 重写__init__() 用来给属性添加默认值,避免多个对象对共同的属性name赋值
5- python中不能定义多个构造方法,只能定义一个,不然会报错
6- 因为python对参数的类型没有限制,构造器中参数一般为字典{}用来处理不同数量参数的问题
"""
class
Hero
(
object
):
# python提供的两个下划线的魔法方法,创建该类对象的时候自动调用,类似于构造方法
# def __init__(self):
# self.name = "旺财"
def
__init__
(
self
,
age
,
my_name
=
"默认的名字"
):
# 构造方法的参数也和kotlin一样可以默认参数
self
.
name
=
my_name
self
.
age
=
age
def
move
(
self
):
print
(
id
(
self
))
print
(
"英雄会行走..."
)
def
print_info
(
self
):
print
(
self
.
name
)
print
(
self
.
age
)
print
(
self
.
hp
)
hero01
=
Hero
(
22
)
print
(
id
(
hero01
))
# 打印的id和move方法中的self的id是同一个地址
hero01
.
move
()
hero01
.
name
=
"英雄名字01"
hero01
.
age
=
22
hero01
.
hp
=
4000
hero01
.
print_info
()
hero02
=
Hero
(
22
)
print
(
hero02
.
name
)
hero03
=
Hero
(
18
)
print
(
hero03
.
name
)
print
(
hero03
.
age
)
You can’t perform that action at this time.