Use guide for Java base Class

  • 2020-04-01 04:34:55
  • OfStack

Everyone knows that Java is an object-oriented programming language. In the Java world, everything is an object. The Class

We know that objects in Java are subclasses of the Object Class, so today we'll look at the use of classes in Java.

Small question: are classes objects? Whose object is the class? The answer is: a Class is an object, a power object of the java.lang.class Class.

Package com.edu.hpu;


public class Test {
  
  public static void main(String[] args) {
    //Instantiate an object with new
    Foo foo = new Foo();
    //An instance of the Class Class is obtained by instantiating the object's getClass() method
    Class c1 = foo.getClass();
    //Any class has an implicit static variable class
    Class c2 = Foo.class;
    //C1 and c2 represent the class type of class Foo.
    System.out.println(c1 == c2);
    
    Class c3 = null;
    try {
      //A third way to get the class type of a class
      c3 = Class.forName("com.edu.hpu.Foo");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    System.out.println(c1 == c3);
    
    try {
      //Creates an instance object of a class by its 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 code above all is for the Class had a deeper understanding, we can use the new keyword to create an instance of the object classes, can also through the Class Class type to create an instance of the object classes, the Class for Class type way of above three kinds of forms, they got only a Class type, so the c1 = = = = serie c2 c3: true.

After the above brief introduction of you are not to have a deeper understanding of class in Java, let's introduce the class type and class instances in practice through a small example.


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();
    }
  }
}

If you look at the above code, when we compile, will it pass? Here we need to talk about compiling and running: classes loaded at compile time are statically loaded; Classes loaded at runtime are dynamically loaded classes. Class.forname (" full name of Class l "); Represents not only the class type, but also dynamically loaded classes. In the above example, we do 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 by 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 according to the needs to facilitate the function expansion of our application.


Related articles: