Discussion-interface, discussion on the application technology of chilies
After studying last week, let meJAVAThe interface in has a preliminary understanding. The following describes some of the interfaces I have learned.
To learn how to use interfaces, you must firstAbstract classWhat is an abstract class? In fact, the biggest difference between an abstract class and a common class is that an abstract class can only be declared and defined, but cannot be instantiated. For a common class, we all know that it can be instantiated, that is, an object can be generated for use. Abstract classes, we need to useAbstractKeyword. At the same time, there is no method subject for abstract methods. In other words, only method declarations are modified by abstract methods, and there is no method implementation. For example:
/*** Common class ** @ author Administrator **/public class Practice1 {}/*** abstract class * @ author Administrator **/public abstract class Practice {}
Abstract classes are different from common classes, that is, modified keywords. The following is the abstract method. The abstract method is also used.AbstractKeyword to modify. abstract methods cannot be defined in common classes. They must be defined in abstract classes, while normal methods can be defined in abstract classes. This may be a bit unclear, take a look at the following example:
// Common class public class Practice1 {// common method public void Method1 () {int a = 0; int B = 10; int c = a + B ;} // abstract method public abstract void Method2 ();/** compiler error: the abstract method method2 in type practice1 can only be defined * by an abstract class indicates that The abstract method can only be defined by one abstract class * // abstract class public abstract class Practice {// common method public void Method1 () {int a = 0; int B = 10; int c = a + B;} // abstract method public abstract void Method2 (); // compiled through}
For interfaces and abstract classes, interfaces can separate design and implementation. abstract analogy interfaces are more flexible, because abstract classes can have common methods, while interfaces can only have abstract methods, you can choose to use it as needed. As long as we understand the abstract class, we can understand more about the interface. First, we can understand the interface as an enhanced version of the abstract class, because the methods in the interface are abstract methods, and the second is that the interface cannot be directly implemented. We useInterfaceKeyword.ImplementsKeyword.
Interface Definition:Access Modifier + interface Name + method subject! Remember that there is no constructor in the interface, and all the member variables defined in the interface are static constants. For example:
// Interface definition public interface Practice2 {// Declaration of the variable int a = 10; // In fact, the following is the complete declaration, but both results are the same, we generally write the above one, which is more concise: public static final int a = 10 ;}
Interface implementation:Implements + Interface NameOrImplements + Interface 1, interface 2, Interface 3...! A class can have multiple interfaces, for example:
// Interface implementation public class Practice3 implements Practice2 {/*** Implementation of the methods in the interface written here */}
Finally, the interface can be used in other classes after implementation. I hope it will help you understand the interface. If there are any errors, I hope you can raise them and help me correct them.