python创建类和使用
#创建类 class起头
'''class Dog():
def __init__(self,name,age):
self.age = age
self.name = name
def sit(self):
print(f"{self.name} is now sitting.")
def roll_over(self):
print(f"{self.name} rolled over!")
my_dog = Dog('a',6)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
b=my_dog.name
print(b)
#使用方法 my_dog.sit()#查看方法输出结果,即上面方法里两个print的结果 my_dog.roll_over()’’’
#创建多个实例
'''class Dog():
def __init__(self,name,age):
self.age = age
self.name = name
def sit(self):
print(f"{self.name} is now sitting.")
def roll_over(self):
print(f"{self.name} rolled over!")
my_dog = Dog('a',6)
your_dog=Dog('b',3)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
my_dog.sit()
print(f"\nYour dog's name is {your_dog.name}.")
print(f"Your dog's name is {your_dog.age} years old.")
your_dog.sit()'''
'''class Car:
def __init__(self,make,model,year):
self.make=make#初始化属性
self.model=model
self.year=year
def get_descriptive_name(self):
long_name=f"{self.year} {self.make} {self.model}"
return long_name.title()
my_new_car=Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())'''
'''class Car:
def __init__(self,make,model,year):
self.make=make#初始化属性
self.model=model
self.year=year
self.odometer_reading=0#初始里程为0
def get_descriptive_name(self):
long_name=f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"this car has {self.odometer_reading} miles on it")
my_new_car=Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())
#my_new_car.odometer_reading=23 直接修改属性的值
my_new_car.read_odometer()'''
#通过方法修改属性的值
'''class Car:
def __init__(self,make,model,year):
self.make=make#初始化属性
self.model=model
self.year=year
self.odometer_reading=0#初始里程为0
def get_descriptive_name(self):
long_name=f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"this car has {self.odometer_reading} miles on it")
def update_odometer(self,mileage):#新定义方法,mileage为形参,self不可少
self.odometer_reading=mileage
my_new_car=Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23)
my_new_car.read_odometer()'''
#通过方法对属性值进行递增
'''class Car:
def __init__(self,make,model,year):
self.make=make#初始化属性
self.model=model
self.year=year
self.odometer_reading=0#初始里程为0
def get_descriptive_name(self):
long_name=f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"this car has {self.odometer_reading} miles on it")
def update_odometer(self,mileage):#新定义方法,mileage为形参,self不可少
self.odometer_reading=mileage
def increment_odometer(self,miles):
self.odometer_reading+=miles
my_new_car=Car('subaru','outback',2019)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23500)
my_new_car.read_odometer()
my_new_car.increment_odometer(100)
my_new_car.read_odometer()'''
'''class Car:
def __init__(self,make,model,year):
self.make=make#初始化属性
self.model=model
self.year=year
self.odometer_reading=0#初始里程为0
def get_descriptive_name(self):
long_name=f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"this car has {self.odometer_reading} miles on it")
def update_odometer(self,mileage):#新定义方法,mileage为形参,self不可少
self.odometer_reading=mileage
def increment_odometer(self,miles):
self.odometer_reading+=miles
class ElectricCar(Car):#子类electriccar,父类car,注意car的代码前面必须写出
#继承后属性初始化可以省略,使用super()
def __init__(self,make,model,year):
#初始化父类属性
super().__init__(make,model,year)
my_tesla=ElectricCar('tesla','s','2019')
print(my_tesla.get_descriptive_name())'''
|