Python Beginner's abstract class __python

Source: Internet
Author: User

An abstract class is a class that contains abstract methods, and an abstract method does not contain any code that can be implemented and can only implement abstract functions in its subclasses.

the subclass inherits the member variables and member functions of the parent class.

1. Define Abstract Classes

Before you define an abstract class, you need to import the Abcmeta class from class library ABC (that is, metaclass for defining abstract baseclasses, the superclass of the abstract base class), and the Abstractmethod class.

When you define an abstract class, you need to include the following code in the class definition:

__metaclass__ = Abcmeta, which specifies that the class's meta class is Abcmeta. The so-called meta class is the class that creates the class.

You need to add the following code before you define an abstract method:

@abstractmethod

Because an abstract method does not contain any achievable code, its function body usually uses pass. The following shows the implementation and polymorphism of the abstract class. The so-called polymorphism refers to the definition of a method in an abstract class that can be implemented in its subclasses, and the methods implemented in different subclasses vary.


Class shape (object):
	__metaclass__= abcmeta# Specifies that the class's meta class is Abcmeta

	def __init__ (self):
		Self.color = ' black '
	@abstractmethod
	def draw (self):
		pass

	
>>> class Circle (Shape):
	def __init__ (Self,x,y, R):
		self.x = x
		self.y = y
		self.r = r
	def draw (self):
		print ' Draw circle: (%d,%d,%d) '% (self.x, SELF.Y,SELF.R)

		
>>> class Line (Shape):
	def __init__ (self,x1,y1,x2,y2):
		self.x1 = x1
		self.y1 = y1
		self.x2 = x2
		self.y2 = y2
	def draw (self):
		print ' Draw circle: (%d,%d,%d,%d) '% (self.x1, Self.y1,self.x2,self.y2)

		
>>> C = Circle (1,2,3)
>>> C.draw ()
Draw circle: (1,2,3)
>>> L = line (1,2,3,4)
>>> L.draw ()
Draw circle: (1,2,3,4)

Because subclasses of abstract classes implement abstract methods defined in abstract classes, you can define various subclass objects of the same abstract class as elements of a sequence, and then iterate through the list and invoke the abstract method.


>>> C = Circle (1,2,3)
>>> C.draw ()
Draw circle: (1,2,3)
>>> L = line (1,2,3,4)
>>> L.draw ()
Draw circle: (1,2,3,4)
>>> C = Circle (1,2,3)
>>> L = line ( 1,2,3,4)
>>> list = []
>>> list.append (c,l)
>>> list.append (c)
>> > List.append (L)
>>> for I in range (len (list)):
	List[i].draw ()

	
Draw circle: (1,2,3)
Draw Circle: (1,2,3,4)


Here, review the replication of the class, (1): New Object name = original Object name.

As above, CC = c = Circle (1,2,3)

Cc.draw (), the output is the same as C.draw ().

(2): Parameter passing.

def drawcircle (c):

If Isinstance (c,shape):

C.draw ()

c 1 = Circle (1,2,3)

Drawcircle (C1)


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.