How to use Ethereum interface java
usage public class test implements interface name
when a class implements a java interface, it must implement all abstract methods in the interface, Otherwise, this class must be declared as abstract
compare abstract class and interface
1, both of which are located in the upper layer of the inheritance tree
the same point
1, representing the abstraction layer of the system. When a system uses a class in the inheritance tree, it should declare the reference variable as the upper abstract type of the inheritance tree as far as possible, so as to improve the coupling between the two systems
2, All of them can't be instantiated
3, and all of them contain abstract methods. These abstract methods are used to describe what services the system can provide, but they don't provide specific implementation.
differences:
1. Some methods can be provided with default implementation in the abstract class, so as to avoid implementing them repeatedly in the subclass. This is the advantage of the abstract class, but this advantage limits multi inheritance, The interface can only contain abstract methods. Because concrete methods are allowed to be added to the abstract class, the function of the abstract class is extended, that is, adding concrete methods to the abstract class will not affect its subclasses. As for the interface, once the interface is published, it must be very stable, because adding abstract methods to the interface at will will affect all the implementation classes, These implementation classes either implement new abstract methods or declare as abstract classes
2. A class can inherit only one direct parent class. This parent class may be an abstract class, but a class can implement multiple interfaces. This is the advantage of interfaces, but this advantage is at the cost of not allowing implementation for any method. Third, why doesn't Java allow multiple inheritance? When the subclass covers the instance method of the parent class or hides the member variable and static method of the parent class, the Java virtual machine adopts different binding rules. If a class is allowed to have more than one direct parent class, the binding rules will be more complex.
therefore, in order to simplify the system structure design and dynamic binding mechanism, Java language forbids multiple inheritance, while there are only abstract methods in the interface, There are no instance variables and static methods. Only the implementation class of the interface can implement the abstract methods of the interface (the abstract methods in the interface are implemented by classes). Therefore, even if a class has multiple interfaces, it will not increase the complexity of dynamic binding of Java virtual machine, The general principles of using interfaces and abstract classes are as follows:
1. Using interfaces as the window for the interaction between the system and the outside world, from the perspective of the outside user (another system), the interfaces promise the users what services the system can provide, and from the perspective of the system itself, the interfaces decide which services the system must implement, Interface is the highest level abstract type in the system. The coupling between two systems can be improved through interface interaction. System a interacts through system B, which means that when system a accesses system B, it declares the reference variable as the interface type in system B, and the reference variable refers to the instance of the interface implementation class in system B< br />public interface B
{
}
public class C implements B
{
}
public class A
{
}
B a = new C();
2. The java interface itself must be very stable. Once the java interface is formulated, it is not allowed to be more flexible, otherwise it will affect the external users and the system itself
3. Use abstract classes to customize the extension points in the system
abstract classes to complete part of the implementation, and some functions will be realized through its subclasses
Interface is a mechanism for Java to implement multiple inheritance. A class can implement one or more interfaces. An interface is a declaration of a series of
methods and a collection of method characteristics. An interface has only method characteristics but no method implementation. Therefore, these
methods can be implemented by different classes in different places, and these implementations can have different behaviors. Simply put, interfaces are not
classes, but a set of requirements for classes are defined, and some classes that implement interfaces should be consistent with interfaces
use the keyword interface to define the interface in Java. For example:
< pre t = "code" L = "Java" > public interface compare {public in compare (object other obj)
} < / pre >
compare interface defines an operation compare, which should complete the function of comparing with another object
it assumes that when the object x of a class that implements this interface calls this method, for example, X. compare (y), if x is less than y,
returns a negative number, if x is equal, it returns 0, otherwise it returns a positive number
examples
< pre t = "code" L = "Java" > {privatestringsid// Student number
/ / constructor
10
public student () {
this & quot& quot;,& quot;& quot;,& quot;& quot;);< br />}
publicStudent(Stringname,Stringid,StringsId){
super(name,id);< br />this.sId=sId;< br />}
publicvoidsayHello(){
super.sayHello();< br />System.out.println(".& quot;);< br />}
//get& setmethod
publicStringgetSId(){
returnthis.sId;}< br />publicvoidsetSId(StringsId){
this.sId=sId;}< br />//implementsCompareinterface
publicintcompare(ObjectotherObj){
Studentother=(Student)otherObj;< br />returnthis.sId.compareTo(other.sId);< br />}
}//endofclass
in order to overcome the shortcoming of single inheritance, Java uses an interface, a class can implement multiple interfaces
interface is a collection of abstract methods and constant value definitions, which is a special Abstract class
interface only contains constant and method definitions, There is no implementation of variables and methods.
all methods in the interface are abstract.
the access type of members in the interface is public.
variables in the interface use the public static final identity by default, System default)
interface is declared by using the keyword interface
format: name of interface
interface body:
interface body contains constant definition and method definition
interface body only declares method and does not allow method implementation
method definition has no method body, It ends with a semicolon
public interface runner
{
int id = 1< br /> void run();<
}
interfaces can be inherited, and extensions can be used to inherit an existing interface
public interface runner
{
int id = 1< br /> void run();< br />}
interface Animal extends Runner
{
void breathe();<
}
implement all methods in the interface by using implements
class fish implements animal
{
public void run()
{
system. Out. Println & quot; Fish is swiming");< br /> }
public void breathe()
{
System.out.println(" Fish is bubbling");
}
}
if you only need to implement some methods defined in an interface, you can define an abstract class to implement
abstract class landanimal implements animal
{
public void breath()
{
system. Out. Println & quot; LandAnimal is breathing");
}
a class can implement one or more interfaces while inheriting a parent class. The extensions keyword must precede the implements keyword
class student extensions person implements runner
{
...
public void run()
{
system. Out. Println & quot; The student is Running");
}
...
}
when implementing interface methods in a class, the name, return type, number of parameters and type of the method must be exactly the same as those in the interface
the method in the interface is public by default. When implementing interface methods in all classes, be sure to modify it with public
if the return type of the method in the interface is not void, When implementing the interface method in the class, the method body should have at least one return statement
if it is void type, the class body can have no statement except two braces
the interfaces provided in Java are all in the corresponding package, and the interfaces provided by java can be used by introcing the package, You can also define your own interface
a java source file is composed of classes and interfaces
interfaces can add functions that many classes need to implement. Different classes can use the same interface, and the same class can also implement multiple interfaces
interfaces only care about functions, but do not care about the specific implementation of functions, Classes that use the same interface do not necessarily have inheritance relationship
public interface: when declaring an interface, the keyword interface is preceded by the public keyword, which can be used by any class
friendly interface class: an interface is not modified, and the friendly interface can be used by classes in the same package. It is reproced for reference only.
Interface is a very important object-oriented way. The classes that inherit the interface do not need to implement all the defined interfaces. In other words, I have defined many interfaces. One class can implement the interface in this way, and another class can implement the interface in that way
or you don't want to care after defining the interface, although it's very rendant. There are not many interfaces, which does not affect the operation
For example,defines a linear table interface
public interface Ilist {
public void clear();
public boolean isEmpty();
public int length();
}
If I want to implement it concretely, I will set up the following implementation classes:
import. Illist
public class SqList implements Ilist {
private Object[] listElem;// Linear table storage space
private int curren// The current length of the linear table
/ / the constructor of the sequential table, constructs a linear table with storage capacity of maxsize
public SqList (int maxsize) {
curren = 0
listElem = new Object[maxSize];
}
public void clear() {
curLen = 0;
}
public boolean isEmpty() {
return curLen == 0;// True when curren = = 0= 0 is false
}
public int length() {
return curLen;
}
public interface a {
public void add()// A interface defines a method
}
public class B implements a {/ / B class implements a interface
@ override
public void add() {
/ / where students are added
}
}
public class test {
public static void main (string [] args) {
A = new b()// The key point here is to use an a interface to receive the implementation of B, which realizes the interface programming
a.add()< br /> }
}
functions:
(1) the same behavior of unrelated classes can be realized through interfaces without knowing the corresponding classes of objects
(2) the interface can indicate the methods to be implemented by multiple classes
(3) through the interface, you can understand the interaction interface of the object without knowing the corresponding class of the object.
to put it bluntly, an interface is actually a behavior template, but its behavior needs to be implemented by other classes, because an interface cannot have any specific methods. What we need to do is to specify what a class must do, not how it should do. If we want to implement an interface, we must ensure that the class defined by the interface is to create a complete set of solutions. Java allows developers to make full use of one interface and multiple methods of polymorphism
why use interfaces in Java? What are the benefits of using interfaces
interface is used to standardize class
it can avoid class inconsistency in design
this is particularly important in multi person cooperative development
for example, if there is a method in interface
then a method must be implemented to realize this interface
this forms a specification
what is an interface: an interface is a collection of method features -- an interface is an abstraction of an abstraction
what is abstract class: the partial implementation of an abstract class to a specific type --- abstract class is the abstraction of a specific type
method features include the name of the method, the number of parameters, and the type of parameters. Exclude: return type, parameter name, and exception thrown
interface is the premise of type conversion and the guarantee of dynamic call. Implementing an interface completes the type conversion (multiple inheritance); Dynamic calls only care about types, not specific classes< The java interface (abstract class) is used to declare a new type
java designers should mainly use interfaces and abstract classes to couple software units with internal and external< In other words, Java interfaces and abstract classes should be used instead of concrete classes for variable type declaration, parameter type declaration, method return type declaration, and data type conversion
of course, a better way is to just use interfaces instead of abstract classes to do these things
in an ideal situation, a concrete class should only implement the methods declared in the interface and abstract class, and should not give rendant methods
interfaces and abstract classes are generally used as the starting point of a type hierarchy
interfaces are more abstract than abstract classes, so the interface is preferred to declare abstract types<
abstract classes and interfaces
abstract classes only provide partial implementation of one class. Abstract classes can have instance variables, and one or more constructors. Abstract classes can abstract methods and concrete methods at the same time
an abstract class will not have an instance, and its constructor cannot be used by the client to create an instance. The constructor of an abstract class can be called by its subclasses, so that all subclasses of an abstract class can have some common implementations, and different subclasses can have different implementations on this basis
interfaces are more abstract than abstract classes, so wired uses interfaces to declare abstract classes
abstract classes are used for inheritance Concrete classes are not used to inherit, "if possible, don't inherit from concrete classes -- Scott meryes")
abstract class design principle:
1. Abstract class should have as much code as possible Common method). Code focuses on the direction of abstraction
2. Abstract classes should have as little data as possible Public attribute). The data is focused on specific directions< The subclass is a special kind of superclass, not a role of superclass! Correctly distinguish the relationship between "has-a" and "is-a"
2. Subclasses should not be replaced
3. Subclasses are responsible for extending superclasses, not for overriding or nullifying
4. Inheritance can only be used when it is meaningful from a taxonomic point of view, not from a specific class
differences between interface and abstract class:
1. Abstract class can provide the implementation of some methods. If you add a new concrete method to an abstract class, all subclasses get the method at once. The interface can't do that This may be the only advantage of abstract classes)
2. Due to the limitation of Java's single root structure, only classes can implement only one abstract class type, while interface types have no such limitation. As a result, the performance of abstract class as a type definition tool lags behind that of interface. Interface is an ideal tool for defining mixed types (implementing multiple inheritance): use a
3. From the perspective of code refactoring, it is easy to reconstruct a specific class into an interface implementation
it's useful to study the difference between them!
br/>}
@RequestMapping("/ Delete.do"< <
publicStringdelete(Student sstudents){
try {
stuService.delete(students);
/
}
return " redirect:selectAll.do"; <
