Example 54- two single serialized Java mode, the interface Detailed

A single pattern is divided into two cases:

(1) Single starving Example: class loading stage creates an object.

(2) lazy single example: the object will use the time to create the object. (Examples 53 is serialized in the lazy formula)

Example starving the formula:

 

package com.bjpowernode.java_learning;

​

public class D54_1_HungtySingtonMode {

  public static void main(String[] args) {

    Customer54 c1 = Customer54.getCustomer54();

    Customer54 c2 = Customer54.getCustomer54();

    System.out.println(c1==c2);

  }

}

//饿汉式单例模式

class Customer54{

  private static Customer54 c = new Customer54();

  private Customer54() {}

  public static Customer54 getCustomer54(){

    return c;

  }

}

Second, the interface is also a reference type, the class can be seen as equivalent

1. How to define the interface, syntax:

 

[Modifier] interface interface name ()

 

 

Constant, abstract methods: 2. The interface can only appear

3. In fact, the interface is a special abstract class, a special interface is completely abstract

4. The method of the interface is not configured, it can not be instantiated

Inheritance can be more and the interface between the interface 5

6. A class can implement multiple interfaces. (Here the "realization" can be seen as equivalent to "inherit")

7. A non-abstract class implements an interface, the interface will need all methods "implemented / rewrite / coverage"

 

Package com.bjpowernode.java_learning; 


public  interface D54_2_InterfaceExercise { 

  public  static  Final String = SUCCESS "Sucess" ; 

  public  static  Final  Double the PI = 3.1415926 ; 

 

  // public static Final may be omitted in the interface, since the syntax rules as known, only It can be a constant 

  byte MAX_VALUE = 127 ; 

 

  public  abstract  void M54 (); // abstract method 

 

  void test54 (); // this is the abstract methods public abstract are herein may be omitted. Same reason 

} 


interface B54 { 

  void M2 (); 

}

interface the C54 { 

  void M3 (); 

} 

interface D54 { 

  void M4 (); 

} 

interface E53 the extends B54, the C54, D54 { 

  void M5 (); 

} 

// the implements are achieved mean 

// Since a single inheritance class, so here the new keyword implements to achieve 

class MyClass implements B54, C54 { 

  public  void m2 () {} // Because m2 is the original abstract methods, here we have a new class overwrites the m2 method 

  public  void M3 () {} 

} 


class F54 the implements the E54 { 

  public  void M2 () {}

  public  void m3 () {} 

  public  void m4 () {} 

  public  void m5 () {} 

}

 

 

We had an example in accordance with the above seven syntax.

Third, the role of the interface (look at an example, say the next summary)

 

package com.bjpowernode.java_learning;

​

public interface D54_3_CustomerService {

  void logout();

}

 

Package com.bjpowernode.java_learning; 


public  class D54_4_ImplementCustomer { 

  public  static  void main (String [] args) { 

    // The following program interface to call for 

    D54_3_CustomerService D1 = new new D54_4_ImplementCustomerService (); // polymorphism 

    d1.logout (); / / underlying classes, the interface type, the method can be used in the interface, since the bottom is the actual call type, the methods used in the actual class 

  } 

} 

class D54_4_ImplementCustomerService the implements D54_3_CustomerService { 

  // abstract interface methods will be implemented 

  public  void Zimbabwe Logout () { 

    System.out.println ( "successfully exit the system!");

  }

}

Fourth, the source code:

D54_1_HungtySingtonMode.java

D54_2_InterfaceExercise.java

D54_3_CustomerService.java

D54_4_ImplementCustomer.java

address:

https://github.com/ruigege66/Java/blob/master/D54_1_HungtySingtonMode.java

https://github.com/ruigege66/Java/blob/master/D54_2_InterfaceExercise.java

https://github.com/ruigege66/Java/blob/master/D54_3_CustomerService.java

https://github.com/ruigege66/Java/blob/master/D54_4_ImplementCustomer.java

2.CSDN: https: //blog.csdn.net/weixin_44630050 (Xi Jun Jun Moods do not know - Rui)

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/11955053.html