Answer the question
In order to leave comments, you need to log in
When to use private fields when defining a class?
There is an opinion that when a class is written, you need to make most of the properties private so that no one can change the internal state of objects, thus reducing unpredictable situations. Others explain that the state of the object is the affairs of the object (it decides how to change it). And in order to influence the object, we can call its public methods, and it will change (if necessary) the state.
But if you do everything private, then we will not have access not only to change but also to read, and this is often necessary. In this regard, I had a question how to deal with such a situation: make getters (for all or only some properties), make methods that would use private properties, or make them public and leave the responsibility of correct changes to the programmer?
Example: there is a rectangle (Rectangle) which we want to draw on some canvas (Canvas)
class Rectangle {
private x, y, w, h: number;
constructor(x, y, w, h: number) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
// ......
}
class Canvas {
private w, h: number;
constructor(w, h: number) {
this.w = w;
this.h = h;
}
public drawRect(rect: Rectangle) {
// Здесь нужны координать и размеры прямоуголника
}
}
const rect = new Rect(0, 0, 10, 15);
const canvas = new Canvas(20, 40);
canvas.drawRect(rect);
class Rectangle {
private x, y, w, h: number;
constructor(x, y, w, h: number) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public drawOnCanvas(canvas: Canvas) {
canvas.drawRect(x, y, w, h);
}
}
class Canvas {
private w, h: number;
constructor(w, h: number) {
this.w = w;
this.h = h;
}
public drawRect(x, y, w, h: number) {
// Есть все что нужно для рисования !!!!
}
}
const rect = new Rect(0, 0, 10, 15);
const canvas = new Canvas(20, 40);
rect.drawOnCanvas(canvas);
Answer the question
In order to leave comments, you need to log in
if you have typescript you can do this
class Rectangle {
readonly x: number
readonly y: number
readonly w: number
readonly h: number
constructor(x: number, y: number, w: number, h: number) {
this.x = x
this.y = y
this.w = w
this.h = h
}
}
class Canvas {
private w: number
private h: number
constructor(w: number, h: number) {
this.w = w;
this.h = h;
}
public drawRect(rect: Rectangle) {
//draw implementation
}
}
You are using almost nothing meaningful from OOP here. I'm writing an example in pascal, but it doesn't change anything.
The standard solution would be to create a "Drawable" base class, one that can be drawn onto the canvas.
Let's say TSprite. And we will declare a virtual Draw method so that we can ask to be drawn.
TSprite = class //ну или интерфейс
public
procedure Draw; virtual; abstract;
end;
TRectangle = class(TSprite)
private
FX, FY, FH, FW: Float;
FCanvas: TCanvas;
public
procedure Draw; override;
constructor Create(Canvas: TCanvas; const X, Y, H, W: Float);
end;
constructor TRectangle.Create(Canvas: TCanvas; const X, Y, H, W: Float);
begin
FCanvas := Canvas;
FX := X;
FY := Y;
FH := H;
FW := W;
end;
procedure TRectangle.Draw;
begin
FCanvas.Rectangle(FX, FY, FH, FW); // Это отрисовка фигуры на канве
end;
Rect := TRectangle.Create(Canvas, 10, 10, 40, 45);
Rect.Draw; // Всё, можно отрисовать
In primitive classes just fields also should be public. What would be less noodles. But this does not apply to the presentation. That is, you have a rectangle and a rectangle stored in the database. In the first case, through the fields, in the second, only through a method that saves to the database and then changes the fields
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question