Below I will use two examples to fully illustrate the scalability of polymorphic in Java. As an excellent programmer, you must understand the scalability of programs, which is very conducive to the subsequent development of programs.
The first example:
Use the motherboard of the computer as an example. For the motherboard of the computer we are familiar with, do there are many modules, network cards, and sound cards in it? If you want to use these functions, you can only use the motherboard to run and then power the network cards and sound cards. But how can this be achieved in the software language? Do you have to run the motherboard first, and then the sound card or network card run? But in this way, the expansion is not high, and the operation of running the sound card and network card run is repeated. Moreover, many modules also have such functions, so you can simply run the module on the motherboard. This is one of them. If, on that day, when the technology is updated and a new module appears, do you need to create the module object and then run again? This is obviously not conducive to development. However, for this problem, after the motherboard manufacturers discuss with the sound card and network card manufacturers, they will use a PCI interface to connect the motherboard and module in the future, which completely solves the problem of the inability to use or reused updates. This corresponds to the polymorphism in our Java. Using polymorphism, we can greatly improve the scalability of the program!
The specific code is as follows!
/*Requirements: Computer running examples, computer running based on motherboard. */interface PCI{ public void open(); public void close();}class MainBoard{ public void run() { System.out.println("mainboard run "); } public void usePCI(PCI p)//PCI p = new NetCard()//Interface reference points to its own subclass object. { if(p!=null) { p.open(); p.close(); } }}class NetCard implements PCI{ public void open() { System.out.println("netcard open"); } public void close() { System.out.println("netcard close"); method(); } }class SoundCard implements PCI{ public void open() { System.out.println("SoundCard open"); } public void close() { System.out.println("SoundCard close"); }}/*class MainBoard{ public void run() { System.out.println("mainboard run"); } public void useNetCard(NetCard c) { c.open(); c.close(); }}class NetCard{ public void open() { System.out.println("netcard open"); } public void close() { System.out.println("netcard close"); }}*/class DuoTaiDemo5 { public static void main(String[] args) { MainBoard mb = new MainBoard(); mb.run(); mb.usePCI(null); mb.usePCI(new NetCard()); mb.usePCI(new SoundCard()); }}The second example:
Use common databases to explain polymorphism. As for databases that we are familiar with, there are currently two database connection modes: JDBC and Hibernatelian connection. When using databases, there are two essential operations: connection and closing. At this time, use the interface to encapsulate these two operations, and you need to use the connection mode to directly change the class name!
The specific code is as follows!
/*Requirements: Database operation. The data is: user information. 1. Connect to the database. JDBC Hibernate2, operates the database. c create r read u update d delete3, close the database connection. */interface UserInfoDao{ public void add(User user); public void delete(User user);}class UserInfoByJDBC implements UserInofDao{ public void add(User user) { 1, JDBC connects to the database. ; 2. Use SQL to add statement to add data. ; 3, close the connection. } public void delete(User user) { 1, JDBC connects to the database. ; 2. Use SQL Add statement to delete data. ; 3, close the connection. }}class UserInfoByHibernate implements UserInfoDao{ public void add(User user) { 1, Hibernate connects to the database. ; 2. Use SQL to add statement to add data. ; 3, close the connection. } public void delete(User user) { 1, Hibernate connects to the database. ; 2. Use SQL Add statement to delete data. ; 3, close the connection. }}class DBOperate{ public static void main(String[] args) { //UserInfoByJDBC ui = new UserInfoByJDBC();// UserInfoByHibernate ui = new UserInfoByHibernate(); UserInfoDao ui = new UserInfoByHibernate(); ui.add(user); ui.delete(user); }}Summarize:
1. Both examples use interfaces to abstract some repetitive operations. In order to enable different modules to use these operations directly and quickly, we can directly use polymorphic upward transformation (see my previous blog), and then use parent class reference to call it. Anyway, no matter whether the technology is updated and new modules are available or existing modules are replaced, we can use parent class reference to directly call their common operations!
2. You don’t have to use interfaces, you can also use abstract classes. However, using interfaces will be more scalable. If you update them in the future, you can directly change the interface without changing the abstract classes. Secondly, using interfaces can be inherited more! This is also a convenient place.
The above article java uses two examples to fully illustrate the scalability of polymorphisms. This is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.