Skocz do zawartości
swety

How create OOP object delphi ?

Rekomendowane odpowiedzi

How create (OOP) object ?

 

solider1

solider2

solider3

solider4

solider(i)

...

...

type

 

TSolider = class

end;

 

var

solider:TSolider;

 

implementation

 

....

 

My ask "How create too many solider with ID be in name or in the object" ? I want work on Solider and be all the solider ....

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

You must create some container for TSoldier objects, like array, list or map. I don't know Delphi so i will show example in C#. I hope that someone can translate code below. :)

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    // Main program class
    class Program
    {
        static void Main( string[] args )
        {
            // Lets check our Squadron class - simple container fo datatype Soldier


            // Make new squadron...
            Squadron alpha = new Squadron();

            // Add one soldier
            Soldier Jeremy = alpha.Add();
            Jeremy.name = "Jeremy";
            Jeremy.x = 10;
            Jeremy.y = 25;

            // Add one soldier with use of different constructor
            Soldier Maron = alpha.Add( "Maron", 20, 100 );

            // Add some other soldiers
            for ( int i = 0; i < 10; i++ )
            {
                alpha.Add();
            }

            // How many soldiers are in alpha squadron ?
            Console.WriteLine( alpha.Soldiers.Length );

            // Test of access to soldiers by iterating squadron with loop foreach
            foreach ( Soldier soldier in alpha.Soldiers )
            {
                Console.WriteLine( soldier.name );
            }

            // Test of indexer ( array like access to soldiers in squadron by using id as index )
            Console.WriteLine( alpha[2].name );

            Console.ReadKey();
        }
    }

    /// <summary>
    /// Our superb class for simple stroing and manipulating soldiers in squadron
    /// </summary>
    public class Squadron
    {
        /// <summary>
        /// Private field, id counter for new soldiers
        /// </summary>
        private int id = 0;

        /// <summary>
        /// Private field, List collection (like ds_list in Game Maker) for containing all soldiers in squadron
        /// </summary>
        private List<Soldier> soldiers = new List<Soldier>();

        /// <summary>
        /// Property that returns array of soldiers (for iteration by loops like foreach etc.)
        /// </summary>
        public Soldier[] Soldiers
        {
            get { return soldiers.ToArray(); }
        }

        /// <summary>
        /// Makes an indexer, for access to soldier in array style ( squadron[soldier_id] )
        /// </summary>
        /// <param name="id">id number</param>
        /// <returns>Soldier object</returns>
        public Soldier this[int id]
        {
            get
            {
                foreach ( Soldier soldier in soldiers )
                {
                    if ( soldier.Id == id )
                        return soldier;
                }
                return null;
            }
        }

        /// <summary>
        /// Create another soldier, add him to squadron (hidden List structure)
        /// and return soldier object
        /// </summary>
         /// <returns>Soldier</returns>
        public Soldier Add()
        {
            id++;
            Soldier soldier = new Soldier( id );
            soldiers.Add( soldier );
            return soldier;
        }

        /// <summary>
        /// Create another soldier, add him to squadron (hidden List structure)
        /// and return soldier object
        /// </summary>
        /// <param name="name">Name</param>
        /// <param name="x">X</param>
        /// <param name="y">Y</param>
        /// <returns>Soldier</returns>
        public Soldier Add( string name, int x, int y )
        {
            id++;
            Soldier soldier = new Soldier( id );
            soldiers.Add( soldier );
            soldier.name = name;
            soldier.x = x;
            soldier.y = y;
            return soldier;
        }

        /// <summary>
        /// Remove soldier from squadron
        /// </summary>
        /// <param name="id">Soldier id</param>
        /// <returns>Return true if soldier existed</returns>
        public bool Remove( int id )
        {
            foreach ( Soldier soldier in soldiers )
            {
                if ( soldier.Id == id )
                {
                    soldiers.Remove( soldier );
                    return true;
                }
            }

            return false;
        }
    }

    /// <summary>
    /// Simple class for creating Soldier objects
    /// </summary>
    public class Soldier
    {
        private int _id = 0;

        public string name = "noname";
        public int x = 0;
        public int y = 0;

        /// <summary>
        /// Property Id (cannot be changed)
        /// </summary>
        public int Id
        {
            get { return _id; }
        }

        /// <summary>
        /// Constructor, makes another soldier
        /// </summary>
        /// <param name="id">Id number</param>
        public Soldier( int id )
        {
            this._id = id;
        }
    }
}

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

You want to create class TSoldier to represent many soldiers or do you want to create soldiers' collection?

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

@Ranmus & BeWuO:

Solider, not Soldier xD.

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

I want create identical object

Source one OBJECT Tsolider

And form out of him create

Solider1

Sloder2

Solider3

Solider(i)

 

or

 

All object will be name Solider

work in

With(Solider.id=1..3)

begin

 

end;

 

Help me ! Help me ! please ... i am novice :DDD

Ranmus You are crazy xD C++ and C# is hardcore for me .D

grafcf1.jpg

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

You just write new class. For example TSoldier and then you have array of TSoldiers and then, when you want to create new instance od that class just type:

 

Variable = new TSoldier;

 

When you wan to recognize them by id, you must create new field "id" and new constructor which takes one argument (id) and set self.id = argument.

 

Then you have many Soldiers with different ids, but you can also use array index as soldier's id.

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach
You just write new class. For example TSoldier and then you have array of TSoldiers and then, when you want to create new instance od that class just type:

 

Variable = new TSoldier;

 

When you wan to recognize them by id, you must create new field "id" and new constructor which takes one argument (id) and set self.id = argument.

 

Then you have many Soldiers with different ids, but you can also use array index as soldier's id.

 

I will try ;) thank you for Help me :P :D :D :D :D :D

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

 it's no go it :( :(

program Project2;

{$APPTYPE CONSOLE}
uses
  SysUtils,
  Unit1 in 'Unit1.pas';

begin
{ TODO -oUser -cConsole Main : Insert code here }
{Ukaz();
ob:=tob.Create;
ob.ShowTextNow;
ob.Free;      }
/////////////////////////////////////////////////////////
solider:=tsolider.Create(5,'Jirka');


with(solider) do
begin
if (solider.name = 'Jirka') then
begin
    readln;
    writeLn('Hey!');
    writeLn('You!');
    writeLn('Super!!!!!');
    readln;
end;

end;
//////////////////////////////////////////////////////


end.

 

unit Unit1;

interface

procedure Ukaz();

type
TOb = class
class procedure ShowTextNow();
end;
///////////////////////////////////////////////////////////////
Tsolider = class
constructor Create (id:Integer;name:String);
public
id:integer;
name:String;
end; 
///////////////////////////////////////////////////////////////
var
tname:string;
ob:TOb;
solider:Tsolider;


implementation

///////////////////////////////////////////////////////////////
constructor Tsolider.Create(id:Integer;name:string);
begin
inherited Create;
WriteLn('Tento objekt ma id: ',id,' a nazev: ',name);
readln;
end;
///////////////////////////////////////////////////////////////

procedure Ukaz();
begin
Writeln('Muhehhe Ja jsem normalni metoda xD');
end;

class procedure TOb.ShowTextNow();
begin
WriteLn('Muhehehehhe  Ja jsem metoda OBJEKTU');
ReadLn;
end;

end.

 

H.e.l.p.me :DD

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

But what's the problem?

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach
But what's the problem?

I dont know how regognize with name or id ... for too many such object Solider

<_< :(

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

First of all, you dont have to use "with" clause.

You can just type soldier.name.

 

Besides, you didnt defined soldiers variables.

in constructor you have to put:

 

self.id = id

self.name = name

 

and delete that inherited Create - it's default

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach
First of all, you dont have to use "with" clause.

You can just type soldier.name.

 

Besides, you didnt defined soldiers variables.

in constructor you have to put:

 

self.id = id

self.name = name

 

and delete that inherited Create - it's default

 

Hey!!!!!! It is functionality !! wellll :D Yeah :DDDDD :D

Thenk you ! BeWuO ! BeWuO ! BeWuO ! :DDDDDDDDD :thumbsup: ;)

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

Luz stary, no prob ;]

 

functionality - hehe brzmi jak fatality ;]

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

What is it ? inherited Create ? :DDD

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

it says that method Create of it's parent will be executed first, but it's parent is class Object and Object's costructor is always executed.

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

How create new instance in Delphi

Variable = new TSoldier; ? EE? XD

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

Yes... I guess ;] Actually, I dont remember - try it ;]

Udostępnij tego posta


Odnośnik do posta
Udostępnij na innych stronach

Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony.

Zarejestruj nowe konto

Załóż nowe konto. To bardzo proste!

Zarejestruj się

Zaloguj się

Posiadasz już konto? Zaloguj się tutaj.

Zaloguj się tutaj

  • Przeglądający   0 użytkowników

    Brak zarejestrowanych użytkowników, przeglądających tę stronę.

×