Java-12.2 Class Object (2)-usage and attention

Source: Internet
Author: User

Java-12.2 Class Object (2)-usage and attention

This chapter is followed by the previous chapter, which describes how to use and pay attention to Class objects.

1. Example

 

Package com. ray. ch11; public class Test {@ SuppressWarnings ({unchecked, rawtypes}) public static void main (String [] args) {try {Class
 
  
SwimmerClass = (Class
  
   
) Class. forName (com. ray. ch11.Swimmer); extends mer = (extends mer) extends merclass. newInstance (); // call the object's method called mer. say (); timer mer. swim (); // obtain the class name System. out. println (timer mer. getClass (). getSimpleName (); // print interface for (Class item: extends mer. getClass (). getInterfaces () {System. out. println (item. getName ();} // obtain the path of the parent class: System. out. println (timer mer. getClass (). getSuperclass (); // new parent class Person person = (Person) extends mer. getClass (). getSuperclass (). newInstance (); // call the person method of the parent class. say ();} catch (ClassNotFoundException e) {System. out. println (class not found);} catch (InstantiationException e) {System. out. println (instantiation);} catch (IllegalAccessException e) {System. out. println (illegal access) ;}} interface canSwim {void swim ();} class Person {public void say () {System. out. println (I am a person) ;}} class extends mer extends Person implements canSwim {public extends mer () {} public extends mer (int age) {}@ Overridepublic void say () {System. out. println (I am a timer mer) ;}@ Overridepublic void swim () {System. out. println (I can swim );}}
  
 

Output:

 

I am a timer mer
I can swim
Timer mer
Com. ray. ch11.canSwim
Class com. ray. ch11.Person
I am a person

 

The above example shows what most Class objects need. However, based on experience, the above method is generally not used in normal programming, because maintenance is very troublesome. If the timer name is changed, the entire class will not run normally.

 

2. Notes

(1) When you need to create a Class object corresponding to Class. forName, you must ensure that there is an empty constructor in the Class. Otherwise, an exception is thrown because newInstance does not accept parameters.

Correct code:

 

package com.ray.ch11;public class Test {public static void main(String[] args) {try {Class
 
   swimmerClass = (Class
  
   ) Class.forName(com.ray.ch11.Swimmer);swimmerClass.newInstance();} catch (ClassNotFoundException e) {System.out.println(class not found);} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}}class Swimmer {public Swimmer() {}public Swimmer(int age) {}}
  
 

Error code:

 

 

package com.ray.ch11;public class Test {public static void main(String[] args) {try {Class
 
   swimmerClass = (Class
  
   ) Class.forName(com.ray.ch11.Swimmer);swimmerClass.newInstance();} catch (ClassNotFoundException e) {System.out.println(class not found);} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}}class Swimmer {public Swimmer(int age) {}}
  
 

The error code throws an exception and outputs:

 

Java. lang. InstantiationException: com. ray. ch11.policmer
At java. lang. Class. newInstance0 (Class. java: 340)
At java. lang. Class. newInstance (Class. java: 308)
At com. ray. ch11.Test. main (Test. java: 8)

 

Summary: This chapter introduces the usage and attention of Class objects. The next chapter introduces other aspects of Class objects.

 

 

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.