Java-based Class, Java-based Class

Source: Internet
Author: User

Java-based Class, Java-based Class

We all know that Java is an object-oriented programming language. In the Java World, everything is an object. How does Java represent an object? Class

We know that all objects in Java are subclasses of the Object Class, so today we will take a look at the use of the Class in Java.

Q: Is the class an object? Who is the object of the class? The answer is: Class is an object, which is a powerful object of the java. lang. Class.

Package com.edu. hpu; public class Test {public static void main (String [] args) {// instantiate an object Foo = new foo () through new (); // obtain the instance Class c1 = foo of the Class through the getClass () method of the instantiated object. getClass (); // any class has an implicit static variable Class c2 = Foo. class; // c1 and c2 indicate the class type of the Foo class (class type) System. out. println (c1 = c2); Class c3 = null; try {// The third method to obtain the Class type c3 = Class. forName ("com.edu. hpu. foo ");} catch (ClassNotFoundException e) {e. printStackTrace ();} System. out. println (c1 = c3); try {// use the class type to create the class instance object 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 the Class? We can use the new keyword to create the Class instance object, you can also create an instance object of a class by using the class type of the class. There are three methods to obtain the class type of the class. They get only one class type, so c1 = c2 = c3: true.

After a brief introduction above, do you have a deeper understanding of the class in Java? Below we will introduce the actual use of the class type and class instance 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();        }    }}

Let's take a look at the above Code. Can we pass the code normally during compilation? Here we need to talk about the compilation and running: The classes loaded at the Compilation Time are static loading classes, and the classes loaded at the runtime are dynamic loading classes. Class. forName ("Full name of l Class"); not only represents the Class type, but also represents the dynamic loading Class. In the above example, Word and Excel are not necessarily used, but when we compile, if Word and Excel do not exist, then errors will occur. So next we will use the class type to dynamically load the class to implement the above logic.

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 to facilitate functional expansion of our applications.

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.