python——课堂笔记🍀

32 阅读1分钟
class :
      名称 = "python程序设计"
      出版社 = "人民邮电出版社"
      编著 = "黑马程序员"
# 方法
      def open(self):
          self.版次 = "第三版"
          print("打开")

# 1、实例化
book = 书()
# 2、通过类和对象进行访问 名称
print(书.名称)
print(book.名称)
# 3、通过类和对象对 编著 进行修改,修改后访问
书.名称 = "chen"
print(书.名称)
print(book.名称)
book.名称 = "chen"
print(书.名称)
print(book.名称)
# 4、通过对象对 实例属性 版次 进行访、修改
book.open()
print(book.版次)
book.版次 = "第二版"
print(book.版次)
# 5、 动态添加实例属性
book.地址 = "北京"
print(book.地址)

# class Car:
#     wheels = 4
#
# car = Car()
# print(Car.wheels)
# print(Car.wheels)
# Car.wheels = 3
# print(Car.wheels)
# print(Car.wheels)
# Car.wheels = 2
# print(Car.wheels)
# print(Car.wheels)

class Car:
    def __init__(self,color):
        self.color = color
    def drive(self):
        print(f"车的颜色为:{self.color}")
car_one = Car("红色")
car_one.drive()
car_two = Car("黑色")
car_two.drive()


class :
    名称 = "python程序设计"
    出版社 = "人民邮电出版社"
    编著 = "黑马程序员"

    # 方法
    def open(self):
        self.版次 = "第三版"
        print("打开")

# 1、将类属性改为构造方法里面的参数,全部是要传参的,实例化转参
class :
    def __init__(self,名称,出版社,编著):
        self.名称 = 名称
        self.出版社 = 出版社
        self.编著 = 编著
    def open(self):
            self.版次 = "第三版"
            print(f"名称为{self.名称}")
            print(f"出版社为{self.出版社}")
            print(f"编著为{self.编著}")
            print("打开")
            
book = 书(名称="Python程序设计",出版社="人民邮电出版社",编著="黑马程序员")
        
# 2、在open方法里面打印 名称、出版社、编著
# 3、调用open()