python 기초 9

class Chinese:
    #    
    name = 'xiaoming'

    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c
        print('666')

    def method2(self):
        print('77777')
    #    
    def method1(self):
        print('%s   fengfeng' %self.a)
    def eat_food(self,food):
        print('%s     %s' %(self.a,food))

p1 = Chinese('a','b','c')
print(p1)
print(p1.name)
print(p1.__dict__)
Chinese.method1(p1)
#TypeError: method2() takes 0 positional arguments but 1 was given
p1.method2()

p1.eat_food('  ')

p2 = Chinese('dou',18,'man')
p2.eat_food('  ')
print(dir(p2))
#        
Chinese.s = 'gege'
print(Chinese.s)
def add_mothod():
    print('      ')
Chinese.add_new_method = add_mothod

print(Chinese.__dict__)
#  
Chinese.add_new_method()
del  Chinese.s

print(Chinese.__dict__)
#         
p3 = Chinese('meimei',17,'women')
p3.a = 'xiaoliu'
p3.method1()

def new_mothod1():
    print("      ")
p3.new_method1 = new_mothod1
#  
p3.new_method1()

class Car:
    l = ['a','b','c']
    def __init__(self,a):
        self.name = a
        print('     ')
    def method1(self):
        print('     ')

p1 = Car('fengfeng')
#          p1      list
# p1.l = ['a','b','c']
# print(p1.l)
# print(Car.l)
#         list       
p1.l.append('d')
print(p1.l)
print(Car.l)
print(Car.__dict__)
print(p1.__dict__)

#    
p1.method1
#   
Car.tell_info('newssss')

class Room:
    tag = 1
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    @property
    def method1(self):
        print('    ')
    @classmethod
    def method2(self):
        print('   ')
    @staticmethod
    def method3(a,b,c):
        print(a,b,c,'           ')
#                 。。。。。###        ,      
r1 = Room('fengfeng',11,'man')
#    
r1.method1
#   
Room.method2()
#    
Room.method3(1,2,3)
r1.method3(4,5,6)
print(Room.__dict__)
print(r1.__dict__)

조합 하 다
class School:
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr
    def zhao_sheng(self):
        print('%s     ' %self.name)

class Course:
    def __init__(self,name,price,period,school):
        self.name = name
        self.price = price
        self.period = period
        self.school = school

s1 = School('heima','  ')
s2 = School('heima','  ')
s3 = School('heima','  ')

msg = '''
1 heima     
2 heima     
3 heima     
'''
while True:
    print(msg)
    menu = {
        '1':s1,
        '2':s2,
        '3':s3
    }
    choice = input('    :')
    school_obj  = menu[choice]
    name = input('   >>: ')
    price = input('    >>: ')
    period = input('    >>: ')
    new_course = Course(name,price,period,school_obj)
    print('  【%s】  【%s】  ' % (new_course.name, new_course.school.name))

좋은 웹페이지 즐겨찾기