Java basic Class Class usage guide _java

Source: Internet
Author: User

We all know that Java is an object-oriented programming language, in the Java World, everything is object, that Java how to represent the object? Class

We know that objects in Java are subclasses of the object class, so today we're going to take a look at the use of class in Java.

Ask the little question: Is the class an object? Whose object is the class? The answer is: Class is object, is the strength object of 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 ();
    The instance Class C1 = Foo.getclass () of the class class is obtained by instantiating the GetClass () method of the object.
    Any class has an implied static variable class class C2 = Foo.class;
    
    C1, C2 represents the class type (class type) System.out.println of the Foo class (C1 = = C2);
    Class C3 = null;
    try {//third method of obtaining class type of class C3 = Class.forName ("Com.edu.hpu.Foo");
    catch (ClassNotFoundException e) {e.printstacktrace ();
    
    } System.out.println (C1 = = C3);
      try {//To create an instance object of the class with the class type of the class 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"); }
}

With the above code, do you have a deeper understanding of class? We can create an instance object of a class by using the new keyword, and we can also create instance objects of classes through class types of classes, and get class types of classes in three different ways, they get only one class type, so c1== C2==c3:true.

After the above simple introduction to the Java class has a more in-depth understanding of the following, we have a small example for you to introduce the class type and instances of the class in the actual use.

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


Everyone look at the above code, when we compile, can we normally pass? Here we need to say compile and run: The class that is loaded at compile time is statically loaded, and the class that is loaded at run time is the dynamic load class. Class.forName ("Full name of the L class") not only represents the class type, but also represents the dynamic load class. In the example above, Word and Excel are not necessarily used, but when we compile, if Word and Excel do not exist, there is an error. So next we implement the logic above by dynamically loading classes with class types.

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... ");
  }

This allows us to dynamically add classes as needed to facilitate the functional expansion of our application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.