기본 클래스의 멤버에 액세스
TypeScript 사이트의 놀이터에서 상속 예제를 참조하십시오.
class Animal {
public name;
constructor(name) {
this.name = name;
}
move(meters) {
alert(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
constructor(name) { super(name); }
move() {
alert("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
constructor( name) { super(name); }
move() {
alert(super.name + " is Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python")
var tom: Animal = new Horse("Tommy the Palomino")
sam.move()
tom.move(34)
한 줄의 코드를 변경했습니다 : Horse.move()
. 거기에 액세스 super.name
하고 싶지만 그 결과가 반환 undefined
됩니다. IntelliSense에서 사용할 수 있고 TypeScript가 제대로 컴파일되지만 작동하지 않는다고 제안합니다.
어떤 아이디어?
작업 예. 아래 참고.
class Animal {
constructor(public name) {
}
move(meters) {
alert(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
move() {
alert(this.name + " is Slithering...");
super.move(5);
}
}
class Horse extends Animal {
move() {
alert(this.name + " is Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
공용 변수에 이름을 수동으로 할당 할 필요가 없습니다.
public name
생성자 정의에서 사용하면 이 작업이 수행됩니다.super(name)
전문 클래스에서 전화 할 필요가 없습니다 .this.name
작품 사용 .
사용시주의 사항 super
.
이것은 언어 사양의 섹션 4.9.2 에서 자세히 다룹니다 .
The behaviour of the classes inheriting from Animal
is not dissimilar to the behaviour in other languages. You need to specify the super
keyword in order to avoid confusion between a specialised function and the base class function. For example, if you called move()
or this.move()
you would be dealing with the specialised Snake
or Horse
function, so using super.move()
explicitly calls the base class function.
There is no confusion of properties, as they are the properties of the instance. There is no difference between super.name
and this.name
- there is simply this.name
. Otherwise you could create a Horse that had different names depending on whether you were in the specialized class or the base class.
You are incorrectly using the super
and this
keyword. Here is an example of how they work:
class Animal {
public name: string;
constructor(name: string) {
this.name = name;
}
move(meters: number) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Horse extends Animal {
move() {
console.log(super.name + " is Galloping...");
console.log(this.name + " is Galloping...");
super.move(45);
}
}
var tom: Animal = new Horse("Tommy the Palomino");
Animal.prototype.name = 'horseee';
tom.move(34);
// Outputs:
// horseee is Galloping...
// Tommy the Palomino is Galloping...
// Tommy the Palomino moved 45m.
Explanation:
- The first log outputs
super.name
, this refers to the prototype chain of the objecttom
, not the objecttom
self. Because we have added a name property on theAnimal.prototype
, horseee will be outputted. - The second log outputs
this.name
, thethis
keyword refers to the the tom object itself. - The third log is logged using the
move
method of the Animal base class. This method is called from Horse class move method with the syntaxsuper.move(45);
. Using thesuper
keyword in this context will look for amove
method on the prototype chain which is found on the Animal prototype.
Remember TS still uses prototypes under the hood and the class
and extends
keywords are just syntactic sugar over prototypical inheritance.
참고URL : https://stackoverflow.com/questions/13121431/accessing-member-of-base-class
'Programing' 카테고리의 다른 글
URL 매개 변수로 배열 전달 (0) | 2020.08.11 |
---|---|
'Hello world'는 어디에서 왔나요? (0) | 2020.08.10 |
Vim에서 여러 줄 정규식 지원 (0) | 2020.08.10 |
사용해도 되나요? (0) | 2020.08.10 |
data.frame에 열 추가 (0) | 2020.08.10 |