[英]Override a method of another class using Java Reflections
我有一个Java项目,其中有2个课程。 我需要使用Java反射来首先打印mywork.java
类中由ProjectAccount.java
的构造函数设置的默认值,然后需要重写toString()
方法以从mywork.java
类传递值并打印它们。
我能够使用Java反射来打印默认的构造函数集值。 但是尝试覆盖和打印带有参数的toString()
方法时出现错误。
ProjectAccount.java
package relive;
public class ProjectAccount {
private String projectAccountID;
private double budget;
public int noOfProjects;
public ProjectAccount(){
this.projectAccountID = "MARTINDAWS-BillAnalyzer-001";
this.budget = 120000.00;
this.noOfProjects = 10;
}
@Override
public String toString(){
return "Project Accoutn ID = " + projectAccountID + " , Project Budget = "+budget + " , No of Projects = "+ noOfProjects;
}
}
mywork.java
package relive;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class mywork {
public static void main(String[] args) {
try{
Class<?> clazz = Class.forName("relive.ProjectAccount");
Constructor<?> constr = clazz.getDeclaredConstructor(null);
Method mets = clazz.getDeclaredMethod("toString", null);
Object obj = constr.newInstance(new Object[] {});
//Print Default values
System.out.println(mets.invoke(obj, null));
String mod_projectAccountID="ORPT-BT-EMP-DEV";
double mod_budget = 2200000.0;
int mod_noOfProjects = 20;
//Print new passed values using the overridden method
System.out.println(mets.invoke(obj, mod_projectAccountID,mod_budget, mod_noOfProjects));
}
catch(ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e){
e.printStackTrace();
}
}
public static String toString(String mod_projectAccountID, double mod_budget, int mod_noOfProjects){
return "Project Accoutn ID = " + mod_projectAccountID + " , Project Budget = "+mod_budget + " , No of Projects = "+ mod_noOfProjects;
}
}
输出和错误
我猜我遇到了这个错误,因为我还没有重写toString()
方法。 任何有关如何覆盖和打印此内容的建议将不胜感激。
那里有一些问题。
mets
,它引用了ProjectAccount
类 mywork
不是ProjectAccount
的子类 子类重写方法的能力允许类从行为“足够接近”的超类继承,然后根据需要修改行为。 覆盖方法与其覆盖的方法具有相同的名称,数量和参数类型,并且返回类型相同。 重写方法还可以返回重写方法返回的类型的子类型。 此子类型称为协变返回类型。
package de.me.example;
public class SuperType {
@Override
public String toString() {
return "SuperType";
}
}
package de.me.example;
public class SubType extends SuperType {
// this annatotion is syntactic sugar nothing more,
//it behaves the same without it
@Override
public String toString() {
return "SubType";
}
}
我刚刚修改了mywork.java
类,以直接访问ProjectAccount.java
类变量并进行设置。
我访问了声明的字段,并根据变量的名称或类型修改了声明的变量,并调用了相同的toString()
方法,而不覆盖toString()
方法。
mywork.java
package relive;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class answer_a {
public static void main(String[] args) {
try{
Class<?> clazz = Class.forName("relive.ProjectAccount");
Constructor<?> constr = clazz.getDeclaredConstructor(null);
Method mets = clazz.getDeclaredMethod("toString", null);
Object obj = constr.newInstance(new Object[] {});
//Print Default values
System.out.println(mets.invoke(obj, null));
String mod_projectAccountID= "ORPT-BT-EMP-DEV";
double mod_budget = 2200000.0;
int mod_noOfProjects = 20;
//Method that accesses the Fields and modifies them with the new variables
Field [] fields = clazz.getDeclaredFields();
for(int y=0; y<fields.length; y++){
fields[y].setAccessible(true);
if(fields[y].getName() == "projectAccountID"){
fields[y].set(obj, mod_projectAccountID);
}
else if(fields[y].getType().getSimpleName() == "double"){
fields[y].set(obj, mod_budget);
}
else if (fields[y].getType().getSimpleName() == "int"){
fields[y].set(obj, mod_noOfProjects);
}
}
System.out.println(mets.invoke(obj, null));
}
catch(ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e){
e.printStackTrace();
}
}
}
输出量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.