> 데이터 베이스 > MySQL 튜토리얼 > Python 기본 학습 코드 객체 지향 프로그래밍

Python 기본 학습 코드 객체 지향 프로그래밍

黄舟
풀어 주다: 2016-12-29 17:33:50
원래의
1753명이 탐색했습니다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

class  AddrBookEntry(object):

    'address book entry class'

    def __init__(self,nm,ph):

        self.name = nm

        self.phone = ph

        print 'created instance for:',self.name

    def updatephone(self,newph):

        self.phone = newph

        print 'update phone for:',self.name

    def updatename(self,newname):

        self.name = newname

        print 'update phone for:',self.phone

john = AddrBookEntry('xiewenbin','13711710490')

print john.name

print john.phone

john.updatephone('18617311540')

john.updatename('xwb')

print john.phone

print john.name

class EmpAddrBookEntry(AddrBookEntry):

    'employee address book entry class'

    def __init__(self,nm,ph,id,em):

        AddrBookEntry.__init__(self,nm,ph)

        self.empid = id

        self.email = em

    def updateemail(self,newem):

        self.email = newem

        print 'update email address for:',self.name

jone = EmpAddrBookEntry('jone doe','408-555-1212',42,'543361609@qq.com')

print jone.name

print jone.phone

print jone.email

jone.updatephone('18617311541')

print jone.phone

jone.updateemail('186@qq.com')

print jone.email

class HotelRoomCalc(object):

    'hotel room rate calculator'

    def __init__(self,rt,sales=0.084,rm=0.1):

        '''hotelroot calc  default arguments:

        sales tax == 8.5%  and room tax == 10%'''

        self.salestax = sales

        self.roomtax = rm

        self.rootrate = rt

    def cacltotal(self,days=1):

        'calculator total;default to daily rate'

        daily = round((self.rootrate * (1 + self.roomtax + self.salestax)),2)

        return float(days) * daily

sfo = HotelRoomCalc(299)

print sfo.cacltotal(3)

class TestStaticMethod(object):

    @staticmethod

    def foo():

        print 'calling static method foo()'

class TestClassMethod(object):

    @classmethod

    def foo(cls):

        print 'calling class method foo()'

        print 'foo() is part of class:',cls.__name__

class C(object):

    foo = 100

print C.foo + 1

class Myclass(object):

    'myclass class definition'

    myversion = 19.0

    def showmyversion(self):

        print Myclass.myversion

mc = Myclass()

mc.showmyversion()

print dir(Myclass)

print Myclass.__dict__

"""

class InstCt(object):

    count = 0

    def __init__(self):

        InstCt.count += 1

    def __del__(self):

        InstCt.count -= 1

    def howmany(self):

        return InstCt.count

a = InstCt()

b = InstCt()"""

x = 3 + 0.14j

print x.__class__

print [type(getattr(x,i)) for i in ('conjugate','imag','real')]

class Foo(object):

    x = {2003:'poe2'}

foo = Foo()

print foo.x

foo.x[2004] = 'xie'

print foo.x

print Foo.x

del foo.x

print foo.x

print Foo.x

로그인 후 복사

위 내용은 Python 기본 학습 코드의 객체지향 프로그래밍 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿