我有一个抽象类:
abstract class Foo {
abstract bar(): string;
}
Run Code Online (Sandbox Code Playgroud)
我有一些扩展的类Foo:
class Foo1 extends Foo {
bar(): string { return 'foo1'; }
}
class Foo2 extends Foo {
bar(): string { return 'foo2'; }
}
Run Code Online (Sandbox Code Playgroud)
我还有一个类,我要代理的所有方法Foo的Foo。这实际上工作正常,如果我Foo在这个类上定义所有方法。但我宁愿不这样做。我宁愿让Foo定义的方法Foo和编译器知道FooProxy也实现了这些方法,而不必实际实现它们。这可能吗?Proxy 类看起来像这样:
class FooProxy {
public foo: Foo;
constructor(foo: Foo) {
this.foo = foo;
let handler = {
get: function(target: FooProxy, prop: string, receiver: any) {
if(Foo.prototype[prop] !== null) {
return target.foo[prop]; …Run Code Online (Sandbox Code Playgroud) 我创建了一个组件,意在成为一个开关.您可以像使用复选框一样使用它.这是一个精简版.
我-switch.component.ts:
import {Component, Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'my-switch',
template: `<a (click)='toggle()'>
<span *ngIf='value'>{{onText}}</span>
<span *ngIf='!value'>{{offText}}</span>
</a>`
})
export class MySwitchComponent {
@Input() onText: string = 'On';
@Input() offText: string = 'Off';
@Input() value: boolean;
@Output() change = new EventEmitter <boolean> ();
position: string;
toggle() {
this.value = !this.value;
this.change.emit(this.value);
}
}
Run Code Online (Sandbox Code Playgroud)
我的计划是这样使用它:
家长component.ts
import {Component} from '@angular/core';
import {MySwitchComponent} from 'my-switch.component';
@Component({
selector: 'my-sites',
directives: [MySwitchComponent]
template: `<table>
<tr *ngFor='let item of items'>
<td>
<my-switch
[(value)]='item.options.option1' …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 go 中实现一组功能。上下文是一个事件服务器;我想防止(或至少警告)为一个事件多次添加相同的处理程序。
我读过,地图通常用作集合,因为可以轻松检查成员资格:
if _, ok := set[item]; ok {
// don't add item
} else {
// do add item
}
Run Code Online (Sandbox Code Playgroud)
不过,我在使用这种函数范式时遇到了一些麻烦。这是我的第一次尝试:
// this is not the actual signature
type EventResponse func(args interface{})
type EventResponseSet map[*EventResponse]struct{}
func (ers EventResponseSet) Add(r EventResponse) {
if _, ok := ers[&r]; ok {
// warn here
return
}
ers[&r] = struct{}{}
}
func (ers EventResponseSet) Remove(r EventResponse) {
// if key is not there, doesn't matter
delete(ers, &r)
}
Run Code Online (Sandbox Code Playgroud)
很明显为什么这行不通:函数不是 Go 中的引用类型,尽管有些人会告诉你它们是。 …