在这种情况下如何在 Typescript 中实现继承
How to implement inheritance in this case in Typescript
我的问题是我希望产品 class 实现 classes [评论和类别] 以在我的代码中获得更大的可扩展性。但是,我陷入了困境。我什至尝试使用 Mixins,但我不得不重新分配 Category 和 Review 中的所有方法,有谁知道更好更智能的解决方案吗?
interface Fetch {
getInfo() :string;
}
class Review implements Fetch {
getInfo(): string {
return 'Nice Product'
}
getName(): string {
return 'Shirt Star wars'
}
geRate(): string {
return '5 stars'
}
}
class Category implements Fetch {
getInfo(): string {
return 'Nice Category'
}
getCategory(): string {
return 'Geek'
}
getSimilar(): string[] {
return []
}
}
class Product extends Review {
constructor() {
super();
}
}
let Shirt = new Product()
Shirt.getInfo()
Shirt.geRate()
以下是您所问问题的示例:
interface Fetch {
getInfo() :string;
}
interface ReviewInterface extends Fetch{
getName(): string;
getRate(): string;
}
interface CategoryInterface extends Fetch{
getCategory(): string;
getSimilar(): string[];
}
class Review implements ReviewInterface {
getInfo(): string {
return 'Nice Product'
}
getName(): string {
return 'Shirt Star wars'
}
geRate(): string {
return '5 stars'
}
}
class Category implements CategoryInterface {
getInfo(): string {
return 'Nice Category'
}
getCategory(): string {
return 'Geek'
}
getSimilar(): string[] {
return []
}
}
class Product implements ReviewInterface, CategoryInterface {
constructor() {
super();
}
// .... Here goes all implementations...
}
let Shirt = new Product()
Shirt.getInfo()
Shirt.geRate()
我的问题是我希望产品 class 实现 classes [评论和类别] 以在我的代码中获得更大的可扩展性。但是,我陷入了困境。我什至尝试使用 Mixins,但我不得不重新分配 Category 和 Review 中的所有方法,有谁知道更好更智能的解决方案吗?
interface Fetch {
getInfo() :string;
}
class Review implements Fetch {
getInfo(): string {
return 'Nice Product'
}
getName(): string {
return 'Shirt Star wars'
}
geRate(): string {
return '5 stars'
}
}
class Category implements Fetch {
getInfo(): string {
return 'Nice Category'
}
getCategory(): string {
return 'Geek'
}
getSimilar(): string[] {
return []
}
}
class Product extends Review {
constructor() {
super();
}
}
let Shirt = new Product()
Shirt.getInfo()
Shirt.geRate()
以下是您所问问题的示例:
interface Fetch {
getInfo() :string;
}
interface ReviewInterface extends Fetch{
getName(): string;
getRate(): string;
}
interface CategoryInterface extends Fetch{
getCategory(): string;
getSimilar(): string[];
}
class Review implements ReviewInterface {
getInfo(): string {
return 'Nice Product'
}
getName(): string {
return 'Shirt Star wars'
}
geRate(): string {
return '5 stars'
}
}
class Category implements CategoryInterface {
getInfo(): string {
return 'Nice Category'
}
getCategory(): string {
return 'Geek'
}
getSimilar(): string[] {
return []
}
}
class Product implements ReviewInterface, CategoryInterface {
constructor() {
super();
}
// .... Here goes all implementations...
}
let Shirt = new Product()
Shirt.getInfo()
Shirt.geRate()