[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [PERL] Rod i objekterne.



Michael Bernhard Sørensen <sslug@sslug> writes:

> #!/usr/bin/perl -w
>
> package Universe;
> sub new {
>     my $type = shift;    # Its own new object name
>     my $self = {};       # No super class
>     bless $self, $type;  #
> }
> sub Display {
>     print("This is the universe.\n");
> }
>
> package StellarObject;
> @ISA = Universe;
> sub new {
>     my $type = shift;         # Its own new object name
>     $self = Universe->new; # Setting super class

Her tror perl at du refererer variablen $self i pakken StellarObject.

>     $self->{Name} = shift;
>     $self->{Xpos} = 0;
>     $self->{Ypos} = 0;
>     $self->{Zpos} = 0;
>     bless $self, $type;       # ???
> }
> sub PrintPos{

Her glemmer du at hente $self fra @_ og derfor ...

>     print("Name: ",  $self->{Name}, "\n");

bruger perl den globale vairabel $self fra pakken StellarObject.

>     print("Xpos = ", $self->{Xpos}, "\n");
>     print("Ypos = ", $self->{Ypos}, "\n");
>     print("Zpos = ", $self->{Zpos}, "\n");
> }
> sub SetPos {
>     my $self = shift;
>     $self->{Xpos} = shift;
>     $self->{Ypos} = shift;
>     $self->{Zpos} = shift;
> }
>
> package main;
> $universe = Universe->new();
> $universe->Display();
> $sun = StellarObject->new("Sun");
> $sun->SetPos(0, 0, 0);
> $sun->PrintPos();
> $earth = StellarObject->new("Earth");
> $earth->SetPos(150000000, 0, 0);
> $earth->PrintPos();
> $sun->PrintPos();

Tilsammen gør de to fejl at printPos vil udskrive positionen for den
sidst konstruerede ting.

-- 
 Peter Makholm     |       Why does the entertainment industry wants us to
 sslug@sslug |      believe that a society base on full surveillance
 http://hacking.dk |                                               is bad?
                   |                       Do they have something to hide?


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-11-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *