Understanding of the "object-oriented" thinking

We must learn to master the language JAVA objects and classes, so that you can understand JAVA object-oriented programming concepts developed deeper to better grasp quickly become JAVA ideas and methods. Master classes and objects are JAVA language becomes a foundation. So, the question is, in the end what is the object and class it?

A: Object
understand about the object is actually very simple. Around us, every thing is a kind of object co-ordinate summarized in one sentence, that there's a physical thing. For example: televisions, car, desk, including the building we human beings are kind of object. If the object is divided into two, namely static and dynamic two parts. Static: As the name suggests, refers to the not moving parts. Take us ourselves, each person your age, sex, height, weight and so on are static. These features can be summarized as Property, all objects are provided with attributes. Dynamic: That people have these properties may make the behavior and actions (smiling, crying, walking, running, etc.). To understand human behavior to explore the object by object's properties and observation of the object.
II: Class
brief summary, the object class is a carrier package properties and behavior of a class of entities other hand has the same properties and behavior referred to as classes. As a simple example, birds. Birds encapsulates all the birds of the specific properties (mouth, wings, claws) and behavior (flight, predation) has. Having said that, I believe we can understand. Speaking of the object, the focus had to mention three basic characteristics of objects: encapsulation, inheritance, polymorphism.
III: Packaging
 Definition: hidden object attributes and implementation details, only open to the public interface to control the level of access to read and modify the properties in the program.

 The purpose of the package: enhance security and simplify programming, the user need not know the specific implementation details, but just want to use members of the class through an external interface, specific access rights.

 Basic requirements encapsulation: all the attributes privatization, providing get and set methods for each property, if there is a constructor with parameters, then must write a constructor with no arguments. Often carried out in the development of the class has been written test, but also to rewrite toString method at all sometimes, but this is not necessary.

Four: Inheritance (extends)
Objective: To achieve code reuse.

Description: When the two classes with the same characteristics (attributes) and behavior (methods) may be extracted into a portion of the same class as a parent class, the class inherits the other two parent class. Following Cheng Houzai class automatically has the properties and methods of the parent class, but special attention is the parent of private property (private) and constructor can not be inherited. In addition subclass can write their own unique properties and methods, it is to extend the project to achieve functional, subclasses can also override the replication method of the parent class, that method. Subclass can not inherit the parent class member variables and methods of access to private. Subclass can override the parent class method, named after the parent class member variables with the same name. Sometimes we need such a demand: we need certain things in this world as much as possible to hide, but allow members to access the sub-class of them. This is a time when you need to use protected.

Usage: 1, to find objects with common attributes and behavior.

           2, the design represents the common state and behavior of the class. (father)

           3, sub-category is decided whether to let an act with a different specific mode of operation. - - - - - - coverage of the inheritance (overwritten).

           4, find out more by looking for opportunities abstract subclasses common behavior.

           5, class inheritance hierarchy is complete.

Five: polymorphic
 concept: the same message may be transmitted according to different objects and behavior in many different ways, which calls the appropriate method for the actual type.

 Method to realize:

(1) Based on the polymorphic implementation inheritance

 Based on the implementation mechanism of inheritance mainly in the parent class inherits the parent class and one or more sub-class of rewriting some methods, multiple sub-class of rewriting the same method may exhibit different behavior.

(2) Multi-state achieved excuse

Inheritance is through several different subclasses override the same method to reflect the parent class, then implement the interface is through, covering the same interface method in a different class to be embodied. Polymorphic interface, the interface must be a reference point to achieve the specified instance of a class of programs that interface. At runtime, the corresponding method is performed based on the actual type of the object reference. Inheritance is single inheritance, can only provide a service interface has a set of related classes, but the interface may be multiple inheritance to achieve more than it can take advantage of a group of related or interface combined with the expansion of unrelated, can provide a consistent outside service interface. So it is with respect to inherit a better flexibility.

Benefits: Enhanced scalability and maintainability of the program.

Summary:
1. The composition of the package to the outside world abstract.
      2. A successor is to abstract information objects.
      3. Polymorphic behavior to abstract object.

Here, give us a little about some relevant information about the extended abstract and interfaces.

Abstract (abstract)
description: In the object-oriented concept, we know that all objects are described by type, but not all classes are used to describe the object, if a class
does not contain enough information to describe a specific target, such a class is an abstract class. An abstract class is often used to represent us in the field of problem analysis, design drawn abstraction is a series of looks different, but essentially the same abstract concepts concrete, we can not instantiate them (take no a specific
thing), so called abstraction.

For example: publicabstract void xuesheng ();

 About abstract methods:

    1, there may be an abstract class abstract methods, which may have non-abstract methods.    
    2, a non-abstract class may all abstract methods.
    3, an abstract method of the abstract class if not, then this class can not be instantiated, whereas if there is an abstract method, then this class must be abstract.

Interface (interface)
 concept: the old things and enhancements, without changing the premise of class methods, further extensions.

 Interface summarized:

      1, in the JAVA interface is a special type defined by the keyword interface.
      2. The interface can only define abstract methods, the method can not have a body, it must be modified public. (Interface methods are abstract)
      3, the interface can define a variable, but is actually a modified static final variable.
      4, the interface can not be defined in a static method.

 Interface definition:

interface Interface {public
   static final int I; // interface can not have common data members, there can be only static data member can not be modified, represents the global static, final unmodifiable, can not static final modification, will implicit declared as static and final

   public void xuesheng1 (); // interface method must be abstract methods, so do not abstract modification

   public void xuesheng2 (); // can not be given to the interface of the default behavior of the method, i.e. a method not have embodied
}


Abstract and contact interface
 1, a plurality of interface may be implemented classes, a class can implement multiple interfaces.
 2, interfaces are all defined fields default public static final attribute, and write do not write no difference.
 3, interface methods are abstract, and abstract public abstract methods are modified default. Other modifications can not use modifiers, can not write.
 4, there is no interface constructor.
 5, the interface is not a class. In particular, can not be used to instantiate a new interface. However, the interface can declare a variable, the variable can point to subclass that implements this interface.

       In summary, an abstract class is a class dysfunction, a set of interfaces only abstract method declarations and static data can not be modified, neither be instantiated. In a sense, interface is a special form of abstract class in JAVA language is an abstract class represents inheritance, a class can only inherit an abstract class, but a class can implement multiple interfaces. In many cases, the interface can indeed replace the abstract class, provided that you do not need to deliberate on the expression of inherited property words.

Guess you like

Origin www.cnblogs.com/WinkJie/p/11788959.html