oop中子类的超类访问方法 [英] superclass accesing methods of a sublass in oop

查看:58
本文介绍了oop中子类的超类访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,超类是否有可能访问继承子类的方法,例如在 Java 中?

I was wondering, is it possible that a superclass to access the methods of a inherited subclass, like for example in Java?

我知道子类可以覆盖甚至实现,在抽象类的情况下,超类的方法,但上面提到的问题可能吗?

I know that a subclass can override and even implements, in case of abstract classes, the methods of the superclass, but the question mentioned above is possible?

谢谢

推荐答案

我会尽量按照他们在大学时向我解释的那样解释.

I' ll try to explain as they explained to me at university.

你有一个参考:

Object o = new Object()

  • 他的静态类型(ST)是 Object :这是他自己的类型,永远不会改变.

    • His static type(ST) is Object : this is his own type and never changes.

      他的动态类型(DT)也是Object(在这种情况下):引用指向一个Object类型的对象,但它可以改变.

      His dynamic type(DT) is also Object(in this case): the reference point to an object of type Object, but it can change.

      例如如果我写:

      Object o = new String("abc") // now his ST == Object but the DT == String
      

    • 话虽如此:

      • 向上转换总是被允许的:考虑两个引用 sr.如果 ST(r) <= ST(s)(r静态类型是,在层次结构中,小于或等于s静态类型)

      • Upcasting is always permitted: consider two references s and r. the assignment s=r compile and execute always if ST(r) <= ST(s) (the static type of r is, in the hierarc less or equals to the static type of s)

      例如:

      class A { }
      class B extends A { }
      ...
      A a = new A(); B b = new B();
      a = b  // OK, upcast
      

    • 向下转换:在编译时,从类型X向下转换到类型Y总是合法的,如果XY 属于层次结构.考虑参考s.我想将 s 转换为 C 类型,因此如果 C <= TS(s) 如果我将转换为:C r,它将始终编译= (C)s

    • Downcasting: at compile-time it is always legal to downcast from a type X to a type Y if X and Y belong to hierarchy. Consider the reference s. I want to cast s to a type C, so if C <= TS(s) it will always compile if I do the cast as : C r = (C)s

      例如:

      class A { }
      class B extends A { }
      class C extends A { }
      ...
      A a = new A(); B b = new B();
      C c = new C();
      ...
      b = c // ILLEGAL
      b = (B)a // OK at compile-time but maybe at run-time it is not!
      

      如果向下转型失败,当我们运行我们的应用程序时,Java 会引发异常.否则成功.

      When we run our application if the downcast fails, Java raise an Exception. Otherwise it success.

      要正确地低头:考虑一个引用 ref 并且我们想要转换为 C 类型.因此,如果 DT(ref) <= C <= ST(ref) ,则向下转换将成功.
      并且向下转换将获得为:C ref2 = (C)ref

      To downcast correctly: consider a reference ref and we want to cast to a type C. So a downcast will success if DT(ref) <= C <= ST(ref) .
      And the downcast will be obtained as: C ref2 = (C)ref

      例如:

      // I suggest to write the hierarchy in a piece of paper and 
      // try the rules before coding.
      
      class A { }
      class B extends A { }
      class C extends A { }
      class D extends B { }
      ...
      
      A a = new A(); B b = new B();
      C c = new C(); D d = new D();
      A r = new B();
      A s = new D();
      a = b; // OK, upcast
      a = d; // OK, upcast
      /* b = c; */ // ILLEGAL
      b = (B)r; // OK, downcast
      d = (D)r; // downcast: it compiles, but fails at run-time
      d = (D)s; // OK, downcast
      /* b = s; */ // ILLEGAL
      /* d = (D)c; */ // ILLEGAL
      b = (B)s; // OK, downcast
      b = (D)s; // OK, downcast
      

    • PS:写错了请见谅,写的有点匆忙.

      PS: please forgive if I made some mistake but I wrote a bit in a hurry.

      这篇关于oop中子类的超类访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆