[英]is this possible in java or any other programming language
public abstract class Master
{
public void printForAllMethodsInSubClass()
{
System.out.println ("Printing before subclass method executes");
System.out.println ("Parameters for subclass method were: ....");
}
}
public class Owner extends Master {
public void printSomething () {
System.out.println ("This printed from Owner");
}
public int returnSomeCals ()
{
return 5+5;
}
}
Without messing with methods of subclass...is it possible to execute printForAllMethodsInSubClass()
before the method of a subclass gets executed? 在不弄乱子类方法的情况下...是否可以在执行子类方法之前执行
printForAllMethodsInSubClass()
?
update: 更新:
Using AspectJ/Ruby/Python...etc Would it also be possible to print the parameters? 使用AspectJ / Ruby / Python ... etc还可以打印参数吗? Above code formatted below:
上面的代码格式如下:
public abstract class Master
{
public void printForAllMethodsInSubClass()
{
System.out.println ("Printing before subclass method executes");
}
}
public class Owner extends Master {
public void printSomething (String something) {
System.out.println (something + " printed from Owner");
}
public int returnSomeCals (int x, int y)
{
return x+y;
}
}
AspectJ can provide this functionality for you, but it's a separate compilation step and some extra libraries involved. AspectJ可以为您提供此功能,但这是一个单独的编译步骤,并且涉及一些额外的库。
public aspect ServerLogger {
pointcut printSomething ();
before(): printSomething()
{
(Master)(thisJoinPointStaticPart.getTarget()).printForAlMethodsInSubClass();
}
}
The Eclipse Project provides a great implementation of AspectJ that integrates nicely with Eclipse and Maven. Eclipse项目提供了一个很好的AspectJ实现,可以很好地与Eclipse和Maven集成。 There's a boatload of great documentation available for it, and a lot of really good material for it here on StackOverflow.
有大量出色的文档可供使用,而StackOverflow上提供了很多非常好的材料。
[update] [更新]
To access parameter info, you can use the 要访问参数信息,您可以使用
thisJoinPoint.getSignature();
method to access information about the function being called if the returned Object is an instance of MethodSignature , you can use Signature.getParameterNames() to access the parameters to the function being called. 如果所返回的Object是MethodSignature的实例,则方法用于访问有关被调用函数的信息,您可以使用Signature.getParameterNames()来访问被调用函数的参数。 You'd have to use a bit of reflection to actually get at the values, I think - AspectJ doesn't seem to handle this for you.
我认为您必须使用一些反射才能真正得出这些值-AspectJ似乎并没有为您解决这个问题。 I'd have to actually do some experimentation to get some working code for you.
我实际上必须做一些实验才能为您获得一些工作代码。
To answer the "any other programming language": It's easily possible in Ruby: 回答“任何其他编程语言”:在Ruby中很容易做到:
class Master
REDEFINED = []
def printForAllMethodsInSubClass
puts 'Printing before subclass method executes'
end
def self.method_added(meth)
if self < Master and not Master::REDEFINED.include? meth
new_name = "MASTER_OVERRIDE_#{meth}".intern
Master::REDEFINED.push meth, new_name
alias_method new_name, meth
define_method(meth) {|*args| printForAllMethodsInSubClass; send(new_name, *args)}
end
end
end
You could also make a proxy declaration method to use in subclasses: 您还可以制作一个代理声明方法以在子类中使用:
class Master
def printForAllMethodsInSubClass
Printing before subclass method executes
end
def self.master_method(name)
define_method(name) {|*args| printForAllMethodsInSubClass; yield *args}
end
end
class Owner
master_method(:print_something) do
puts "This was printed from Owner"
end
end
(This approach would also translate very naturally to Python decorators.) (这种方法也会很自然地转化为Python装饰器。)
这在面向方面的编程语言(例如AspectJ)中是可能的。
In Python you can accomplish this using meta classes, here's a small example. 在Python中,您可以使用元类来完成此操作,这是一个小示例。 You can probably make it more elegantly but it is just to make the point
您可能可以使其更加优雅,但这仅是为了说明问题
import types
class PrintMetaClass(type):
def __init__(cls, name, bases, attrs):
# for every member in the class
override = {}
for attr in attrs:
member = attrs[attr]
# if it's a function
if type(member) == types.FunctionType:
# we wrap it
def wrapped(*args, **kwargs):
print 'before any method'
return member(*args, **kwargs)
override[attr] = wrapped
super(PrintMetaClass, cls).__init__(name, bases, attrs)
for attr in override:
setattr(cls, attr, override[attr])
class Foo:
__metaclass__ = PrintMetaClass
def do_something(self):
print 2
class Bar(Foo):
def do_something_else(self):
print 3
In this example, the PrintMetaClass
gets in the way of the creation of the Foo
class and any of its subclasses redefining every method to be a wrapper of the original and printing a given message at the beginning. 在此示例中,
PrintMetaClass
了Foo
类及其任何子类的创建,将每个方法重新定义为原始方法的包装,并在开始时打印给定消息。 The Bar
class receives this aspect-like behavior simply by inheriting from Foo
which defines its __metaclass__
to be PrintMetaClass
. Bar
类仅通过继承自Foo
来接收类似方面的行为, __metaclass__
将其__metaclass__
定义为PrintMetaClass
。
Metaclasess in OOP: OOP中的中继词:
Metaclasses in python: python中的元类:
Besides aspect oriented programming have a look at Template Method Pattern, http://en.wikipedia.org/wiki/Template_method_pattern . 除了面向方面的编程外,还可以查看模板方法模式http://en.wikipedia.org/wiki/Template_method_pattern 。
In short: the parent class have an abstract method, which subclasses have to implement, this abstract method is called by a method in the parent class where put your printouts or whatever necessary statements. 简而言之:父类具有一个抽象方法,子类必须实现该抽象方法,该抽象方法由父类中的方法调用,该方法放置您的打印输出或任何必要的语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.