in reply to Error in Inheritance Class
It's not entirely clear to me what you're trying to do, but the errormessage you get is correct. There is no method "useChild1" in the Parant class.
Is what you try to do:
PARENT CLASS
(Parent.pm)
package Parent; use strict; use warnings; use Child; sub new { my $class = shift; my $this = {}; bless $this, $class; $this->setChild( Child->new( @_ ) ); return $this; } sub setChild { my ( $this, $child ) = @_; if ( defined $child ) { $this->{_child} = $child; return 1; } return undef; } sub getChild { my $this = shift; return $this->{_child}; } sub setChildValue1 { my ( $this, $value ) = @_; if ( defined $value ) { return $this->getChild()->setValue1( $value ); } return undef; } sub getChildValue1 { my $this = shift; return $this->getChild()->getValue1(); } sub setChildValue2 { my ( $this, $value ) = @_; if ( defined $value ) { return $this->getChild()->setValue2( $value ); } return undef; } sub getChildValue2 { my $this = shift; return $this->getChild()->getValue2(); } 1;
package Child; use strict; use warnings; sub new { my ( $class, $value1, $value2 ) = @_; my $this = {}; bless $this, $class; $this->setValue1( $value1 ); $this->setValue2( $value2 ); return $this; } sub setValue1 { my ( $this, $value ) = @_; if ( defined $value ) { $this->{_value1} = $value; return 1; } return undef; } sub getValue1 { my $this = shift; return $this->{_value1}; } sub setValue2 { my ( $this, $value ) = @_; if ( defined $value ) { $this->{_value2} = $value; return 1; } return undef; } sub getValue2 { my $this = shift; return $this->{_value2}; } 1;
In other words, access properties of the child-object via the parent?use Parent; use strict; use warnings; my $parent = new Parent( 'value1', 'value2' ); my $child = $parent->getChild(); print "Values 1 and two from the child are:\n1....", $child->getValue1(),"\n2....", $child->getValue2(),"\n"; print "Values 1 and two from the child via the parent are:\n1....", $parent->getChildValue1(),"\n2....", $parent->getChildValue2(),"\n";
|
|---|