Class and Sub-CLass

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

    #1

    Class and Sub-CLass

    Hi,

    Is it possible to use CLasses inside another class instead of deriving the classes?

    Like in C++
    Class Y{

    public :

    MethodY1();
    MethodY2();
    }

    Class X{

    public :

    Class Y ObjY;

    MethodX1();
    MethodX2();


    }

    Thanks in advacne
    PSB
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by psbasha
    Hi,

    Is it possible to use CLasses inside another class instead of deriving the classes?

    Like in C++

    Thanks in advacne
    PSB
    Yes. It's perfectly legal in the syntax of the language.
    With Python it is possible to do nested classes :
    Code:
    class Test(object):
    
        def __init__(self):
    	self.y = 0
    
        class TestInner(object):
    
    	def __init__(self):
    	    self.x = 0
    
    	def someFunction(self):
    	    print "Hello from Innerclass"
    
    someObject = Test.TestInner()
    
    someObject.someFunction()

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Yes. It's perfectly legal in the syntax of the language.
      With Python it is possible to do nested classes :
      Code:
      class Test(object):
      
          def __init__(self):
      	self.y = 0
      
          class TestInner(object):
      
      	def __init__(self):
      	    self.x = 0
      
      	def someFunction(self):
      	    print "Hello from Innerclass"
      
      someObject = Test.TestInner()
      
      someObject.someFunction()
      The above example is discussed in this thread.

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #4
        Originally posted by bartonc
        The above example is discussed in this thread.
        Sorry I have wrongly mentioned in the Subject.It is declaring the class as a member variable inside the another class ( not nested class)

        Declaring the Class variable inside another classs

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by psbasha
          Sorry I have wrongly mentioned in the Subject.It is declaring the class as a member variable inside the another class ( not nested class)

          Declaring the Class variable inside another classs
          Check out Python>Code sub forum. Temporary links are provided in the announcement and in the sticky at the top of the forum. I give lots of examples with good titles on those threads.

          Comment

          Working...