【无标题】
作者:互联网
面向对象三大特性_练习题:
1.定义车类(Car),每个对象都有私有属性轮子(wheel),品牌(brand),价格 (price)。 品牌和价格都是通过set、get方法让外部来定义的。(可以使用@property定义)。其中对于价格的设置需要设置条件,如果小于0,则打印信息提示“价格范围异常,未设置成功”;否则正常修改,打印信息“价格修改成功”。
实例化1辆车,尝试修改品牌、价格(正确及错误)的情况。外部分别通过公有方法打印输出私有属性的值。
class Car:
def __init__(self, wheel, brand, price):
self.__wheel = wheel
self.__brand = brand
self.__price = price
@property
def getWheel(self):
return self.__wheel
@property
def getBrand(self):
return self.__brand
@property
def getPrice(self):
return self.__price
def setBrand(self, brand):
self.__brand = brand
def setPrice(self, price):
try:
if int(price) < 0:
print("【价格范围异常,未设置成功】")
else:
self.__price = int(price)
print("【价格修改成功】")
except Exception:
print("【传值有误,请重新修】")
c1 = Car(4, "奔驰", 200000)
c1.setBrand("五菱宏光")
print(c1.getBrand)
c1.setPrice(-2000)
print(c1.getPrice)
c1.setPrice(300000)
print(c1.getPrice)
2.定义一个继承树:父类Animal,派生了三个子类:Bird,Dog和Human。
Animal都有名字(name)。鸟会飞(fly)。狗会叫(bark)。人类会思考(think)。Human派生了2个子类:Student和Teacher。学生能学习(study),老师能教学(teacher)。方法中打印输出对应方法名即可。每个人都有一只狗,每个学生都有一个老师。
分别定义1个Dog、Student、Teacher对象,默认初始化都有名字(name),通过setDog方法和setTeacher方法设置它们之间的关系。
class Animal:
def __init__(self, name):
self.name = name
print(f"一个名叫{self.name}的动物出生了")
class Bird(Animal):
def fly(self):
print("调用fly()方法...")
class Dog(Animal):
def bark(self):
print("调用bark()方法...")
class Human(Animal):
def setDog(self, dog):
self.dog = dog
print(f'{self.name}拥有一只小狗,名叫{self.dog.name}')
def think(self):
print("调用think()方法...")
class Student(Human):
def setTeacher(self, teacher):
self.teacher = teacher
print(f"{self.name}有一位老师,名叫{self.teacher.name}")
def study(self):
print("调用study()方法...")
class Teacher():
def teacher(self):
print("调用teacher()方法")
b1 = Bird("小鹦鹉")
b1.fly()
d1 = Dog("小黄")
d1.bark()
h1 = Human("小明")
h1.think()
h1.setDog(d1)
s1 = Student("小红")
t1 = Student("王老师")
s1.setTeacher(t1)
3.定义父类Tree,子类AppleTree,LemonTree。Tree中有seed、flower、fruit方法。子类重新定义父类fruit的方法,打印输出果实类型。父类中定义grow方法,输出seed、flower、fruit三个过程。分别定义AppleTree和LemonTree的对象,调用上述grow方法。
提示:使用super来访问父类方法。
class Tree:
def seed(self):
pass
def flower(self):
pass
def fruit(self):
pass
def grow(self):
print("调用seed()方法...")
print("调用flower()方法...")
print("调用fruit()方法...")
class AppleTree(Tree):
def fruit(self):
print("果实类型:苹果")
def grow(self):
super().grow()
class LemonTree(Tree):
def fruit(self):
print("果实类型:柠檬")
def grow(self):
super().grow()
app1 = AppleTree()
app1.grow()
app1.fruit()
lemon1 = LemonTree()
lemon1.grow()
lemon1.fruit()
【综合练习】
读程序,画出内存图解,说明答案和为什么。
执行之后检查结果,然后再确认和自己的猜想是不是一致。
(1)
class A:
Country = '中国'
def __init__(self,name,age,country):
self.name = name
self.age = age
def func1(self):
print(self)
a = A('Bhoomi',83,'印度')
b = A('Pchy',74,'泰国')
A.Country = '英国'
a.Country = '日本'
print(a.Country)
print(b.Country)
print(A.Country)
(2)
class A:
Country = ['中国']
def __init__(self,name,age,country):
self.name = name
self.age = age
def func1(self):
print(self)
a = A('Bhoomi',83,'印度')
b = A('Pchy',74,'泰国')
a.Country[0] = '日本'
print(a.Country)
print(b.Country)
print(A.Country)
(3)
class A:
Country = '中国'
def __init__(self,name,age,country):
self.name = name
self.age = age
self.Country = country
def func1(self):
print(self)
a = A('Bhoomi',83,'印度')
b = A('Pchy',74,'泰国')
A.Country = '英国'
a.Country = '日本'
print(a.Country)
print(b.Country)
print(A.Country)
(4) 【扩展】
class A:
Country = '中国'
def __init__(self,name,age,country):
self.name = name
self.age = age
def Country(self):
return self.Country
a = A('Bhoomi',83,'印度')
b = A('Pchy',74,'泰国')
print(a.Country)
print(a.Country())
提示:对象中的属性和方法名相同时,优先访问对象中的方法。
标签:name,Country,self,无标题,print,class,def 来源: https://blog.csdn.net/hacker008_/article/details/123254563