我希望Handler下面的内容将自己推入列表中
use std::vec::Vec;
use std::rc::Rc;
use std::cell::RefCell;
struct Handler<'a> {
list: Rc<RefCell<Vec<&'a mut Handler<'a>>>>
}
impl<'a> Handler<'a> {
fn new(list: Rc<RefCell<Vec<&'a mut Handler<'a>>>>) -> Self {
Handler { list: list }
}
fn push(&mut self) {
self.list.borrow_mut().push(self)
}
}
fn main() {
let list = Rc::new(RefCell::new(Vec::new()));
let mut h1 = Handler::new(list);
let mut h2 = Handler::new(list);
h1.push();
h2.push();
// Here the list should contain both h1 and h2
}
Run Code Online (Sandbox Code Playgroud)
但我遇到了这个错误,但我找不到解决方法!
use std::vec::Vec;
use std::rc::Rc;
use std::cell::RefCell;
struct Handler<'a> …Run Code Online (Sandbox Code Playgroud) 我试着像这样调用父的第二个构造函数
abstract class A(val i: Int) {
constructor(c: C) : this(c.i)
}
class B() : A(0) {
constructor(c: C) : super(c) // error is here
}
class C(val i: Int)
Run Code Online (Sandbox Code Playgroud)
但它会产生Primary constructor call expected错误.子类如何调用父的辅助构造函数?