C++:
«`cpp
class Counter {
public:
Counter() : count(0) {}
int getCount() const { return count; }
void increment() { count++; }
void decrement() { count—; }
private:
int count;
};
«`
Python:
«`python
class Counter:
def __init__(self):
self.count = 0
def get_count(self):
return self.count
def increment(self):
self.count += 1
def decrement(self):
self.count -= 1
«`
Java:
«`java
public class Counter {
private int count = 0;
public int getCount() {
return count;
}
public void increment() {
count++;
}
public void decrement() {
count—;
}
}
«`
JavaScript:
«`javascript
class Counter {
constructor() {
this.count = 0;
}
getCount() {
return this.count;
}
increment() {
this.count++;
}
decrement() {
this.count—;
}
}
«`
Утилизация:
«`
// C++
Counter counter;
counter.increment();
cout << counter.getCount() << endl;
// Python
counter = Counter()
counter.increment()
print(counter.get_count())
// Java
Counter counter = new Counter();
counter.increment();
System.out.println(counter.getCount());
// JavaScript
const counter = new Counter();
counter.increment();
console.log(counter.getCount());
```