程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python object oriented -- basic concepts of classes and objects, relationships between classes and objects, and class design

編輯:Python

One 、 Concepts of classes and objects :

Class and object are two core concepts of object-oriented programming .

1.1 class

Class is a general term for a group of things with the same characteristics or behavior , It is abstract. , Can't be used directly

  1. Features are called attributes
  2. Behavior is called method

Class is equivalent to the drawing when making an airplane , It's a template , Is responsible for creating objects , This drawing does not allow the plane to take off , We have to make an aircraft with pilot conditions first .

 

1.2 object

An object is a concrete existence created by a class , You can use it directly .

Which class creates the object , Just have the properties and methods defined in which class .

In program development , There should be a class first , Another object .

The object is equivalent to an aircraft made of drawings . In a word, a class can be regarded as a drawing , Think of the object as a plane made out of drawings .

 

Two 、 The relationship between classes and objects

Class is a template , Object is created from the template of class , There should be classes before objects .

Class has only one , There can be many objects .

Different objects may have different properties .

What properties and methods are defined in the class , What properties and methods are there in the object , It can't be more , It can't be less .

These are the core concepts in object-oriented programming , One is a class and the other is an object . In a word, the responsibility of a class is very single. It is responsible for creating objects , Define the attributes and methods of the created object in the class . When we have classes, we can use them to create many objects , After the object is created, the method in the class can be called with the object , And set the properties in the class .

3、 ... and 、 Class design

We all know that the final result of an object-oriented program is that different objects call different methods . In object-oriented, you want to create objects , There must be a class first , How is that kind designed ? Let's start slowly .

Before using object-oriented development , Needs should be analyzed first , Determine which classes need to be included in the program .

For example, in the following plant war zombie game , To set the sunflower class , Peas, Sagittarius and so on .

 

In program development , To design a class , There are usually three elements that need to be met :

  1. Class name : The name of this kind of thing , Meet the nomenclature of the great hump
  2. attribute : What are the characteristics of such things
  3. Method : What kind of behavior does this kind of thing have

The name of the great hump :CapWords

Capitalize every word , There is no underline between words

3.1 The determination of class name

Noun extraction , Analyze the nouns appearing in the whole business process , It is usually the class to be found .

3.2 Determination of properties and methods

Description of object characteristics , It can usually be defined as an attribute .

The behavior of objects ( Verb ), It can usually be defined as a method .

Tips : Properties or methods that are not involved in requirements are used in class design , There is no need to consider


practice 1:

demand :

Xiaoming this year 20 year , height 180cm, Every day in the morning run Finishing step , Will go to eat thing

Side dishes this year 18 year , height 165cm, Little dishes don't run , I like small dishes eat thing

answer : Design a human , The attribute contains the name 、 Age 、 height , Behaviors include running and eating , Create two different objects .

practice 2:

demand :

One yellow Of Dogs It's called rhubarb

Meet a stranger bark

See your family Wag the tail

answer : Design a dog class , Attributes are names and dislikes , Behavior is barking and wagging your tail .

  more python Please pay attention to python Self study network


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved