Everyone knows that Java is an object-oriented programming language. In the Java world, everything is an object. How do you represent an object in that Java? Class
We know that objects in Java are subclasses of the Object class, so today we will study the use of Class in Java together.
Ask a small question: Are classes objects? Whose object is the class? The answer is: the class is an object, a strength object of the java.lang.Class class.
package com.edu.hpu;
public class Test { public static void main(String[] args) { //Instantiate an object by new Foo foo = new Foo(); //Get the instance of the Class class by instantiating the getClass() method of the object Class c1 = foo.getClass(); //Any class has an implicit static variable class Class c2 = Foo.class; //c1 and c2 represent the class type of Foo class (class type) System.out.println(c1 == c2); Class c3 = null; try { //The third method to obtain the class type of the class c3 = Class.forName("com.edu.hpu.Foo"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(c1 == c3); try { //Create an instance object of the class through the class type Foo c4 = (Foo) c1.newInstance(); c4.start(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }}class Foo{ public void start(){ System.out.println("Foo class"); }}Through the above code, do you have a deeper understanding of Class? We can create class instance objects through the new keyword, and we can also create class instance objects through the class type. There are three forms of the above methods to obtain class class types. They only get one class type, so c1==c2==c3:true.
After the brief introduction above, do you have a deeper understanding of class in Java? Let’s use a small example to introduce the actual use of class types and class instances.
class Offices{ public static void main(String [] args){ if("Word".equals(args[0])){ Word w = new Word(); w.start(); } if("Excel".equals(args[0])){ Excel e = new Excel(); e.start(); } }}Let’s take a look at the above code. When we compile, can it pass normally? Here we need to talk about compilation and operation: the class loaded at the compile time is a static loading class; the class loaded at the run time is a dynamic loading class. Class.forName("full name of class l"); not only represents the class type, but also represents the dynamic loading class. In the above example, we may not necessarily use Word and Excel, but when we compile, if Word and Excel do not exist, then an error will occur. So next we implement the above logic by dynamically loading the class type.
class Office{ public static void main(String [] args){ try{ Class c = Class.forName(args[0]); OfficeAble oa = (OfficeAble)c.newInstance(); oa.Write(); }catch(Exception e){e.printStackTrace();} }} interface OfficeAble{ public void Write();}class Word implements OfficeAble{ public void Write(){ System.out.println("Word...start..."); }}class Excel implements OfficeAble{ public void Write(){ System.out.println("Excel..Write.."); }}In this way, we can dynamically add classes as needed, so as to facilitate the expansion of our application's functions.