Python class inheritance usage instance analysis

Source: Internet
Author: User

Python class inheritance usage instance analysis

This example describes how to use python class inheritance. Share it with you for your reference. The details are as follows:

?

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

Help ('object') # test

Class Class1 (object ):

"""

Class1 inherits the most basic container class object (just a place holder)

This is the newer class writing convention, adding (object) is "still" optional

"""

K = 7

Def _ init _ (self, color = 'green '):

"""

Special method _ init _ () is called first (acts as Constructor ).

It brings in data from outside the class like the variable color.

(In this case color is also set to a default value of green)

The first parameter of any method/function in the class is always self,

The name self is used by convention. Assigning color to self. color allows it

To be passed to all methods within the class. Think of self as a carrier,

Or if you want impress folks call it target instance object.

The variable k is assigned a value in the class, but outside of the methods.

You can access k in a method using self. k

"""

Self. color = color

Def Hello1 (self ):

Print "Hello from Class1! "

Def printColor (self ):

"In this case self allows color to be passed """

Print "I like the color", self. color

Def _ localHello (self ):

"""

A variable or function with a double underline prefix and no or max. single

Underline postfix is considered private to the class and is not inherited or

Accessible outside the class.

"""

Print "A Hard Hello only used within the class! "

 

Class Class2 (Class1 ):

"""

Class2 inherits Class1 (Class2 is the subclass, Class1 the base or superclass)

Class1 has to be coded before Class2 for this to work !!!

Class2 can now use any method of Class1, and even the variable k

"""

Def Hello2 (self ):

Print "Hello from Class2! "

Print self. k, "is my favorite number"

 

# The color blue is passed to _ init __()

C1 = Class1 ('blue ')

# Class2 inherited method _ init _ () from Class1

# If you used c2 = Class2 (), the default color green wocould be picked

C2 = Class2 ('red ')

Print '-' * 20

Print "Class1 says hello :"

C1.Hello1 ()

Print '-' * 20

Print "Class2 says a Class1 hello :"

C2.Hello1 ()

Print '-' * 20

Print "Class2 says its own hello :"

C2.Hello2 ()

Print '-' * 20

Print "Class1 color via _ init __():"

C1.printColor ()

Print '-' * 20

Print "Class2 color via inherited _ init _ () and printColor ():"

C2.printColor ()

Print '-' * 20

Print "Class1 changes its mind about the color :"

C1 = Class1 ('yellow') # same as: c1. _ init _ ('yellow ')

C1.printColor ()

Print '-' * 20

Print "Wonder what Class2 has to say now :"

C2.printColor ()

Print '-' * 20

# This woshould give an error! Class1 does not have a method Hello2 ()

If hasattr (Class1, "Hello2 "):

Print c1.Hello2 ()

Else:

Print "Class1 does not contain method Hello2 ()"

# Check inheritance

If issubclass (Class2, Class1 ):

Print "Class2 is a subclass of Class1, or Class2 has inherited Class1"

# You can access variable k contained in Class1

Print "Variable k from Class1 =", c1.k

Print '-' * 20

# This woshould give an error! You cannot access a class private method

If hasattr (Class1, "_ localHello ()"):

Print c1. _ localHello ()

Else:

Print "No access to Class1 private method _ localHello ()"

The running result is as follows:

?

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

Help on class object in module _ builtin __:

 

Class object

| The most base type

 

--------------------

Class1 says hello:

Hello from Class1!

--------------------

Class2 says a Class1 hello:

Hello from Class1!

--------------------

Class2 says its own hello:

Hello from Class2!

7 is my favorite number

--------------------

Class1 color via _ init __():

I like the color blue

--------------------

Class2 color via inherited _ init _ () and printColor ():

I like the color red

--------------------

Class1 changes its mind about the color:

I like the color yellow

--------------------

Wonder what Class2 has to say now:

I like the color red

--------------------

Class1 does not contain method Hello2 ()

Class2 is a subclass of Class1, or Class2 has inherited Class1

Variable k from Class1 = 7

--------------------

No access to Class1 private method _ localHello ()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.