-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstruct.py
More file actions
38 lines (29 loc) · 1011 Bytes
/
struct.py
File metadata and controls
38 lines (29 loc) · 1011 Bytes
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
"""
Copyright © https://github.com/microwind All rights reserved.
@author: jarryli@gmail.com
@version: 1.0
@description: 结构体数据结构 - Python实现
"""
# Person 类:姓名、年龄、地址
class Person:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
def introduce(self):
print(f"Hi, I am {self.name}, {self.age} years old, from {self.address}.")
# Employee 类:继承 Person,增加职位
class Employee(Person):
def __init__(self, name, age, address, position):
super().__init__(name, age, address)
self.position = position
def introduce(self):
print(f"I am {self.name}, a {self.position} at the company, living in {self.address}.")
p1 = Person("Alice", 30, "123 Main St")
p1.introduce()
e1 = Employee("Bob", 28, "456 Elm St", "Software Developer")
e1.introduce()
"""
jarry@MacBook-Pro struct % python struct.py
Hi, I am Alice, 30 years old, from 123 Main St.
"""