I wonder i placment a super in different possitions makes any difference to functionality.
我不知道我在不同的位置上扮演一个超级能否对功能产生任何影响。
public void test(){
super.test()
// my code
}
and
public void test(){
//my code
super.test()
}
Is it exactly the same?
它完全一样吗?
3 个解决方案
#1
2
No it's not the same. In the first example, the method of the super class is executed first while in your second example it's executed in last. This little example will demonstrate this :
不,它不一样。在第一个例子中,首先执行超类的方法,而在第二个例子中,它在最后执行。这个小例子将证明这一点:
Consider this :
考虑一下:
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
new Child().test(5);
}
}
class Parent {
public void test(int a){
System.out.println(a);
}
}
class Child extends Parent{
@Override
public void test(int a){
super.test(a);
a++;
}
}
It will print 5.
它将打印5。
However if I place the super call at the end of the overriden test method,
但是,如果我在覆盖测试方法结束时放置超级调用,
class Child extends Parent{
@Override
public void test(int a){
a++;
super.test(a);
}
}
it will print 6.
它将打印6。
#2
2
If you call super class method test()
before your code, the super class method will be called before your subclass method code named as my code
executed. Order of execution will change according to your order which you have placed in the method.
如果在代码之前调用超类方法test(),那么在您的子类方法代码被命名为我的代码执行之前,将调用超类方法。执行顺序将根据您在方法中的顺序而变化。
#3
1
No, it is not. The code in your test() method is run sequentially.
不它不是。 test()方法中的代码按顺序运行。
Thus, calling
new B().test();
new C().test();
on
class A {
public void test() {
System.out.print("A");
}
}
class B extends A {
public void test() {
System.out.println("B");
super.test();
}
}
class C extends A {
public void test() {
super.test();
System.out.println("C");
}
}
will result in the output:
将导致输出:
BA
AC
#1
2
No it's not the same. In the first example, the method of the super class is executed first while in your second example it's executed in last. This little example will demonstrate this :
不,它不一样。在第一个例子中,首先执行超类的方法,而在第二个例子中,它在最后执行。这个小例子将证明这一点:
Consider this :
考虑一下:
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
new Child().test(5);
}
}
class Parent {
public void test(int a){
System.out.println(a);
}
}
class Child extends Parent{
@Override
public void test(int a){
super.test(a);
a++;
}
}
It will print 5.
它将打印5。
However if I place the super call at the end of the overriden test method,
但是,如果我在覆盖测试方法结束时放置超级调用,
class Child extends Parent{
@Override
public void test(int a){
a++;
super.test(a);
}
}
it will print 6.
它将打印6。
#2
2
If you call super class method test()
before your code, the super class method will be called before your subclass method code named as my code
executed. Order of execution will change according to your order which you have placed in the method.
如果在代码之前调用超类方法test(),那么在您的子类方法代码被命名为我的代码执行之前,将调用超类方法。执行顺序将根据您在方法中的顺序而变化。
#3
1
No, it is not. The code in your test() method is run sequentially.
不它不是。 test()方法中的代码按顺序运行。
Thus, calling
new B().test();
new C().test();
on
class A {
public void test() {
System.out.print("A");
}
}
class B extends A {
public void test() {
System.out.println("B");
super.test();
}
}
class C extends A {
public void test() {
super.test();
System.out.println("C");
}
}
will result in the output:
将导致输出:
BA
AC