Simple Object-Oriented Programming

Object-oriented Python

 

Object-Oriented Technology Overview

  • Class (Class):  used to describe a collection of objects having the same properties and methods. It defines the set of properties and methods common to each object. Objects are instances of classes.
  • Class variables: Class variables are common throughout the instantiated object. And class variables defined outside the body of the function in the class. Class variables are typically not used as instance variables.
  • Data members: class variables or instance variables, for the associated data processing and class instance object.
  • Method overrides: If you can not meet the needs of the subclass inherits from a parent class, can be rewritten, this process is called covering methods (override), also known as the overriding method.
  • Local variables: variables defined in the process, only the role of the current instance of the class.
  • Examples of variables: the class declaration, the attribute variable is represented. This variable is called an instance variable, but the statement is in addition to other members of the class methods inside the class declaration.
  • Inheritance: that is, a derived class (derived class) inherits the base class (base class) fields and methods. Inheritance also allows a derived class object as a base class object treated. For example, there is such a design: the object type is derived from a Dog Animal class, which is an analog "is a (is-a)" relationship (FIG embodiment, an Animal Dog).
  • Instantiate: create an instance of a class, the specific object class.
  • Method: function defined in the class.
  • Object: a data structure example defined by the class. Objects include two data members (instance variables and class variables) and methods.

Create a class:

class MyString():

  

Initialization assignment:

#     def __init__(self,engishstr):
#         self.mystr = engishstr

  

 

A simple object-oriented real-column:

#     def show_counts(self):
#         len_word = len(self.mystr)
#         print(len_word)
#     def show_char_counts(self):
#         word = self.mystr
#         len_word = len(self.mystr)
#         word_count = word.count(" ")
#         print(len_word - word_count)
#     def show_cap(self):
#         word = self.mystr
#         word_upper = word.upper()
#         print(word_upper)
#     def show_word_counts(self):
#         word = self.mystr
#         word_split = word.split(" ")
#         len_word = len(word_split)
#         print(len_word)
#     def show_last_word_len(self):
#         word = self.mystr
#         word_split = word.split(" ")
#         word_len = len(word_split[-1])
#         print(word_len)
#     def show_long_word(self):
#         word = self.mystr
#         word_split = word.split(" ")
#         max_word = word_split[0]
#         for x in word_split:
#             if len(x)>len(max_word):
#                 max_word = x
#         print("最长单词为%s,长度位%d" % (max_word,len(max_word)))

  

 

Guess you like

Origin www.cnblogs.com/J-xiaowei/p/11408851.html