当我使用this
关键字访问类中的非静态变量时,Java不会给出任何错误.但是当我不使用它时,Java会出错.我为什么要用this
?
我知道通常应该什么时候使用this
,但这个例子与正常用法非常不同.
例:
class Foo {
// int a = b; // gives error. why ?
int a = this.b; // no error. why ?
int b;
int c = b;
int var1 = this.var2; // very interesting
int var2 = this.var1; // very interesting
}
Run Code Online (Sandbox Code Playgroud) 我想记住这个:
def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2)
println(fib(100)) // times out
Run Code Online (Sandbox Code Playgroud)
所以我写了这个,这令人惊讶地编译和工作(我很惊讶,因为fib
在其声明中引用自己):
case class Memo[A,B](f: A => B) extends (A => B) {
private val cache = mutable.Map.empty[A, B]
def apply(x: A) = cache getOrElseUpdate (x, f(x))
}
val fib: Memo[Int, BigInt] = Memo {
case 0 => 0
case 1 => 1
case n => fib(n-1) + fib(n-2)
}
println(fib(100)) // prints 100th fibonacci number instantly
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在a中声明fib时def
,我得到一个编译器错误:
def foo(n: …
Run Code Online (Sandbox Code Playgroud) scope scala memoization dynamic-programming forward-reference
我有点困惑.前向声明和前向引用有什么区别?在我的脑海中,当你声明一个尚未实现的函数时,前向声明是不正确的?您是否必须查看指定的情况,以声明案例"前向引用"或"前向声明"?
可能重复:
转发引用 - 为什么这段代码会编译?
Scala和前向引用
object Main extends App {
val a = 4
val b = a + c
val c = 5
println(b) // => 4
}
Run Code Online (Sandbox Code Playgroud)
这将打印4作为c
显然是0
当b
被分配.a
并且c
是价值观,因此它们不应该0
在一个时刻,而5
在下一个时刻.在Scala中,它们应该是不可变的,对吧?
我不应该至少得到某种警告吗?当然,在上面的例子中,你必须是一个想念它的白痴,但在更复杂的情况下,很难说出它的顺序.
我当然知道我可以使用,lazy val b = ...
但如果我认为我把它按正确的顺序排列,当我真的没有时.不是很危险,因为根本没有得到任何警告吗?运行得很好!?这是如何获得通过两者的Scala IDE 和编译器?这是故意的吗?或者这是一个无法修复的错误?:/
:)
我有一个递归结构,由两个组件组成:
OptionsMenuComponent :( 模板+组件)
Template:
<menu-item *ngFor="let menuItem of menu.menuItems" [menuItem]="menuItem"> </menu-item>
Component:
import { Component, Input } from '@angular/core';
import { Menu } from '../models/menu.model';
import { MenuItemComponent } from '../menu-item/menu-item.component';
@Component({
moduleId: __moduleName,
selector: 'options-menu',
templateUrl: 'options-menu.component.html',
styleUrls: ['options-menu.component.css'],
directives: [MenuItemComponent],
})
export class OptionsMenuComponent {
@Input() menu: Menu;
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)
菜单项:(模板+组件)
Template:
<span class="menu-item-title">{{menuItem.title}}</span>
<options-menu *ngIf="menuItem.subMenu != null" [menu]="menuItem.subMenu" class="sub-menu"> </options-menu>
Component:
import { Component, Input, forwardRef } from '@angular/core';
import { …
Run Code Online (Sandbox Code Playgroud) forward-reference typescript angular2-directives angular2-template angular
What does forwardRef do in angular, and what is its usage?
here is an example:
import {Component, Injectable, forwardRef} from '@angular/core';
export class ClassCL { value; }
@Component({
selector: 'my-app',
template: '<h1>{{ text }}</h1>',
providers: [{provide: ClassCL, useClass: forwardRef(() => ForwardRefS)}]
})
export class AppComponent {
text;
constructor( myClass: ClassCL ) {
this.text = myClass.value;
}
}
Injectable()
export class ForwardRefS { value = 'forwardRef works!' }
Run Code Online (Sandbox Code Playgroud) 我正在使用 TypeScript 构建 React 应用程序。
我想创建按钮,滚动到主页上子组件的标题。
我已经在子组件中创建了一个引用,遵循此堆栈溢出答案,并(尝试)使用前向引用在我的父组件上访问它。
export class Parent extends Component {
private testTitleRef!: RefObject<HTMLHeadingElement>;
scrollToTestTitleRef = () => {
if (this.testTitleRef.current !== null) {
window.scrollTo({
behavior: "smooth",
top: this.testTitleRef.current.offsetTop
});
}
};
render() {
return <Child ref={this.testTitleRef} />
}
}
interface Props {
ref: RefObject<HTMLHeadingElement>;
}
export class Child extends Component<Props> {
render() {
return <h1 ref={this.props.ref}>Header<h1 />
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我触发时,scrollToTestTitleRef
我收到错误:
Cannot read property 'current' of undefined
Run Code Online (Sandbox Code Playgroud)
这意味着 ref 未定义。这是为什么?我究竟做错了什么?
编辑: Estus …
以下给出了编译器错误:
#include <string>
const std::string& get_name();
int main(){
auto&& name1 = get_name();//should bind to whatever
const auto& name2 = get_name();//also ok
const auto&& name3 = get_name();//<-not ok, why ?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
链接到Godbolt:https://godbolt.org/z/l6IQQ7
如果我使用const auto&
它可以编译-但这不会绑定到值。
auto&&
将绑定到任何东西,以便自然也可以工作。但是,const auto&&
在这种情况下不绑定背后的逻辑是什么?我知道auto&&
会保留constness-但是有没有办法使之const
明确,同时又与引用/值无关?
动机:
对于函数等内部的“正常编程工作”,可以说类似这样的东西很棒:“我不在乎它是值还是引用-但我不会在其余函数中更改它”。
给定当前语言,这应该是可能的。
相关问题: 为什么添加const会使通用引用成为右值
作者.module.ts
@Module({
imports: [
forwardRef(() => ArticleModule),
MongooseModule.forFeature([{ name: Author.name, schema: AuthorSchema }]),
],
controllers: [AuthorController],
providers: [AuthorService, AuthorsRepository, UtilService],
exports: [AuthorService],
})
export class AuthorModule {}
Run Code Online (Sandbox Code Playgroud)
文章.module.ts
@Module({
imports: [
forwardRef(() => AuthorModule),
CityModule,
// BookmarkModule,
MongooseModule.forFeature([{ name: Article.name, schema: ArticleSchema }]),
],
controllers: [ArticleController],
providers: [ArticleService, ArticlesRepository],
})
export class ArticleModule {}
Run Code Online (Sandbox Code Playgroud)
author.schema.ts(部分文件)
@Schema(DefaultSchemaConfig)
export class Author extends BaseSchema {
@ApiPropertyOptional()
@IsString()
@Prop()
name?: string;
@ApiPropertyOptional({ type: ArticleRefDto, isArray: true })
@IsArray()
@Prop({ type: [{ type: MongooseSchema.Types.ObjectId, ref: …
Run Code Online (Sandbox Code Playgroud)