Working inheritance #68

This commit is contained in:
2026-06-07 01:57:09 +02:00
parent 1977bd59be
commit e70e813497
2 changed files with 24 additions and 53 deletions
+18 -51
View File
@@ -1,62 +1,29 @@
pub class Counter {
pub count i32;
pub constructor(start i32) {
self.count = start;
}
pub fn increment(*mut self) {
self.count++;
}
pub fn get(*self) i32 {
self.count
}
}
pub fn test_class_create() {
let c = Counter::new(0);
assert_eq!(c.get(), 0);
}
pub fn test_class_method() {
let mut c = Counter::new(5);
c.increment();
assert_eq!(c.get(), 6);
}
pub fn test_class_field_access() {
let c = Counter::new(42);
assert_eq!(c.count, 42);
}
class NamedCounter : Counter {
pub class Animal {
pub name String;
pub constructor(start i32, name String) {
self._super = Counter::new(start);
self.name = name;
constructor() {
self.name = "Rex".to_string();
}
pub fn get_name(*self) String {
self.name.clone()
}
pub override fn increment(*mut self) {
println!("OK!!");
pub fn speak(*self) {
println!("Unknown");
}
}
pub fn test_class_inherit() {
let nc = NamedCounter::new(10, "test".to_string());
assert_eq!(nc.get_name(), "test");
assert_eq!(nc.count, 10);
pub class Dog : Animal {
constructor() {
unsafe {
std::ptr::write(&mut self._super, Animal::new());
}
}
pub override fn speak(*self) {
println!("Woof!");
}
}
pub fn run_tests() {
test_class_create();
test_class_method();
test_class_field_access();
test_class_inherit();
let dawg = Dog::new();
dawg.speak();
}