typescript - 如何从定义为第一类属性的类中访问类的属性?
问题描述
假设我有 2 个类,SimpleClass 和 MyChart:
class SimpleClass {
top: number;
bottom: number;
chart: MyChart;
constructor() {
this.chart = new MyChart();
}
}
class MyChart {
name: string;
public method() {
}
}
MyChart 的 method() 是否可以访问字段top
和bottom
?
解决方案
MyChart 类是否始终是 SimpleClass 的属性?
在这种情况下,您可以将 SimpleClass 作为构造函数参数传递给 MyChart。
这将使它们之间产生强烈的耦合,因此根据您的场景,Alexander 传递您需要的属性的想法可能是一个不错的选择。您还可以定义一个特殊类型以传递给仅包含您需要的属性的构造函数,然后创建此传递的实例作为构造函数参数。
干杯!
推荐阅读
- ios - 如何使用 RealityKit 将 3D 模型放置在参考图像之上?
- reactjs - 单击按钮时更改网格
- asp.net - Search() 操作方法返回 Index View() 时出错
- javascript - 参数返回 undefine 和 php 通过 id 选择返回 null
- vue.js - 如何在 nuxt 中添加@carbon/type
- android - 如何在 Ktor-client for Android 中清除不记名令牌
- c++ - 是否可以从枚举创建一个 initializer_list?
- c# - C# 语法帮助、日期格式和将 " 添加到字符串
- swift - 添加第三个 TextField 时不加载 AlertView
- asp.net - 呈现页面时未加载 es-scripts