极简生活,极简编程,简到极致,就是完美!
今天学的也不是很多,明天应该能把web应用开发部分肝玩。真的写着写着就麻木了,我的,快乐呢!
同样资源在文末,话不多说,干吧!
class Student(object):
def __init__(self, name, age):
self.name = name
self.age = age
def getName(self):
return self.name
def getAge(self):
return self.age
stu1 = Student("张三", 18)
stu2 = Student("李四", 20)
stu_list = [stu1, stu2]
for stu in stu_list:
print(stu.name)
print(stu.age)
张三
18
李四
20
class Car:
_id = 0
_name = ""
_price = 0.0
def __init__(self, cid, name, price):
self._id = cid
self._name = name
self._price = price
def __str__(self):
return "编号:{}--名称:{}--价格:{}".format(self._id, self._name, self._price)
car1 = Car(1001, "丰田", 100000)
car2 = Car(1002, "本田", 150000)
print(car1)
print(car2)
编号:1001--名称:丰田--价格:100000
编号:1002--名称:本田--价格:150000
class Triangle:
bottom = 0
height = 0
def __init__(self, bottom, height):
self.bottom = bottom
self.height = height
def getArea(self):
return (self.bottom*self.height)/2
triange1 = Triangle(10, 20)
print(triange1.getArea())
100.0
import abc
class Person(metaclass=abc.ABCMeta):
@abc.abstractmethod
def say(self):
pass
class Student(Person):
def say(self):
print("好好学习!")
class Teacher(Person):
def say(self):
print("教书育人!")
stu11 = Student()
tea11 = Teacher()
stu11.say()
tea11.say()
好好学习!
教书育人!
class NormalPerson():
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class SimplePerson():
def __init__(self, age, sex):
self.age = age
self.sex = sex
p1 = NormalPerson("张三", 18, "男")
p2 = SimplePerson(0, "未知")
person_list = [p1, p2]
new_list = []
for person in person_list:
if hasattr(person, 'name'):
new_list.append(person)
print(new_list)
[<__main__.NormalPerson object at 0x0000029F445D71F0>]
资料在:面向对象编程.zip,明天见,Byebye!
|