Understanding class and Its Application

Source: Internet
Author: User
I. Class class

1. The Class Object describes the running classes and interfaces. You can use the class object to obtain information about the running classes and interfaces.
2. Each class has a corresponding class object. The Class Object of each class is stored in the file where the compiled class is located. Therefore, when the JVM loads a. Class file, it loads a class object. Therefore, a class has only one class object.

Ii. Basic Application of the class Class 1. How to obtain the Class Object of A Class

1.1 using the class. forname (classname) method, this method receives a string parameter to specify the Class Object of the class to be generated, such as class. forname ("dog ").
1.2 It is obtained through class literals.
1) the literal constant is in the form of classname. Class. For example, dog. Class.
2) for basic classes, each basic type of overwriting class has a standard data named type, which can generate a reference pointing to the corresponding basic type of Class Object. For example, Int. Class is equivalent to integer. type.
1.3 obtained through the object. getclass () method, as shown in figure

    1. Dog dog =NewDog ();
    2. Dog. getclass ();

1.4 example of generating a Class Object

  1. ClassCat {
  2. CAT (){System. Out. println ("init CAT ()");}
  3. Static{
  4. System. Out. println ("loading cat ");
  5. }
  6. }
  7. ClassDog {
  8. Dog (){System. Out. println ("init dog ()");}
  9. Static{
  10. System. Out. println ("loading dog ");
  11. }
  12. }
  13. ClassDuck {
  14. Duck (){System. Out. println ("init duck ()");}
  15. Static{
  16. System. Out. println ("loading Duck ");
  17. }
  18. }
  19. Public ClassTest {
  20. Public Static VoidMain (String[] ARGs ){
  21. System. Out. println ("in main ()");
  22. NewCAT ();
  23. System. Out. println ("after create CAT ()");
  24. Try{
  25. ClassC1 =Class. Forname ("dog ");// (1)
  26. ClassC2 = dog.Class;// (2)
  27. }
  28. Catch(ClassnotfoundexceptionCnfe ){
  29. Cnfe. printstacktrace ();
  30. }
  31. System. Out. println ("after class. forname (\" dog \")");
  32. Duck d =NewDuck ();
  33. System. Out. println ("after create duck ()");
  34. ClassC3 = D. getclass ();
  35. System. Out. println ("after class. forname (\" duck \")");
  36. }
  37. }

The running result is:
In Main ()
Loading cat
Init CAT ()
After create CAT ()
Loading dog
After class. forname ("dog ")
Loading duck
Init duck ()
After create duck ()
After class. forname ("Duck ")
Code(1) (2) the class object of class dog is generated, but the class dog object is not generated.
**: Generating a class object does not produce a class object.

2. Use classc object for type comparison

2.1. Direct comparison
The two class objects, whether compared using the equals () function or directly using the = Operator, compare whether they are of the same type.
2.2. Use the class. isinstance (object) function for comparison, as shown in figure

    1. class CAT {}
    2. class dog {}
    3. class duck {}
    4. Public class test {
    5. Public static void main ( string [] ARGs) {
    6. class C2, C3;
    7. duck d = New duck ();
    8. C2 = dog. class ;
    9. C3 = D. getclass ();
    10. System . out. println ("c2.isintance (d):" + (c2.isinstance (D);
    11. System . out. println ("c3.isintance (d):" + (c3.isinstance (D);
    12. }
    13. }

The running result is:
C2.isintance (d): false
C3.isintance (d): True
2.3. Compare with the instanceof keyword. However, if two class objects and class objects in different inheritance systems are compared, a compilation error occurs.

    1. ClassCat {}
    2. ClassDog {}
    3. ClassDuck {}
    4. Public ClassTest {
    5. Public Static VoidMain (String[] ARGs ){
    6. ClassC3;
    7. // Object d = new duck (); (1)
    8. Duck d =NewDuck ();// (2)
    9. C3 = D. getclass ();
    10. System. Out. println ("d instanceof dog:" + (dInstanceofDog ));// (3)
    11. }
    12. }

Because Duck and dog are in two different inheritance systems, code (3) may be compiled incorrectly. If you comment out code (2) and remove the comments of code (1), the compilation will pass. This is because all classes inherit from objects, so objects and dogs are in the same inheritance system and can be compared.
2.4. Comprehensive instance

  1. ClassBase {}
  2. ClassDerived {}
  3. Public ClassTest {
  4. Public Static VoidTest (ObjectX ){
  5. System. Out. println ("testing X of type" +
  6. X. getclass ());
  7. System. Out. println ("x instanceof base" +
  8. (XInstanceofBase ));
  9. System. Out. println ("x instanceof derived" +
  10. (XInstanceofDerived ));
  11. System. Out. println ("base. isinstance (x)" +
  12. Base.Class. Isinstance (x ));
  13. System. Out. println ("derived. isinstance (x)" +
  14. Derived.Class. Isinstance (x ));
  15. System. Out. println ("x. getclass () = base. Class" +
  16. (X. getclass () = base.Class));
  17. System. Out. println ("x. getclass () = derived. Class" +
  18. (X. getclass () = derived.Class));
  19. System. Out. println ("x. getclass (). Equals (base. Class)" +
  20. (X. getclass (). Equals (base.Class)));
  21. System. Out. println ("x. getclass (). Equals (derived. Class)" +
  22. (X. getclass (). Equals (derived.Class)));
  23. }
  24. Public Static VoidMain (String[] ARGs ){
  25. Test (NewBase ());
  26. Test (NewDerived ());
  27. }
  28. }

running result:
testing X of type class base
X instanceof base true
X instanceof derived false
base. isinstance (x) True
derived. isinstance (x) False
X. getclass () = base. class true
X. getclass () = derived. class false
X. getclass (). equals (base. class) True
X. getclass (). equals (derived. class) False
testing X of type class derived
X instanceof base false
X instanceof derived true
base. isinstance (x) False
derived. isinstance (x) True
X. getclass () = base. class false
X. getclass () = derived. class true
X. getclass (). equals (base. class) False
X. getclass (). equals (derived. class) True

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.