[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
 

[PERL] Rod i objekterne.



Hej derude.

Jeg har lidt bøvl med et objekt. Her er koden:

#!/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
    $self->{Name} = shift;
    $self->{Xpos} = 0;
    $self->{Ypos} = 0;
    $self->{Zpos} = 0;
    bless $self, $type;       # ???
}
sub PrintPos{
    print("Name: ",  $self->{Name}, "\n");
    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();

Når jeg i den sidste linie vil skrive positionen på $sun, så får jeg $earth ud 
på skærmen. Hvorfor får jeg det? Jeg troede lige at jeg var begyndt at fatte 
klasser i Perl. :-( Det lugter af noget reference der er gået galt, men jeg 
kan ikke gennemskue det.

Kan nogen komme med en konstruktiv kritik?

Mvh Michael


 
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] *