Java reflection Class of class exercises

Source: Internet
Author: User

1  PackageZhang;2 /**3 * Java Reflection class exercises4  * 5 * In Object-oriented language, everything is object, so who is the object of the class? 6 * Classes are of class type7  * 8  * */9 classtest{Ten     voidprint () { OneSystem.out.println ("Hello world!"); A     } -}//test is a class.  -  Public classClassdemo { the      Public Static voidMain (string[] args) { -         //How to create a test object -Test test=NewTest ();//this creates the object except test by using new -         //so the class is also an object, as the test class, how do you create an object that is Object test?  +         /* -          *  + * Constructor. Only the Java Virtual machine creates Class A * objects. at * Private Class () {} - * The class class is constructed in a private way, so you cannot create a class object from new.  - * Class objects can only be created from Java virtual machines -          */ -   -         /** in * There are three different ways -          * */ to         /*The first: Class T1=test.class this way is actually telling us any + * A class that has an implied static member class -          *  the * Created with known types *          */ $         Panax NotoginsengClass T1=test.class; -          the         /* + * Second Type A * created with objects of known types the          * */ +Class t2=Test.getclass (); -          $System.out.println (t2==T1); $          -         /* -          *  the * Third Type -          * */WuyiClass t3=NULL; the         Try { -T3=class.forname ("Zhang. Test "); Wu}Catch(ClassNotFoundException e) { -             //TODO auto-generated Catch block About e.printstacktrace (); $         } -          -System.out.println (t3==T2); -         //that is to say: whether T1 or T2 represents the class type of the test class, a class can only be an instance object of a class class.  A          +         //We can create the test object by T1 or T2 or T3.  the         Try { -Test test1= (Test) t1.newinstance ();//This creates an instance of test. $ test1.print (); the              the              the}Catch(instantiationexception e) { the             //TODO auto-generated Catch block - e.printstacktrace (); in}Catch(illegalaccessexception e) { the             //TODO auto-generated Catch block the e.printstacktrace (); About         }     the     } the}

Package Zhang;
/**
* Java Reflection class Exercises
*
* In object-oriented language, everything is object, so who is the object of the class?
* Classes are of class type
*
* */
Class test{
void print () {
System.out.println ("Hello world!");
}
}//test is a class.
public class Classdemo {
public static void Main (string[] args) {
How to create a test object
Test test=new test ();//This creates an object except test by using new
So the class is also an object, as the test class, how do you create an object that is Object test?
/*
*
* Constructor. Only the Java Virtual machine creates Class
* Objects.
* Private Class () {}
* The class class is constructed in a private way, so you cannot create a class object from new.
* Class objects can only be created from Java virtual machines
*/

/**
* There are three different ways
* */
/* First: Class T1=test.class this way is actually telling us any
* A class that has an implied static member class
*
* Created with known types
*/

Class T1=test.class;

/*
* Second Type
* Created with objects of known types
* */
Class T2=test.getclass ();

System.out.println (T2==T1);

/*
*
* Third Type
* */
Class T3=null;
try {
T3=class.forname ("Zhang. Test ");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

System.out.println (T3==T2);
In other words: whether T1 or T2 represents the class type of the test class, a class can only be an instance object of the class.

We can create the test object by T1 or T2 or T3.
try {
Test test1= (Test) t1.newinstance ();//This creates an instance of test
Test1.print ();


} catch (Instantiationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}











Java reflection Class of class exercises

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.