The notes of a book I read earlier last week when I looked at design patterns, I thought about the notes before reading a book.
Interface-oriented programming is an important point is the interface callback, the interface declaration of the variable is called an interface variable, is a reference variable, you can hold a reference to the instance of the class that implements the interface, the object reference.
Interface callback: You can assign a reference to an object created by a class that implements an interface to an interface variable declared by that interface, then the interface variable can invoke an interface method that is implemented by the class.
com com; Interface
Implcom object; Object that implements the interface
com = object; Interface callback, COM implements different interface methods depending on the object, and the callback class overrides the interface method
The interface and the abstract class are compared as follows:
Abstract classes and interfaces can have an abstract method.
There can be only constants in an interface, no variables, and there can be constants or variables in an abstract class.
Abstract classes can also have non-abstract methods, interfaces are not available.
You should design your program to determine whether to use an abstract class or an interface based on a specific analysis. The abstract class provides variables and non-abstract methods that can be inherited by subclasses in addition to the important abstract methods that need to be rewritten. If an important problem requires inheritance for better resolution, for example, subclasses need to override the abstract method of the parent class, and also need to inherit some variables from the parent class or inherit some important non-abstract methods, you can consider using the abstract class. If a problem does not require inheritance, but requires several classes to give the implementation details of some important abstract methods, you can consider using an interface.
The most important core idea to use interface programming is to use interface callbacks and interface variables to hold references to objects that implement the interface's classes, so that interface variables can be called back to the interface methods implemented by the class.
1 234 |
public interface advertisement { public void showadvertisement ();      public string getcorpname (); |
2, Design Advertisementboard Class (Billboard), The class has a show (advertisement Adver) method, the parameter of the method is the type of the interface advertisement, it is obvious that the parameter adver can hold any reference to the object of the class that implements the advertisement interface, The callback class overrides the interface method Showadvertisement () to display the company's advertising words, and the callback class overrides the interface method Getcorpname to get the company name.
123456 |
public class AdvertisementBoard{ public void show (Advertisement adver){ System.out.println(adver.getCorpName()+ "广告词" ); adver.showAdvertisement(); //接口回调 } } |
A company class that implements the interface:
12345678 |
public
class
Acorp implement Advertisement {
public
void
showAdvertisement(){
System.out.println(
"AAAAAAAAAAAAAAAAA"
);
}
public
String getCorpName(){
return
"A Corp"
;
}
}
|
The B Company class that implements the interface:
12345678 |
public
class
Bcorp implement Advertisement {
public
void
showAdvertisement(){
System.out.println(
"BBBBBBBBBBBBBBBBB"
);
}
public
String getCorpName(){
return
"B Corp"
;
}
}
|
Run Live program:
1234567 |
public
class
test (){
public
state
void
main (string args[]){
AdvertisementBoard board =
new
AdvertisementBoard();
board.show(
new
Acorp());
board.show(
new
Bcorp());
}
}
|
The final program calls different methods depending on the object. If you want to add Company C, just implement the method of the interface.
java--interface-Oriented programming