Example for Overridding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Example for Overridding

    Hi,

    Could anybody help me with a sample code for function overidding.

    Thanks & Regards
    PSB
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by psbasha
    Hi,

    Could anybody help me with a sample code for function overidding.

    Thanks & Regards
    PSB
    Just like you'd expect:
    Code:
    class SuperClass(object):
        def func1(self):
            print 1234
    
    class SubClass(SuperClass):
        def func1(self):
            print 5678
            SuperClass.func1(self)
    
    sc = SubClass()
    sc.func1()

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      Originally posted by bartonc
      Just like you'd expect:
      Code:
      class SuperClass(object):
          def func1(self):
              print 1234
      
      class SubClass(SuperClass):
          def func1(self):
              print 5678
              SuperClass.func1(self)
      
      sc = SubClass()
      sc.func1()
      Hi,

      Thanks for the example.

      In case of C++ we have a Pointer concept and we can play with Base Class Object and Derived class objects to access the common methods or overriden methods thru thier Objects.Is it possible in Python to get the same functionality by assigning the derive class object to the base class and get access to the methods of the derived classes.As shown in the below example.If possible could you please help me in getting the sample code.

      Example in C++ :

      class A {
      virtual void f() { cout << "Class A" << endl; }
      };

      class B:public A {
      void f(int) { cout << "Class B" << endl; }
      };

      class C:public B {
      void f() { cout << "Class C" << endl; }
      };

      int main() {
      B b; C c;
      A* pa1 = &b;
      A* pa2 = &c;
      // b.f();
      pa1->f();
      pa2->f();
      }
      The following is the output of the above example:

      Class A
      Class C

      Regards
      PSB

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        In this case, BasisTransToGlo bal() is based on Plane3D which is based on object. BasisTransToGlo bal() inhehits all of Plane3d()'s methods. Plane3D() overloaded __repr__ is overridden.
        Code:
        class BasisTransToGlobal(Plane3D):
            
            def __init__(self, vN, vA, vB, vR=Point(0,0,0)):
                self.vN = vN
                self.vA = vA
                self.vB = vB
                self.vR = vR
                # local basis aligned with plane defined
                Plane3D.__init__(self, vN, vA, vB)
                
                if chk_type([vR, ]):
                    
                    if self.N_len > 0.0:
                        
                        # Unit vector normal to defined plane, local basis 'Z'
                        self.N = self.N_uv
                        
                        # Unit vector between vN and vA, local basis 'X'
                        self.A = self.d0
                        
                        # Unit cross product vector, local basis 'Y'
                        self.B = self.cross_product(self.N, self.A)
                        
                        # global coordinate vector
                        self.R = self.translate(vR.x, vR.y, vR.z) + vN
                        
                    else:
                        Warning("The points are collinear ***INVALID***")
                        self.R = None
                else:
                    raise TypeError, "Arguments must be <type 'point'>"
        
            def translate(self, X, Y, Z):
                ....................................................................................
                # global coordinate vector with respect to local basis origin
                return Point(R1.x*M, R1.y*M, R1.z*M)
        
            def __repr__(self):
                return 'BasisTransToGlobal(Point(%s), Point(%s), Point(%s), Point(%s))' % (self.vN, self.vA, self.vB, self.vR)

        Comment

        Working...