G
Guest
Hello,
I´m coming from the Java World. Here Programmers often use (like in
C++?) getter and setter methods.
F.e.:
class Mirror{
private int width_;
private int height_;
public Mirror(width, height)
{
width_ = width;
height_ = height;
}
public int getWidth()
{
return width_;
}
public int getHeight()
{
return height_;
}
}
In C# I often have seen now, that programmers do not use getter and
setter methods.
The define their class variables as public like:
class Mirror{
public int width_;
public int height_;
public Mirror(width, height)
{
width_ = width;
height_ = height;
}
}
Why do they do that? Only to have fewer methods?
For me it makes sense to have getter and setter methods to encapsulate
the classes so that no class can change the instance variables of
another class.
Except of course if they use the setter-methods.
Regards,
Martin
I´m coming from the Java World. Here Programmers often use (like in
C++?) getter and setter methods.
F.e.:
class Mirror{
private int width_;
private int height_;
public Mirror(width, height)
{
width_ = width;
height_ = height;
}
public int getWidth()
{
return width_;
}
public int getHeight()
{
return height_;
}
}
In C# I often have seen now, that programmers do not use getter and
setter methods.
The define their class variables as public like:
class Mirror{
public int width_;
public int height_;
public Mirror(width, height)
{
width_ = width;
height_ = height;
}
}
Why do they do that? Only to have fewer methods?
For me it makes sense to have getter and setter methods to encapsulate
the classes so that no class can change the instance variables of
another class.
Except of course if they use the setter-methods.
Regards,
Martin