Write interface applications in Java programming

xiaoxiao2021-03-06  66

The Java language provides an interface mechanism. This interface mechanism makes Java's object-oriented programming more flexible. We can use interfaces to define a class of performance, but the interface cannot contain any implementation. In the "Thinking In Java" book, the author has such a description of the interface: "Interface is more step by step than the abstract" concept. You can regard an interface as a pure abstract class. " I think the author is more accurate to this interpretation of the interface. Understand and use a good interface mechanism will help us better master the object-oriented programming language of Java. Let's discuss the use rules of the interface and related applications. First, the definition of the interface and the implementation of the interface and the definition class are similar, just to change the Class keyword to Interface. The method name, return type and parameter list are required, and there is no method body. Fields can be defined in the interface, which are all finaled as static and final, so it should be set to the value of these fields as needed. E.g:

Public interface flyable {void fly ();} public interface talkable {void Talk ();} public interface message {int max_size = 4096; string getMessage ();

In several interfaces defined above, Flyable and Talkable only define a method, and Message has a field Max_Size in addition to the method. It can be seen that these interfaces define only the form of performance, without any implementation, so it cannot be used directly. To use these interfaces, you need to have a corresponding class to implement them. When implementing an interface, you should use the imports keyword after the class name, and if you want to implement multiple interfaces, you should use a comma to separate them, and then implement the methods defined in these interfaces one by one. Such as follows:

Public Class Parrot Implements Flyable, Talkable {Public Void Fly () {System.out.Println ("Flying Like A Parrot ...");} public void talk () {system.out.println ("Hello! i am a parrot! ");}} public class TextMessage implements Message {String message; public void setMessage (String msg) {message = msg; if (message.length ()> MAX_SIZE) message = message.substring (0, MAX_SIZE);} public String GetMessage () {returnome;}}

In the Parrot (Parrot) example, we use interface Flyable to represent flight capabilities, Talkable represents speaking skills, but they do not include specific implementation. Parrot also has these two capabilities, so we have achieved Flyable and Talkable two interfaces for the Parrot class. Also we can also define a Swallow class, but the swallows are only flying, so we only need to implement flyable for Swallow. Since their respective flight methods are different, they have their own specific implementation of flight. In addition, it is precisely because a class can implement multiple interfaces simultaneously, making Java's object-oriented features very flexible. Using this feature, we can implement multi-inheritance in the C language, even more flexible features. Let's discuss the application of the interface in the actual application. Second, use the interface to define some global variables because the fields in the interface are static and final, so we can easily use this to create some constants. For example: public interface constants {string root = "/ root"; int max_count = 200; INT min_count = 100;}

The constant can be referenced directly in the form of constants.root during use. We can also use the following way to create constants of uncertain initial values.

Public interface randomcolor {int red = math.random () * 255; int Green = math.random () * 255; int blue = math.random () * 255;

Where RED, GREEN, and Blue are established when they are first accessed and remain unchanged. Third, with the interface to define the basic data structure in the early stage of designing a software system, we can use the interface to make some descriptions of some basic data elements, and then perform different implementations as needed. Please take a look at the following example:

Public interface user {int getage (); string getName (); string getPassword ();} public class xmluser imports user {// This () uses XML technology to implement the method in the user interface () PUBLIC INTETAGE () {...} public string getName () {...} public String getPassword () {...}} public abstract class UserFactory {public static UserFactory getUserFactory () {return new XMLUserFactory ();} public User getUser (String name); public User getAdmin ( ); public User createUser (String name, String password, int age); public void addUser (User user); public void delUser (User user);} public class XMLUserFactory extends UserFactory {// UserFactory here implemented with XML abstract art method}

In this example, we define an interface user and an abstract class UserFactory. Then we implement these two classes in XML technology. It can be seen that we only need to get an instance of a UserFactory from getUserFactory () with userFactory without having to consider the specific implementation method of this instance. With this instance of UserFactory, we can also get the User instance, nor do you need to take specific implementation methods. If we decide to implement USER and UserFactory with JDBC technology, we only need to implement JDBCUSER and JDBCUserFactory in the above form. Then modify the GetUserFactory method in the userfactory to change their implementation methods. And we have written USERFACTORY and USER's part of the user do not need to make any modifications. This is a simple example of using an interface to define data structures. There is still a lot of flexible use in practical applications. You need to constantly understand during the learning process. 4. Understanding the principles of distributed applications currently have many software projects use distributed technologies. Java has a variety of technologies that support distributed applications, and there are more technologies such as RMI, CORBA, and now EJB technology is more popular. However, these technologies have been developing, in fact, they are based on interfaces. Call the RMI (Remote Method Invocation) as an example. When writing RMI applications, we need to do two most basic things, first define an interface, this interface should inherit the java.rmi.Remote interface, this interface should contain the method name you want from the remote call. Next is written a class to implement the method in this interface. For example: public interface Product extends java.rmi.Remote {String getName () throws java.rmi.RemoteException;} public class ProductImpl implements Product {String name; public ProductImpl (String n) {name = n;} public String getName () Throws java.rmi.RemoteException {return name;}}

转载请注明原文地址:https://www.9cbs.com/read-86689.html

New Post(0)