This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
class Foo {
#x = 123;
get x() {
return this.#x;
}
}
Stores a private property #x in the class Foo which is accessible via a getter.
public class Foo {
private int x;
private void set(int x) { this.x = x; }
public int get() { return x; }
}
local Foo = {}
do
local x = 0
Foo.getX = function()
return x
end
end
print(Foo.getX()) -- 0
print(x) -- nil
local function Foo()
local private = {x = 9}
local mt = {
__index = private,
__newindex = function (t, k, v)
error("attempt to update a read-only table", 2)
end
}
return setmetatable({}, mt)
end
local foo = Foo()
print(foo.x) -- 9
foo.x = 3 -- error: attempt to update a read-only table
The foo table is a read-only proxy to private table. You can manipulate private inside Foo and any closures that capture it, but you can't do that through foo.
class Foo {
lexical_has 'x', isa => Int, accessor => \(my $_x), default => 0;
method x() { return $self->$_x }
method increment_x() { $self->$_x(1 + $self->$_x) }
}
class Foo(object):
def __init__(self):
self._x = 0
@property
def x(self):
"""
Doc for x
"""
return self._x
struct Foo {
x: usize
}
impl Foo {
pub fn new(x: usize) -> Self {
Foo { x }
}
pub fn x<'a>(&'a self) -> &'a usize {
&self.x
}
pub fn bar(&mut self) {
self.x += 1;
}
}