Java 如果只有类的对象,如何重写类的行为?
这个问题问起来可能很愚蠢,但请帮我解决这个问题。 我需要重写一个类的行为,但我将只得到它的对象。 下面是代码示例 我想在所有地方使用方法Util.newChildComponent(字符串id)本身Java 如果只有类的对象,如何重写类的行为?,java,Java,这个问题问起来可能很愚蠢,但请帮我解决这个问题。 我需要重写一个类的行为,但我将只得到它的对象。 下面是代码示例 我想在所有地方使用方法Util.newChildComponent(字符串id)本身 class ParentComponent{ public ParentComponent(String id){} protected void someBehavior(){} } class ChildComponent extends ParentComponent{
class ParentComponent{
public ParentComponent(String id){}
protected void someBehavior(){}
}
class ChildComponent extends ParentComponent{
public ChildComponent(String id){
super(id);
}
protected void childsBehavior(){}
}
public class Util {
public static ParentComponent newChildComponent(String id)
{
ParentComponent fc = new ChildComponent(id);
// Initialize the object, and performs some common configuration.
// Performs different operations on the generated child object.
// The child class has many members apart from childsBehavior().
return fc;
}
}
// The above classes belongs to a jar, so I cannot edit the code.
// I need to use Util.newChildComponent(String id)
// But I need to override the behavior as mentioned below.
public void f1()
{
// TODO: How to override childsBehavior() method using the object
// I have it in pc? Is it possible?
// Util.newChildComponent(id) method decorates the ChildComponent
// So I need to use the same object and just override childsBehavior() method only.
ParentComponent pc = Util.newChildComponent("childId");
// I need to achieve the result below
/*
ParentComponent pc = new ChildComponent(id){
@Override
protected void childsBehavior(){
super.someBehavior();
// Do the stuff here.
}
}; */
}
提前感谢。看看Java的动态代理。这应该允许您通过创建调用处理程序来处理对方法的调用,从而在运行时更改行为。如果需要,您甚至可以将其转发到原始方法。看看Java的动态代理。这应该允许您通过创建调用处理程序来处理对方法的调用,从而在运行时更改行为。如果需要,您甚至可以将其转发到原始方法。声明一个新类,该类使用您要更改的方法扩展该类
public class MyClass extends ChildComponent {
@Override
protected void childsBehavior()
{
.....
}
}
然后在您的代码中可以使用casting,这是由于继承,我们可以享受的特性
MyClass pc=(MyClass)Util.newChildComponent(“childId”)
但是你必须像这样创建一个新的对象
MyClass pc = new MyClass("childId");
并手动执行在Util
上完成的配置。除非扩展Util
并创建MyClass
对象并将配置代码复制到该对象的方法
现在
pc
将拥有ParentComponent
和ChildComponent
的所有方法,但是childsBehavior
将是您声明的。声明一个新类,该类使用您要更改的方法扩展该类
public class MyClass extends ChildComponent {
@Override
protected void childsBehavior()
{
.....
}
}
然后在您的代码中可以使用casting,这是由于继承,我们可以享受的特性
MyClass pc=(MyClass)Util.newChildComponent(“childId”)
但是你必须像这样创建一个新的对象
MyClass pc = new MyClass("childId");
并手动执行在Util
上完成的配置。除非扩展Util
并创建MyClass
对象并将配置代码复制到该对象的方法
现在
pc
将拥有ParentComponent
和ChildComponent
的所有方法,但是childsBehavior
将是您声明的。如果您不想更改使用Util
实例化所需对象的代码,您可以使用所需的行为重写Util
类(即使用重写的方法实例化扩展ChildComponent
的类),只需记住将其放入项目中同名的包中(以便其客户端的导入不会更改)。这样,其他代码就不会发生任何更改。如果您不想更改使用Util
实例化所需对象的代码,可以使用所需的行为重写Util
类(即使用重写方法实例化扩展ChildComponent
的类),只需记住将其放在项目中同名的包中(以便其客户机的导入不会改变)。这样,其他代码就不会发生任何更改。听起来像是在寻找一个新代码
这是他优秀作品的一个用途
static class ParentComponent {
protected void someBehavior() {
System.out.println("ParentComponent someBehavior");
}
}
static class ChildComponent extends ParentComponent {
private ChildComponent(String id) {
}
protected void childsBehavior() {
System.out.println("childsBehavior");
}
}
public static class Util {
public static ParentComponent newChildComponent(String id) {
ParentComponent fc = new ChildComponent(id);
// Initialize the object, and performs some common configuration.
return fc;
}
}
interface Parent {
void someBehavior();
}
// The adaptor.
public static class Adapter implements Parent {
final ParentComponent parent;
private Adapter(ParentComponent pc) {
this.parent = pc;
}
// Override the parent behaviour.
public void someBehavior() {
System.out.println("Adapter invoke someBehaviour()");
parent.someBehavior();
System.out.println("Adapter finished someBehaviour()");
}
}
public void test() {
ParentComponent pc = Util.newChildComponent("childId");
Parent adapted = DynamicObjectAdapterFactory.adapt(pc, Parent.class, new Adapter(pc));
adapted.someBehavior();
}
印刷品:
Adapter invoke someBehaviour()
someBehavior
Adapter finished someBehaviour()
听起来你在找一个新的工作 这是他优秀作品的一个用途
static class ParentComponent {
protected void someBehavior() {
System.out.println("ParentComponent someBehavior");
}
}
static class ChildComponent extends ParentComponent {
private ChildComponent(String id) {
}
protected void childsBehavior() {
System.out.println("childsBehavior");
}
}
public static class Util {
public static ParentComponent newChildComponent(String id) {
ParentComponent fc = new ChildComponent(id);
// Initialize the object, and performs some common configuration.
return fc;
}
}
interface Parent {
void someBehavior();
}
// The adaptor.
public static class Adapter implements Parent {
final ParentComponent parent;
private Adapter(ParentComponent pc) {
this.parent = pc;
}
// Override the parent behaviour.
public void someBehavior() {
System.out.println("Adapter invoke someBehaviour()");
parent.someBehavior();
System.out.println("Adapter finished someBehaviour()");
}
}
public void test() {
ParentComponent pc = Util.newChildComponent("childId");
Parent adapted = DynamicObjectAdapterFactory.adapt(pc, Parent.class, new Adapter(pc));
adapted.someBehavior();
}
印刷品:
Adapter invoke someBehaviour()
someBehavior
Adapter finished someBehaviour()
因此,您希望覆盖
childsBehavior
,但也要使用Util.newChildComponent
来创建实例?查看此答案,ChildComponent是您无法修改的组件?@milez所有三个ParentComponent,ChildComponent&Util是否要覆盖childsBehavior
并使用Util.newChildComponent
创建实例?请查看此答案,ChildComponent是您无法修改的组件?@milez所有三个父组件、ChildComponent&Utilal通过此操作,我认为在这里编写包装器是更合适的方法。因为它应该是完全相同的类,除了1函数的行为,而不是父子关系。这将在运行时返回一个ClassCastException,因为您试图向下转换到另一个类。虽然这是可行的,但我认为在这里编写包装器是更合适的方法。因为它应该是完全相同的类,除了1函数的行为,而不是父子关系。这将在运行时返回ClassCastException,因为您试图向下转换到不同的类。。