A model multiple inheritance method, device and medium implemented using Java language
The business model is described by metadata and dynamically analyzed the annotation of the inheritance relationship, realizing the function of multiple models in Java, solving the problem of large code replication and maintenance workload in existing technologies, and improving code reuse and flexibility.
Patent Information
- Application Number
- CN202311363045.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-20
- Publication Date
- 2025-01-24
- Estimated Expiration
- 2043-10-20
AI Technical Summary
The prior art achieves a multi-inheritance effect by using Java interfaces, but there are problems such as large workload of code copying and maintenance and high expansion complexity.
Metal data describes business models. Through model annotations, the model is declared in the Java class, the original metadata is obtained, the meta -models are established, the model registry is registered, the inheritance relationship is dynamically analyzed, and the model inherits more.
The development and maintenance workload has been reduced, the code reuse and flexibility is improved, the multiple inheritance is inherited, and the inheritance relationship is dynamically modified during runtime, which is suitable for online installation and unloading scene.
Smart Images

Figure CN117234500B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer technology, and in particular, relates to a model multiple inheritance method, device and medium implemented using Java language. Background Art
[0002] Compared with single inheritance, multiple inheritance has the following advantages: Multiple inheritance can inherit properties and methods from multiple parent classes: (1) Through multiple inheritance, a derived class can obtain the properties and methods of multiple different parent classes at the same time, which makes the code more reusable and flexible, and avoids copying and pasting code to achieve similar functions; (2) Multiple inheritance can express richer class relationships: Some real-world problems need to be modeled using the characteristics of multiple different parent classes, for example: a class has the characteristics of both "animal" and "aircraft". Through multiple inheritance, derived classes can directly inherit these different characteristics and accurately express the relationship between classes.
[0003] The Java language itself does not support multiple inheritance, that is, a class cannot directly inherit from multiple classes. This is one of the design decisions of the Java language, which aims to maintain the clarity of the code, the simplicity of the inheritance relationship and reduce potential conflicts. In the prior art, Java can achieve a similar effect of multiple inheritance by using interfaces. Java allows a class to implement multiple interfaces, thereby obtaining the functions of multiple interfaces. Its main shortcomings are as follows: (1) The code needs to be copied in each class that implements the interface to complete the implementation of the interface, which results in a large amount of development work or program maintenance work; (2) Only new subclasses can be obtained, and the original class will not be modified, which increases the complexity and amount of code for the expansion. Summary of the invention
[0004] The purpose of the present invention is to provide a model multiple inheritance method implemented by Java language to solve the technical problems existing in the prior art that similar multiple inheritance is implemented by using interfaces.
[0005] In order to achieve the above object, the technical solution adopted by the present invention is as follows:
[0006] A model multiple inheritance method implemented using Java language includes the following steps:
[0007] Step S1: Declare the model using model annotation in the Java class;
[0008] Step S2: When Java is running, reflect the Java class annotated with the marked model to obtain the original metadata of the model declaration in step S1, where the original metadata includes the name, inheritance relationship, fields and methods of the model;
[0009] Step S3: constructing a meta-model according to the inheritance relationship of the original metadata;
[0010] Step S4: registering the meta-model into the model registry;
[0011] Step S5: obtaining a meta-model from a model registry according to the model name of the original metadata;
[0012] Step S6: Call the metamodel creation method in step S5 to instantiate a model dataset, wherein the model dataset provides methods for reading and writing fields through the get / set methods of the model dataset, and the values of the fields are stored in the dataset repository. The model dataset provides methods for executing the model through the call method of the model dataset, and the model dataset provides methods for executing the parent model inherited by the model through the callSuper method of the model dataset.
[0013] In one embodiment, the step S1 of declaring the model using the model annotation in the Java class specifically includes:
[0014] (1) Use annotations on Java classes to declare the model name and inheritance relationship;
[0015] (2) In a Java class, use static fields to declare the original metadata of the model fields;
[0016] (3) Use annotations on Java methods to declare the model method.
[0017] In one implementation, the specific method of constructing the meta-model according to the original metadata in step S3 is as follows: according to the inheritance relationship of the original metadata, the final model metadata is parsed and saved as the meta-model.
[0018] In one implementation, in step S3, during Java runtime, annotations are dynamically parsed to construct an inheritance relationship; in the inheritance relationship, the fields of the child model override the fields of the parent model, and the methods of the child model override the methods of the parent model. When inheriting multiple parent models, the inheritance link is parsed in the order of declaration.
[0019] In one embodiment, parsing the final model metadata includes parsing the name, inheritance relationship, fields, and methods of the model.
[0020] In one implementation, when parsing a model method, a model method linked list is constructed according to the MRO algorithm, and the constructed method linked list is stored in a method dictionary.
[0021] In one implementation, when a model method is called, a method list is loaded from the method dictionary according to the method name, and a real Java method is matched for calling.
[0022] In one implementation, when methods of the same Java class call each other, all model methods are intercepted by the dynamic proxy of the Java class, and the interceptor determines whether the current method should be executed or needs to be calculated based on the model MRO, and calls are made based on the judgment result.
[0023] To achieve the above object, the present invention also provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the model multiple inheritance method implemented using the Java language.
[0024] To achieve the above-mentioned purpose, the present invention also provides a model multiple inheritance device implemented using Java language, comprising a processor and a memory; the memory is used to store computer programs; the processor is connected to the memory, and is used to execute the computer programs stored in the memory, so that the model multiple inheritance device implemented using Java language executes the above-mentioned model multiple inheritance method implemented using Java language.
[0025] Compared with the prior art, the present invention has the following beneficial effects:
[0026] (1) The business model described in the present invention in the form of metadata supports multiple inheritance reuse, and can extract common business logic into an independent model that can be inherited and reused in a specific business model. Compared with the prior art that uses a Java class to implement multiple interfaces, it is necessary to copy the code in each class that implements the interface to complete the implementation of the interface. The present invention has a lower development workload or program maintenance workload, which can effectively improve efficiency.
[0027] (2) The model in the present invention is analytical. The inheritance relationship of the model, in addition to supporting multiple inheritance, can also inherit itself, thereby expanding the original model. After the development of the standard functions of the system is completed, secondary development does not need to modify the original source code. By extending and inheriting the existing model, the system can be expanded and upgraded.
[0028] (3) By adopting the present invention, annotations are dynamically parsed to construct inheritance relationships at runtime. The inheritance relationships can also be modified while the program is running, and they can be re-parsed after modification. This is faster than dynamic compilation and has more flexible combination capabilities. It is suitable for scenarios where applications are installed and uninstalled online. After installing a new application, the model relationship can be re-parsed to take effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 This is a schematic diagram of the process of Example 1 of the present invention.
[0030] Figure 2 This is a principle block diagram of Example 3 of the present invention. DETAILED DESCRIPTION
[0031] In order to enable those skilled in the art to have a clearer understanding and knowledge of the present invention, the present invention is further described in detail below in conjunction with the embodiments. It should be understood that the specific embodiments described below are only used to explain the present invention, and are convenient for understanding. The technical solutions provided by the present invention are not limited to the technical solutions provided by the following embodiments, and the technical solutions provided by the embodiments should not limit the protection scope of the present invention.
[0032] It should be noted that the illustrations provided in the following embodiments are only used to illustrate the basic concept of the present invention in a schematic manner. Therefore, the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the form, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0033] Example 1
[0034] like Figure 1 As shown, this embodiment provides a model multiple inheritance method implemented in Java language. The design principle of this method is: using the parsing operation method, describing the business model in the form of metadata, the inheritance relationship of the model, and supporting multiple inheritance reuse.
[0035] In this embodiment, the specific steps of the model multiple inheritance method implemented using the Java language are as follows:
[0036] 1. Declare the model using model annotations in Java classes
[0037] A model is similar to a Java class, containing metadata such as name, inheritance relationship, fields, methods, etc. The specific contents are as follows:
[0038] (1) Use annotations on Java classes to declare the model name and inheritance relationship; use @Model.Meta(name = "model name", inherit = {"parent model 1", "parent model 2"}) to declare the model on the Java class. The annotation Model.Meta mainly contains the following information: name, title, inheritance relationship, description, database table, sorting order, method and other information;
[0039] (2) In the Java class, static fields are used to declare the original metadata of the model fields. The original metadata includes the name, inheritance relationship, fields and methods of the model. The model fields are declared using the Field class, such as staticField name = Field.Char().label("name"). If an inherited field has the same name, it is considered as an override and the field metadata will be merged. The field metadata mainly includes: name, title, required, index, type, etc. Field types include Char, Boolean, String, Float, Selection, Many2one, One2many, Many2many, etc. The model method refers to a Java class method that meets the following two conditions: 1. The modifier is public; 2. The first parameter is the method of the dataset Records. If an inherited method has the same name, it is considered as an override. The overridden method is constructed into a model method list according to the MRO algorithm.
[0040] (3) Use annotations on Java methods to declare the model method.
[0041] 2. Get the original metadata of the model declaration at Java runtime
[0042] When Java is running, the Java class annotated with the model is reflected to obtain the original metadata of the model declaration, where the original metadata includes the name, inheritance relationship, fields and methods of the model; the content of the model declaration can be obtained through Class.getAnnotation(Model.Meta.class).
[0043] 3. Constructing a metamodel based on the inheritance relationship of the original metadata
[0044] The metamodel is constructed based on the original metadata. Specifically, the final model metadata is parsed based on the inheritance relationship of the original metadata and saved as a metamodel. In the inheritance relationship, the fields of the child model override the fields of the parent model, and the methods of the child model override the methods of the parent model. When inheriting multiple parent models, the inheritance link is parsed in the order of declaration. Model metadata mainly includes: name, title, inheritance relationship, description, database table, sorting order, method, field and other information. All declared model information is saved in the MetaModel metamodel.
[0045] It should be noted that, through the above settings, it is possible to dynamically parse annotations to construct inheritance relationships during Java runtime. Therefore, the inheritance relationship can be modified while the program is running, and it can be re-parsed after modification. It is faster than dynamic compilation performance and has more flexible combination capabilities.
[0046] Parsing the final model metadata includes parsing the model's name, inheritance relationship, fields, and methods.
[0047] When parsing model methods, a model method list is constructed according to the MRO algorithm (MRO (Method Resolve Order) algorithm of the Python language), and the constructed method list is saved in the method dictionary.
[0048] 4. Register the metamodel to the model registry
[0049] Register all parsed metamodels in the model registry.
[0050] 5. Get the metamodel from the model registry based on the model name of the original metadata
[0051] 6. Call the creation method of the metamodel to instantiate a model dataset
[0052] Based on the obtained metamodel, call the creation method of the metamodel to instantiate a model dataset; specifically, obtain the metadata MetaModel through the model registry Register.Get("model name"), and create the dataset Records through MetaModel.browser(string[]ids); read and write fields through the get / set method of the model dataset, and the value of the field is saved in the dataset warehouse; execute the model method through the call method of the model dataset, and execute the method of the parent model inherited by the model through the callSuper method of the model dataset.
[0053] After the model is built, the logic of calling the model method is as follows: when calling the model method, load the method list from the method dictionary according to the method name, match the real Java method for calling; when the methods of the same Java class call each other, intercept all the model methods through the dynamic proxy of the Java class, and judge in the interceptor whether the current method should be executed or calculated according to the model MRO, and find the correct method to call according to the judgment result. For example: use three Java classes, class A, class B, and class C, to declare model M. There are run methods in all three classes. The inheritance order of M is C->B->A. The method list obtained after MRO construction is {run:[C.run,B.run,A.run]}. When calling the run method of model M, C.run will be found in the method list for execution. If callSuper is used in C.run to call the overridden parent model method, B.run will be found in the method list for execution.
[0054] By using the method provided in this embodiment, the inheritance relationship is constructed by dynamically parsing annotations at runtime, and the inheritance relationship can be modified while the program is running. The modification will take effect after re-parsing. For example, model x is declared to inherit model m in application app1, and is declared to inherit model n in application app2; when only application app1 is installed, model x inherits all the characteristics of model m, and when both applications app1 and app2 are installed, model x will inherit all the characteristics of both m and n.
[0055] Example 2
[0056] This embodiment provides a computer-readable storage medium, on which a computer program is stored, and the computer program is executed by a processor to implement a model multiple inheritance method implemented in Java language provided in Embodiment 1. A person of ordinary skill in the art can understand that all or part of the steps of implementing the method provided in Embodiment 1 can be completed by hardware related to the computer program, and the above-mentioned computer program can be stored in a computer-readable storage medium, and when the program is executed, the steps of the method provided in Embodiment 1 are executed; and the above-mentioned storage medium includes: ROM, RAM, magnetic disk or optical disk and other media that can store program codes.
[0057] Example 3
[0058] like Figure 2As shown, this embodiment provides a model multiple inheritance device implemented using Java language, including: a processor and a memory; the memory is used to store computer programs; the processor is connected to the memory, and is used to execute the computer programs stored in the memory, so that a model multiple inheritance device implemented using Java language executes a model multiple inheritance method implemented using Java language provided in Example 1.
[0059] Specifically, the memory includes: ROM, RAM, disk, USB flash drive, memory card or CD and other media that can store program codes.
[0060] Preferably, the processor can be a general-purpose processor, including a central processing unit, a network processor, etc.; it can also be a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component.
[0061] The above embodiments are merely illustrative of the principles and effects of the present invention, and are not intended to limit the present invention. Anyone familiar with the art may modify or alter the above embodiments without departing from the spirit and scope of the present invention. Therefore, all equivalent modifications or alterations made by a person of ordinary skill in the art without departing from the spirit and technical concept disclosed by the present invention shall still be covered by the claims of the present invention.
Claims
1. A model multiple inheritance method implemented using Java language, characterized in that: The following steps are involved: Step S1: Declare the model using model annotation in the Java class; Step S2: When Java is running, reflect the Java class annotated with the marked model to obtain the original metadata of the model declaration in step S1, where the original metadata includes the name, inheritance relationship, fields and methods of the model; Step S3: constructing a meta-model according to the inheritance relationship of the original metadata; Step S4: registering the meta-model into the model registry; Step S5: obtaining a meta-model from a model registry according to the model name of the original metadata; Step S6: calling the creation method of the metamodel in step S5 to instantiate a model dataset, wherein the model dataset provides a method for reading and writing fields through the get / set method of the model dataset, and the value of the field is stored in the dataset warehouse, the model dataset provides a method for executing the model through the call method of the model dataset, and the model dataset provides a method for executing the parent model inherited by the model through the callSuper method of the model dataset; The step S1 of declaring the model using the model annotation in the Java class specifically includes: (1) Use annotations on Java classes to declare the model name and inheritance relationship; (2) In a Java class, use static fields to declare the original metadata of the model fields; (3) Use annotations to declare model methods on Java methods; The specific method of step S3 to construct a meta-model according to the original metadata is as follows: according to the inheritance relationship of the original metadata, the final model metadata is parsed and saved as a meta-model; In the step S3, when Java is running, the annotation is dynamically parsed to construct an inheritance relationship; in the inheritance relationship, the fields of the child model cover the fields of the parent model, and the methods of the child model cover the methods of the parent model. When inheriting multiple parent models, the inheritance chain is parsed in the order of declaration; the final model metadata parsed includes the name, inheritance relationship, fields and methods of the parsed model; when parsing the model method, a model method list is constructed according to the MRO algorithm, and the constructed method list is saved in the method dictionary; when calling the model method, the method list is loaded from the method dictionary according to the method name, and matched to the real Java method for calling; when methods of the same Java class call each other, the methods of all models are intercepted by the dynamic proxy of the Java class, and it is judged in the interceptor whether the current method should be executed or needs to be calculated according to the model MRO, and the call is made according to the judgment result.
2. A computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program is executed by a processor to implement the model multiple inheritance method implemented using the Java language as claimed in claim 1.
3. A model multiple inheritance device implemented using Java language, characterized in that: include: Processor and memory; The memory is used to store computer programs; The processor is connected to the memory and is used to execute the computer program stored in the memory, so that the model multiple inheritance device implemented using the Java language executes the model multiple inheritance method implemented using the Java language as claimed in claim 1.
Citation Information
Patent Citations
Automatic registration method, device and system for bullet screen events in Android and storage medium
CN107450922A
Method for managing annotation inheritance
US7840939B1