1. core issues to be addressed by the framework and framework
I made a house for sale to users. Users Installed doors and windows and air conditioners themselves. My house was a framework. users needed to use my framework and insert the doors and windows into the framework I provided. The framework is different from the tool class. The tool class is called by the user class, while the framework is called by the user.
2. core issues to be addressed by the Framework
When I am writing a framework (house), you may still be in elementary school and will not write programs yet? How can I call the framework program you wrote later?
Because you cannot know the name of the class to be called when writing a program, you cannot directly create an instance object of a class in the program. Instead, you must use the reflection method.
3. Comprehensive Cases
- First, use the new statement to create the instance objects of arraylist and hashset. This example uses eclipse to automatically generate the equals and hashcode methods of the reflectpoint class and compare the running results of the two sets.
- Then, create the instance objects of arraylist and hashset by using the configuration file and reflection, and compare and observe the running results.
- Elipse manages resource files
Config. properties: classname1 = java. util. arraylist
Classname2 = java. util. hashset
public class ReflectPoint{ public int x; public int y;public ReflectPoint(int x, int y){super();this.x = x;this.y = y;}public String toString(){return this.x+" "+this.y;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;final ReflectPoint other = (ReflectPoint) obj;if (x != other.x)return false;if (y != other.y)return false;return true;}}
Import Java. util. *; import Java. io. *; public class reflecttest2 {public static void main (string [] ARGs) throws exception {// remember to use the complete path, but the complete path is not hard-coded, it is computed. // For example: project directory/internal path // inputstream in = new fileinputstream ("config. properties "); // use the class loader to get the stream // inputstream in = reflecttest2.class. getclassloader (). getresourceasstream ("day01/config. properties ");/* a class loader can load. class file, it can also load other files in the classpath environment, since it has such capabilities, it has no reason not to provide such a method. It can only load files in the classpath environment. Note: When you directly use the class loader, you cannot start. */Inputstream in = reflecttest2.class. getresourceasstream ("Resources/config. properties "); properties prop = new properties (); prop. load (in); In. close (); // close system resources // string classname1 = prop. getproperty ("classname1"); string classname2 = prop. getproperty ("classname2"); // collection collections = (Collection) class. forname (classname1 ). newinstance (); Collection collections = (Collection) class. forname (classname2 ). newinstance (); reflectpoint rp1 = new reflectpoint (3, 3); reflectpoint RP2 = new reflectpoint (5, 6); reflectpoint RP3 = new reflectpoint (3, 3); collections. add (rp1); collections. add (RP2); collections. add (RP3); collections. add (rp1); system. out. println ("Collections:" + collections. size ());}}
Iv. Configuration File Management