Java for three basic overview and characteristics

Java is an object-oriented high-level programming language, classes and objects forms the core of the Java program. Around Java classes and Java objects, there are three basic features: package is written in Java class specification, it is a form of link between class and class inheritance, and polymorphism is decoupling between the system components or modules to provide a solution .

 

  This paper mainly focus on these three characteristics introduce Java object-oriented, component decoupling of the core idea.

 

1, object-oriented thinking

  Object-oriented programming is today's mainstream programming ideas, has replaced the process of application development technology, Java is a fully object-oriented programming language, so you must be familiar with object-oriented will be able to write Java programs.

  Object-oriented programming is the core of the objects, each object contains user-specific function implemented disclosed and hidden portions. Program JDK many objects from the standard library, but we need more programmer-defined class.

  In theory, as long as the object is to achieve business functions, the specific implementation details do not need special care.

  Object-oriented has the following characteristics:

  (1) it is a common object-oriented thinking, thinking more in line with people's habits;

  (2) Object-oriented complex business logic may be simplified and enhanced reusability of code;

  (3) having object oriented abstraction, encapsulation, inheritance, and polymorphism characteristics.

  Object-oriented programming languages ​​are mainly: C ++, Java, C # and so on.

 

2, the relationship between classes and objects

  class:

  Consistency universal feature of certain things, abstract, and description capsulation function, the object is to construct a blueprint or template, written in Java bytecode within certain class. Between classes are: dependence, polymerization, and inheritance relationships.

 

  Object:

  Instance of a class using the new keyword or reflective technology created. All objects of the same class, have similar data (such as a person's age, gender) and behavioral (such as a person's eating, sleeping), but each object holds its own unique state, with the object status of the program run and change, need to pay attention to changes in the state must be changed by calling the method, which is the basic principle of the package.

 

3, packaging ideas

  The core idea is to "hide the details," "data security": the objects do not allow outside access to member variables and methods of privatization, only public methods in line with the wishes of the developer to access these data and logic, to ensure the security of data and stability program.

  Specific implementation is:

  Use  private modifier to private member variable to prevent the direct free to call an external program or modify the member variable, and then provided outside  public of  set and  get method in accordance with the wishes of developers (you can write some business logic code, though this is rarely done) set and get members value of the variable.

  The method may also be present only in the class of internal use  private, the package which is thought, is one of the basic object-oriented development specification.

 

  Here, we need to talk about Java's access modifiers keywords. Java, mainly private, protected, public and default access permissions are four:

  public modifier, with the greatest access, you can access any class in the CLASSPATH under, interfaces, exceptions and so on.

  protected modifier, the main role is to protect the child class, subclass can access these member variables and methods, not the rest of the class.

  default modifier, based mainly present package can access.

  private modifier, access is limited to the interior of this class, in the actual development process, most of the member variables and methods are modified using private.

 

  

 

  Java access control layer is to stay in the compilation, access checks only at compile time, do not leave marks in the class file.

  By reflection, or have access to private members of the class.

       We give a small example:

public  class MobilePhone, { 
    
    // use the keyword private member variable privatization 
    private String os;
     private String phoneNumber;
     private String Brand;
     private  Double dumpEnergy; 

    // provide external access, set the member variables public method
     // so that you can follow us his willingness to visit, set the member variables
     // but also contribute to the effectiveness of internal data verification method 
    public  void Setos (String os) {
         the this .os = os; 
    } 
    public String getOs () {
         return  the this .os; 
    } 
    public  void setPhoneNumber(String phoneNumber){
        this.phoneNumber = phoneNumber;
    }
    public String getPhoneNumber(){
        return this.phoneNumber;
    }
    public void setBrand(String brand){
        this.brand = brand;
    }
    public String getBrand(){
        return this.brand;
    }
    public void setDumpEnergy(double dumpEnergy){
        this.dumpEnergy = dumpEnergy;
    }
    public doublegetDumpEnergy () {
         return  the this .dumpEnergy; 
    } 

    // texting methods, no changes are 
    public  void the sendMessage (Message String, String targetPhoneNumber) { 
        System.out.println ( "issued" + targetPhoneNumber + ", content: "+ Message); 
    } 

    // charging method, no changes are 
    public  Double cHARGE () { 
        System.out.println ( " charging, the remaining power: "* 100 + + dumpEnergy"% " );
         return dumpEnergy; 
    } 
    
    / / boot method to provide both 
    public  void Startup () { 
        System.out.println ( "Booting ......" );
        
        // call the private booting method 
        startup2 (); 

        System.out.println ( "complete boot" ); 
    } 

    // private boot method, the boot package details 
    Private  void startup2 () { 
        System.out.println ( "start the operating system. ..... " ); 
        System.out.println ( " load boot entry ...... " ); 
        System.out.println ( " ...... " ); 
    } 
}
View Code

 

 In the actual development process, such packages have become a norm Bean Java code written. Mainstream framework for assignment objects using reflection techniques, are used in the methods set and get the value, rather than a direct operation of the value field.

 

4, class inheritance and instantiation

  (1) extracting a plurality of different classes in the common data and logic, the commonality of these packaged content i.e. a new parent class (also called base class or superclass), so that prior to the class inherit the class those not necessary to repeat the contents of the common definition of a category, such as BaseDAO, BaseAction like.

  * (2) Java inheritance mechanism is the single inheritance, that is, a class can have only one direct parent.

  * (3) If the parent class and subclass members have the same name as variables and methods, subclass member variables and methods you can use the super keyword to call the parent class, the used method provided that members visible to the subclass.

  * (4) when calling the constructor subclass constructor implicitly call parent class super (). If the parent class constructor no no reference, in order to avoid a compilation error, the need to subclass constructor explicitly call the parent class's constructor parameter containing.

  Call the parent class constructor (5) create subclasses: subclass requires the use of member variables and methods of the parent class, so we'll call the constructor to initialize the parent class, then initialize after subclass member variables and methods. Accordingly, constructor methods are not covered.

  * (6) when extended a required subclasses of the parent class, can override inherited methods, but the method of the subclass must be greater than or equal access to the parent class permissions.

  (7) inherited improve the reusability, scalability program, but also the premise of polymorphic features of the Java language.

  (8) the actual development, program design process, not the conventional parent class, subclass but first have a common data and logic, and then extracted out of the parent package.

 

  We understand simple instantiation process under the class

  (1) JVM class files in the specified reading the classpath, is loaded into memory, if there is direct parent, the parent class will be loaded;

  (2) allocated heap memory space;

  (3) performing the parent class, subclass static block of code;

  (4) Object attribute default initialization;

  (5) calls the constructor;

  (6) in the constructor, to call the parent class constructor initializes parent transactions;

  (7) initialize the parent class data, the display initialize, execute block configuration subclass;

  (8) then initializes a specific subclass constructor;

  (9) After the initialization is complete, the address assigned to the reference

 

  To illustrate the above, let's write a simple example of practical significance, only to demonstrate class inheritance instantiation process.

  

/ * 
    Parent 
* / 
class the Parent { 

    int NUM =. 5 ; 

    static { 
        System.out.println ( "parent static block of code" ); 
        System.out.println (); 
    } 

    { 
        System.out.println ( "parent configuration code block. 1, "+ NUM); 
        NUM =. 1 ; 
        System.out.println ( " parent configuration code block 2, "+ NUM); 
        doSomething (); 
        System.out.println (); 
    } 

    the parent () { 
        System.out.println ( "parent class constructor. 1," + NUM); 
        NUM = 2 ;
        System.out.println ( "parent class constructor 2," + NUM); 
        doSomething (); 
        System.out.println (); 
    } 

    void doSomething () { 
        System.out.println ( "parent method doSomething 1" + NUM); 
        NUM =. 3 ; 
        System.out.println ( "parent method doSomething 2," + NUM); 
        System.out.println (); 
    } 
} 

/ * 
    subclass 
* / 
class child the extends the parent { 

    int NUM = 10 ; 
    
    / * 
        static block of code is executed when the class is loaded 
    * / 
    static { 
        System.out.println ( "subclass static code block" );
        System.out.println (); 
    } 

    / * 
        configuration block 
    * / 
    { 
        System.out.println ( "subclass constructor code block. 1," + NUM); 
        NUM =. 11 ; 
        System.out.println ( "configuration subclass block 2, "+ NUM); 
        doSomething (); 
        System.out.println (); 
    } 

    child () { 
        System.out.println ( " sub-class constructor. 1, "+ NUM); 
        NUM = 12 is ; 
        the System. Out.println ( "subclass constructor 2," + NUM);  
        doSomething ();
        System.out.println (); 
    } 

    void doSomething () {
        System.out.println ( "method doSomething subclass. 1," + NUM); 
        NUM = 13 is ; 
        System.out.println ( "subclass method doSomething 2," + NUM); 
        System.out.println (); 
    } 

} 

public  class A {
     public  static  void main (String [] args) { 
        child child = new new child (); 
        child.num = 20 is ; 
        child.doSomething (); 
    } 
} 

output: 

parent class static block of code 

subclass static code block 

parent code block constructor 1, 5 
parent configuration code block 2, 1 
subclass doSomething 1, 0 
subclass doSomething method 2, 13 


parent class constructor 1, 1 
parent class constructor 2, 2 
subclass doSomething 1, 13 
subclass doSomething method 2, 13 


subclass constructor code blocks 1, 10 
subclass constructor code blocks 2, 11 
subclass doSomething 1, 11 
subclass doSomething method 2, 13 


subclass constructor 1, 13 
sub-class constructor 2, 12 
subclass doSomething method 1, 12 
subclass method doSomething 2, 13 


subclass method doSomething 1, 20 
subclass method doSomething 2, 13
View Code

5, polymorphism, reflection, and decoupling components

  Polymorphism refers to allow objects of different classes of the same "message" to respond. I.e. the same message can be transmitted according to different objects and many different behavior. It can be used to eliminate the type of coupling between the core and the polymorphic Spring is oriented programming interface.

  (1) Java parent class may be used, the interface variable reference subclasses to achieve the object class;

  (2) In the process, we will subclass implementation class objects do automatic type promotion, its unique features can not access, and if need be to make casts.

 

  Technical characteristics of reflection and polymorphism of Java development framework, a core component of decoupling, in this regard, Spring's IOC and DI provides us with an excellent example of learning, Spring's IOC to use reflective technology to create, manage objects, DI injection techniques using polymorphic dependent object for the component.

 

  In the absence of learning Spring, a simple solution is to use an interface .properties file stores a program used to realize class type key information, and then use a global Properties object in the program information stored, and using a reflective technology to achieve these class initialization, a static method to get the object class that implements the specified interface, can be used dependent objects acquired key component in a subject in need.

  The benefits of this program is to bring: When we need to modify the implementation of a component, such as JDBC DAO implementation before the change Hibernate implementation, as long as the implementation of these new classes into the classpath, the .properties file corresponds type interface implementation class into a new class Hibernate implemented without modifying the code of dependent components.

Guess you like

Origin www.cnblogs.com/donleo123/p/11617141.html
Recommended