class Book:
name = "biology" #Class Attribute
#1.instantiation
book = Book()#target
#2.Access the name through classes and objects
print(Book.name)
print(book.name)
#3.Modify via classes and objects, then access after modification.
Book.name = "organism"
print(Book.name)
print(book.name)
book.name = "study"
print(Book.name)
print(book.name)
book.year = "2025"
print(book.year)
#4.Access and modify the instance attributes through the object
book.year = "2026"
print(book.year)
#5.Dynamically add instance attributes
book.author = "Norton"
book.price = 59.9
print(book.__dict__)
```python
class Book:
# 1.Modify the class attributes to be parameters of the constructor method, and pass the parameters during instantiation.
def __init__(self, name, publisher, author):
self.name = name # Instance attribute: Name
self.publisher = publisher # Instance attribute: Publisher
self.author = author # Instance attribute: Compiler/Author
# 2. Print the name, publisher, and author/compiler in the open method.
def open(self):
print(f"name:{self.name}")
print(f"publisher:{self.publisher}")
print(f"author:{self.author}")
# 3. Instantiate (pass parameters) and call open().
book = Book(name="biology", publisher="Science Press", author="Norton")
book.open()
结果如下: