Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of python object-oriented Development

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the python object-oriented development example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Python object oriented

Python has been an object-oriented language from the beginning of its design, which is why it is easy to create a class and object in Python. In this chapter, we will introduce object-oriented programming of Python in detail.

If you haven't been exposed to object-oriented programming languages before, you may need to understand some of the basic features of object-oriented languages and form a basic object-oriented concept in your mind, which will help you learn Python object-oriented programming more easily.

Next, let's briefly understand some of the basic characteristics of the object below.

Brief introduction of object-oriented Technology

Class: used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to each object in the collection. Object is an instance of a class.

Class variables: class variables are common to the entire instantiated object. Class variables are defined in the class and outside the function body. Class variables are not usually used as instance variables.

Data members: class variables or instance variables that deal with data related to the class and its instance objects.

Method rewriting: if a method inherited from the parent class does not meet the needs of the subclass, it can be overwritten. This process is called method override, also known as method rewriting.

Local variable: a variable defined in a method that acts only on the class of the current instance.

Instance variables: in the declaration of a class, properties are represented by variables. Such variables are called instance variables and are declared inside the class declaration but outside the other member methods of the class.

Inheritance: a derived class (derived class) inherits the fields and methods of the base class (base class). Inheritance also allows an object of a derived class to be treated as a base class object. For example, there is a design: an object of type Dog is derived from the Animal class, which simulates the "is a (is-a)" relationship (example, Dog is an Animal).

Instantiation: create an instance of a class, a concrete object of the class.

Method: the function defined in the class.

Object: an instance of a data structure defined by a class. The object includes two data members (class variables and instance variables) and methods.

I. Experimental requirements

1. Familiar with object-oriented mechanisms in python

two。 Ability to correctly define and use classes and objects.

2. Content of the experiment

1 Design a vector class to calculate the input and output of data, the addition, subtraction, dot product and angle of vectors.

Import mathclass Vectors: def _ init__ (self): self.x1 = 0 self.x2 = 0 self.y1 = 0 self.y2 = 0 self.x = self.x2-self.x1 self.y = self.y2-self.y1 def add (self): self.x1 = int (input ("input x1") self.y1 = int (input ("input y1")) Self.x2 = int (input ("input x2")) self.y2 = int (input ("input y2")) def out (self): print (self.x) Self.y) def plus (self, a, b): self.x = a.x + b.x self.y = a.y + b.y def sub (self, a, b): self.x = a.x-b.x self.y = a.y-b.y def pointmuti (self, a, b): return (a.x * b.x + a.y * b.y) def angle (self, a) B): A1 = (A.X * a.x + a.y * a.y) * * 0.5b1 = (b.x * b.x + b.y * b.y) * * 0.5 ab = a.x * b.x + a.y * b.y return ab / / (A1 * b1) v = Vectors () a = Vectors () b = Vectors () b.add () a. Out () b.out () v.plus (a B) v.out () v.sub (a, b) print (v.pointmuti (a, b)) print (math.acos (v.angle (a, b)

2 Design a triangle class to realize the calculation of data input, output, perimeter and area.

Import mathclass Triangle: def _ _ init__ (self): a = 0b = 0c = 0 def add (self): self.a = int (input ("enter the length of the first edge:") self.b = int (input ("enter the length of the second edge:") self.c = int ("enter the length of the third edge:") While (self.a + self.b

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report