Autor Beitrag
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Do 25.05.06 15:45 
Hallo,

ich mache derzeit meine ersten Schritte auf dem Gebiet der Objektorientierten Programmierung. Ich will eine Klasse TDatei schreiben, die Variablen und Methoden enthält.

Methoden:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
 procedure DeleteSubject(Sender : TObject);
 procedure UpdateSubjects;
 procedure AddSubjects( number : integer);
 procedure CreateAnalyzing;
 function Validation : string;
 function SaveMMFFile(datei : TMMFFile) : boolean;
 function AnalyzeData() : string;


Variablen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 description : string;
 saved : boolean;
 filename : string;
 author : string;
 date : string;
 time : string;
 protect : boolean;
 hash : string;


Wie muss die Deklaration nun aussehen?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
type
  TFile = class
     private
  //und weiter?


Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 25.05.06 15:49 
Hallo!

Ich mach's mal exemplarisch für eine Methode und ein Feld:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
type
  TFile = class
  public
    description : string;

    procedure DeleteSubject(Sender : TObject);
  end;

implementation

procedure TFile.DeleteSubject(Sender : TObject);
begin
...
end;

Ich habe dabei sowohl Feld als auch Methode mal public gemacht. Solltest Du das Feld privat machen wollen, dann ist es üblich, noch ein "F" dafür zu schreiben. Bleibt es public, solltest Du eine Property draus machen.


Darf ich Dir an dieser Stelle meinen Crashkurs Delphi ans Herz legen :-)

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 26.05.06 12:53 
Was ist denn besser in diesem Fall und warum - private oder public?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 26.05.06 13:11 
Hallo!

"Besser" oder "Schlechter" gibt es hier nicht. Es geht hier um das Prinzip der Sichtbarkeiten und der Kapselung. Objekte geben Dir die Möglichkeit, nur die Teile deines Codes für andere (also für andere Objekte, Prozeduren, etc.) sichtbar zu machen, die diese sehen müssen. Das gibt Dir zum einen die Kontrolle, von wo auf was zugegriffen wird. Zum anderen kannst Du die Interna Deiner Klasse ändern, wie Du willst, so lange Du nur die öffentlichen Teile gleich lässt.

Du musst nun also überlegen, ob die obigen Felder nur intern verwendet werden oder auch von außen drauf zugregiffen werden muss. Ich verfahre dabei immer nach dem Schema, immer erst einmal so wenig wie möglich sichtbar zu machen. Die Sichtbarkeit hinterher zu erhöhen geht problemlos, sie zu verringern kann Probleme geben, wenn man doch mal von außen auf ein Feld zugegriffen hat, das jetzt "versteckt" werden soll.

Außerdem habe ich mir angewöhnt, Felder nicht direkt public zu machen, sondern in Properties zu kapseln. Wo der Vorteil da liegt, kannst Du in oben verlinktem Crashkurs nachlesen, das schreib ich hier nicht nochmal auf ;-)

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 26.05.06 13:20 
Ich habe mir das so gedacht, dass ich in einer anderen Klasse (TForm2) die Werte der Felder/Properties abfrage. Nehmen wir z.B. description, welches die Beschreibung der Datei enthält. Dies soll aus einem anderen Formular, also aus einer anderen Klasse, abgefragt werden können. Also ist public besser als private, weil es ja von außen sichtbar sein soll?
Und könntest du bitte mal die verschiedenen Sichtbarkeitsstufen aufsteigend ordnen, beginnend mit dem wenigsten?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 26.05.06 13:27 
user profile iconMarco D. hat folgendes geschrieben:
Ich habe mir das so gedacht, dass ich in einer anderen Klasse (TForm2) die Werte der Felder/Properties abfrage. Nehmen wir z.B. description, welches die Beschreibung der Datei enthält. Dies soll aus einem anderen Formular, also aus einer anderen Klasse, abgefragt werden können. Also ist public besser als private, weil es ja von außen sichtbar sein soll?
Wenn andere Klassen das abrufen sollen, muss es publich sein. Du solltest aber überlegen, ob Du einige der Infos als readonly-Properties zur Verfügung stellst, denn z.B. der Dateiname wird sich ja wahrscheinlich nicht mehr (sinnvoll) ändern.

user profile iconMarco D. hat folgendes geschrieben:
Und könntest du bitte mal die verschiedenen Sichtbarkeitsstufen aufsteigend ordnen, beginnend mit dem wenigsten?
Nimm's mir nicht übel, aber das ist nun wirklich über die Hilfe selber lösbar.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 26.05.06 13:31 
user profile iconChristian S. hat folgendes geschrieben:
user profile iconMarco D. hat folgendes geschrieben:
Ich habe mir das so gedacht, dass ich in einer anderen Klasse (TForm2) die Werte der Felder/Properties abfrage. Nehmen wir z.B. description, welches die Beschreibung der Datei enthält. Dies soll aus einem anderen Formular, also aus einer anderen Klasse, abgefragt werden können. Also ist public besser als private, weil es ja von außen sichtbar sein soll?
Wenn andere Klassen das abrufen sollen, muss es publich sein. Du solltest aber überlegen, ob Du einige der Infos als readonly-Properties zur Verfügung stellst, denn z.B. der Dateiname wird sich ja wahrscheinlich nicht mehr (sinnvoll) ändern.

Was wenn man die Datei unter einem anderen Namen abspeichert

user profile iconChristian S. hat folgendes geschrieben:

user profile iconMarco D. hat folgendes geschrieben:
Und könntest du bitte mal die verschiedenen Sichtbarkeitsstufen aufsteigend ordnen, beginnend mit dem wenigsten?
Nimm's mir nicht übel, aber das ist nun wirklich über die Hilfe selber lösbar.

Ich versuch's mal ;)

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 26.05.06 13:40 
user profile iconMarco D. hat folgendes geschrieben:
Was wenn man die Datei unter einem anderen Namen abspeichert
Naja, da musst Du jetzt selber überlegen, welche Aktionen von außen sinnvoll sind. Ich weiß ja nicht, was die Klasse machen soll :zwinker:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 26.05.06 21:10 
Ich will in der Klasse ein Array of TEdit deklarieren:
ausblenden Delphi-Quelltext
1:
 FEingabeArray : array of TEdit;					

Delphi sagt immer, TEdit sei ein undefinierter Bezeichner. An den Units kann es nicht liegen:
ausblenden Delphi-Quelltext
1:
2:
3:
uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

Was mache ich falsch?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Fr 26.05.06 21:43 
du musst StdCtrls noch in die uses-klausel mitaufnehmen ;)
dann klappts
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 27.05.06 15:42 
Ok das geht. :zustimm:

Nächstes Problem:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
private
FEingabeArray : array of TEdit;
public
//hier der Fehler:
property EingabeArray : array of TEdit read FEingabeArray write FEingabeArray;

Fehler:
Zitat:

Bezeichner erwartet, aber 'ARRAY' gefunden

Was tun?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 27.05.06 15:54 
Hallo Marco!

Im Interesse aller (auch der Übersichtlichkeit wegen) würde ich dich bitten, pro Thread nur eine Frage zu stellen. Was ich ebenfalls anmerken möchte ist folgendes: bevor du hier wegen Kleinigkeiten (ich nenn's mal so) Threads erstellst, würde ich dich bitten, entweder Christians Tutorial zu lesen oder alternativ in der OH oder sonstwo im Internet dich in das Thema OOP einzulesen. Es heißt zwar "learning by doing", aber ohne entsprechende Grundlage ist das nur beschränkt sinnvoll.

AXMD
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Sa 27.05.06 16:24 
Hallo!

Vielleicht hilft dir auch schon dieser Thread weiter und dein PRoblem mit em Array ist gelöst ;)


greetz

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 27.05.06 21:27 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
type TEditArray = array of TEdit;

//Klassendeklaration
private
FEingabeArray : TEditArray
public
//hier der Fehler:
property EingabeArray : TEditArray read FEingabeArray write FEingabeArray;

Ok funktioniert.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Sa 27.05.06 22:03 
Vergiss nicht, dass SetLength(EingabeArray, x); nicht funktioniert. Musst du über eine weitere property machen ;)

greetz

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 27.05.06 22:06 
Ja dankeschön :zustimm:

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot