Understanding java-12.6 interface and type information from the beginning (how to bypass all methods of the interface directly calling the Class)

Source: Internet
Author: User

In this chapter we discuss the interface and type information.

In the previous chapters we mentioned the interface, the parent class refers to the subclass object, and then narrows the method, but this chapter learns to reflect, and the constraint becomes less restrictive.

Let's take a look at the following example:

Package Com.ray.ch11;public Class-Test {public-static void main (string[] args) {person mans = new Man (); Man.sleep (); Man.run ();//Man.say ();//error}}interface person {public void sleep ();p ublic void Run (); Class Man implements person {public void Say () {} @Overridepublic void Sleep () {} @Overridepublic void Run () {}}

Our general code will define the interface directly to the above, and then the interface to the implemented class. The general situation is the same as above, the method is less. However, let's modify the method:

Package Com.ray.ch11;public Class-Test {public-static void main (string[] args) {person mans = new Man (); Man.sleep (); Man.run ();//Man.say ();//errorif (man instanceof man) {man man2 = (man) Man;man2.say ();}} Interface person {public void sleep ();p ublic void Run (); Class Man implements person {public void Say () {} @Overridepublic void Sleep () {} @Overridepublic void Run () {}}

We have modified the Isinstanceof method to go down, so that we can get all the methods in man, even the following code:

Package Com.ray.ch11;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;public Class Test {public static void main (string[] args) {try {class<?> Manclass = class.forname ("Com.ray.ch11.Man"); Man mans = (man) manclass.newinstance (); Method[] methods = Man.getclass (). Getdeclaredmethods (); for (Method method:methods) {System.out.println ( Method.getname ());} Method Saymethod = Manclass.newinstance (). GetClass (). Getdeclaredmethod ("say"); Saymethod.setaccessible (true); Saymethod.invoke (man);} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SecurityException e) {e.printstacktrace ();} catch ( Instantiationexception e) {e.printstacktrace (),} catch (Illegalaccessexception e) {e.printstacktrace ();} catch ( Nosuchmethodexception e) {e.printstacktrace (),} catch (IllegalArgumentException e) {e.printstacktrace ();} catch ( InvocationTargetException e) {e.printstacktrace ();}}} Interface person {public void sleep ();p ublic void Run (); Class Mans implements Person{private void Say () {System.out.println ("method say");} @Overridepublic void Sleep () {} @Overridepublic void Run () {}}
With the code above, we call all the methods in the class directly, including private methods.

This is more deadly for encapsulation, so the server-side code we provide generally hides the class's information, such as:

Package Com.ray.ch11;public class Test {public static person Makeperson () {Return to New Man ();} public static void Main (string[] args) {Person person = Makeperson ();}} Interface person {public void sleep ();p ublic void Run (); Class Man implements person {private void Say () {System.out.println ("method say");} @Overridepublic void Sleep () {} @Overridepublic void Run () {}}

We use a method to imply the implementation of the class, all in the form of interfaces to the external display, so that some of the above problems can be avoided.


Summary: This chapter focuses on the content of interface and type information, and needs to be aware of the case where the class is called directly across the interface.


This chapter is here, thank you.

-----------------------------------

Directory




Understand java-12.6 interface and type information from the beginning (How to bypass the interface to call all methods of the class directly)

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.