我的问题是我希望Product类实现两个类[Review和Category]以在我的代码中获得更多的可伸缩性。但是,我陷入了困境。我尝试使用甚至是Mixins,但我不得不重新分配来自Category and 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()
答案 0 :(得分:2)
以下是您提出的问题的示例:
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()