Reflection mechanism: Java. Lang. Reflect

Source: Internet
Author: User

Basic Features of object-oriented programming: encapsulation, inheritance, and polymorphism. Therefore, the following example uses interfaces, abstract classes, and subclasses.

Animal interface:

package com.taobao.demo;public interface Animal {void eat();void say();}

Abstractanimal abstract class:

package com.taobao.demo;public abstract class AbstractAnimal implements Animal {public String name;private Gender gender;public AbstractAnimal() {}public AbstractAnimal(String name, Gender gender) {this.name = name;this.gender = gender;}@Overridepublic void eat() {System.out.println("eat...");}@Overridepublic void say() {System.out.println("say...");}public String getName() {return name;}public void setName(String name) {this.name = name;}public Gender getGender() {return gender;}public void setGender(Gender gender) {this.gender = gender;}public enum Gender {MALE, FEMALE;}}

Cat class:

Package COM. taobao. demo; public class cat extends actanimal implements animal {// Add a private attribute private color eyecolor to the CAT; Public CAT () {} public CAT (string name, gender, color eyecolor) {super (name, gender); this. eyecolor = eyecolor;} @ overridepublic void eat () {system. out. println ("I like to eat fish. ") ;}@ overridepublic void say () {system. out. println ("Miao... miao... miao... ");} public color geteyecolor () {return eyecolor;} public void seteyecolor (color eyecolor) {This. eyecolor = eyecolor;} public Enum color {white, blue, yellow, black ;}}

Dog class:

Package COM. taobao. demo; public class dog extends actanimal implements animal {// Add a private attribute private int age to the dog; public dog () {} public dog (string name, gender Gender, int age) {super (name, gender); this. age = age ;}@ overridepublic void eat () {system. out. println ("I like to eat bone. ") ;}@ overridepublic void say () {system. out. println ("Wang... wang... wang... ");} public void setage (INT age) {This. age = age ;}public int getage () {return age ;}}

The above classes are basic classes used for experiments. It is also a meaningless class specially written for practice.

Animalfactory class:

package com.taobao.demo.factory;import com.taobao.demo.Animal;public class AnimalFactory {public static Animal getInstance(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException {return (Animal) Class.forName(className).newInstance();}}

 

Test reflection practices:

Package COM. taobao. demo. reflect; import Java. lang. reflect. constructor; import Java. lang. reflect. field; import Java. lang. reflect. method; import COM. taobao. demo. using actanimal; import COM. taobao. demo. animal; import COM. taobao. demo. CAT; import COM. taobao. demo. factory. animalfactory; public class reflectpractice {public static void main (string [] ARGs) throws exception {// all class objects are class instance classes <?> Clazz1 = Class. forname ("com. Taobao. Demo. Cat"); Class <?> Clazz2 = new CAT (). getclass (); Class <?> Clazz3 = cat. class; // judge whether the above three are of the same class system. out. println (clazz1.equals (clazz2) & clazz1.equals (clazz3); // output class name system. out. println (clazz1.getname (); // obtain the Instance Object cat cat0 = (CAT) clazz1.newinstance (); CAT cat1 = new CAT (); // call the cat class constructor by class. The default constructor is obtained in a strange order. Cat cat2 = (CAT) clazz1.getconstructors () [1]. newinstance ("miaomiao", named actanimal. gender. male, Cat. color. yellow); // obtain the corresponding construc by specifying the parameter type Tor <?> CSR = clazz1.getconstructor (string. class, abstractanimal. gender. class, Cat. color. class); // print the method definition, return value, parameter type, and other system of the constructor. out. println (CSR. togenericstring (); // This constructor constructs the Instance Object cat cat3 = (CAT) CSR. newinstance ("miaomiao", named actanimal. gender. male, Cat. color. yellow); // obtain the parent class and Interface Information class <?> Superclass = clazz2.getsuperclass (); system. Out. println ("parent class name:" + superclass. getname (); Class <?> Clazz4 = Class. forname (clazz2.getsuperclass (). getname (); system. Out. println (clazz4.getname (); Class <?> [] Interfaces = clazz3.getinterfaces (); For (class <?> Interf: interfaces) {system. out. println (interf. getname ();} // obtain the attribute field [] fields = clazz3.getdeclaredfields (); For (field: fields) {system. out. println (field. getname ();} // get the attributes of the parent class. Field [] fields2 = clazz3.getfields (); system. out. println ("parent class attribute" + fields2.length); For (field: fields2) {system. out. println (field);} // obtain the member method [] meths = clazz3.getmethods (); For (method: meths) {system. out. println (method);} // call methods of other classes method = clazz1.getmethod ("say"); CAT catt = (CAT) clazz1.newinstance (); method. invoke (catt); method = clazz1.getmethod ("seteyecolor", Cat. color. class); method. invoke (catt, Cat. color. yellow); system. out. println (catt. geteyecolor (); // The Field field = clazz1.getfield ("name"); field. set (cat0, "xiaomao"); system. out. println (field. get (cat0); // test the factory METHOD Animal animal = animalfactory. getinstance ("com. taobao. demo. cat "); animal. getclass (). newinstance (). say ();}}

 

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.