当前位置:网站首页>Typescript类的使用
Typescript类的使用
2022-07-28 21:36:00 【RemoteDev】
/*
* 基类
* */
class Base{
constructor(obj=null) {
if (obj){
console.log('子类调用,基类构造函数',obj.a,obj.b);
}else{
console.log('基类构造函数');
}
}
toString(obj){
let [a,b]=[obj.a,obj.b];
console.log('子类调用:toString()===>',a,b);
}
}
//私有方法名符号定义
let s1 = Symbol('privateM1');
let s2 = Symbol('privateM2');
//私有属性名符号定义
let p1 = Symbol('privateP1');
/*
* 派生类,继承于Base
* */
class Framework extends Base{
constructor({a=0,b=0}={}) {
super({a,b});//向基类传入参数
super.toString({a,b});//调用基类函数
this[p1]='RemoteDev';
console.log('私有属性输出:',this[p1]);
}
getPrivateProto(){
return this[p1];
}
setPrivateProto(v){
this[p1] = v;
}
//公有方法实现,默认下类的所有方法都是公有的
getParentName(){
console.log('Public Method Call ...');
outOfClassMethod();//调用类外方法,这样类对象就无法访问outOfClassMethod
}
//类的私有方法实现
_getParentName(){
console.log('Private Method Call ...');
}
call_out_method(obj){
outOfClassMethod.call(this,obj)//类外方法绑定形式实现私有方法
}
//调用通过Symbol定义的私有方法
call_private(){
this[s1]('hello');
this[s2]('world');
}
//通过Symbol形式实现类私有方法,类对象无法访问通过Symbol命名的方法
[s1](obj){
console.log('通过Symbol命名的私有方法:',s1,obj)
}
[s2](obj){
console.log('通过Symbol命名的私有方法:',s2,obj)
}
//类的私有私有属性与私有方法的另一种写法,#代表私有,目前属于提案阶段
// #x=0;
// #getx(){return #x;}
}
function outOfClassMethod(obj=null){
console.log("通过调用类外部的方法形式实现类的私有方法",obj);
}
let base = new Base();//实例化基类
let framework = new Framework({a:8,b:9});//实例化派生类
//类实例是对象,类是函数
console.log('类对象的数据类型:',typeof base,typeof framework,'类的数据类型',typeof Base,typeof Framework);
console.log('类原型对象构造:',Framework.prototype.constructor,'类构造:',Framework.constructor);
console.log('类的原型对象prototype的constructor指向类名:',Framework.prototype.constructor===Framework);//类的原型对象prototype的constructor指向类名
console.log('类名:',Framework.name);
console.log(framework.constructor===Framework.prototype.constructor);
//通过类原型对象向类添加方法
Base.prototype.Add = function (x,y) {
return x+y;
}
console.log('调用添加到基类原型对象上的方法:',base.Add(1,2));//调用添加到基类原型对象上的方法
console.log('派生类调用基类原型对象的方法:',framework.Add(5,6));//派生类调用基类原型对象的方法
// @ts-ignore
Base.prototype.test =()=> {
console.log('test func');
};
console.log(Object.getOwnPropertyNames(Base.prototype));//取类所有方法名
//使用Object.assign为类添加方法
Object.assign(Base.prototype, {
M1() {
console.log('M1');
},
M2() {
console.log('M2');
},
M3() {
console.log('M3');
}
});
//调用添加的方法
base.M1();
framework.M2();
framework.M3();
//注: 类内部定义的所有方法都是不可枚举的
console.log(Object.keys(Base.prototype));//constructor,toString这两个方法都在类内部定义,所以Object.keys无法枚举
//使用变量名定义类方法
let method = 'Done';
Object.assign( Base.prototype,{[method](){
console.log('通过变量名定义的函数');
}});
console.log(Object.keys(Base.prototype));
base.Done();//通过方法名调用
Reflect.get(base,method)();//使用反射通过变量名调用
framework.Done();
console.log(Object.getOwnPropertyNames(Framework.prototype));
console.log('取当前类的继承对象',Object.getPrototypeOf(framework));
console.log('取当前类的继承对象的所有方法',Object.getPrototypeOf( Object.getPrototypeOf(framework)));
//根据子类取得基类,并调用基类方法
let p = Object.getPrototypeOf( Object.getPrototypeOf(framework))
p.Done();
console.log(Object.getOwnPropertyNames(p));
console.log(Reflect.ownKeys(p));
Reflect.get(p,method)();
console.log('base对象原型',base.__proto__,'framework对象原型:',framework.__proto__);
//通过表达式形式声明类
const ExpClass = class Exp {//此处Exp为类的别名,作用域只限于类内部定义
constructor() {
console.log('构造时打印类名:',Exp.name);
}
getClassName(){
console.log('调用getClassName打印类名:');
return Exp.name;
}
}
let exp = new ExpClass();
console.log(exp.getClassName());
//立即执行类
let execClass = new class{
constructor() {
console.log('立即执行类');
}
}();
//带参数的立即执行类
let execClassWithParam = new class{
constructor(param) {
console.log('立即执行类,参数:',param);
}
}('RemoteDev');
//注: 类不存在变量提升,必须先声明后再实例化
framework.getParentName();
framework._getParentName();//unsafe 类外依然可调用
framework.call_out_method({x:'hello'});
console.log(Object.getOwnPropertyNames(Framework.prototype));
console.log(Object.getPrototypeOf(framework));//基类
console.log(Object.keys(framework));//类内部定义方法不可枚举,所以为[]
framework.call_private();//通过公有方法访问私有方法
console.log( '读私有属性值:',framework.getPrivateProto());
framework.setPrivateProto('NICK');//写私有属性值
console.log( '读私有属性值:',framework.getPrivateProto());边栏推荐
- Goer shares and Shanghai Taisi Weida growth cooperation agreement! Special SOC jointly promotes the development of TWS headphones
- mgr.exe病毒导致启动程序启动失败
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
- WebView optimization
- Sqlilabs-3 (entry notes)
- Record a question about the order of trigonometric function exchange integrals
- 【MySQL系列】 MySQL表的增删改查(进阶)
- Improvement 17 of yolov5: cnn+transformer -- integrating bottleneck transformers
- Console.log() console display... Solution
- 【雷达】基于核聚类实现雷达信号在线分选附matlab代码
猜你喜欢

定了!哪吒S全系产品将于7月31日上市发售

RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块

【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码

WebView optimization

cannot resize variables that require grad
![[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code](/img/31/e4cd4c261a7fc5cfa731976314530b.png)
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code

cnpm安装步骤

The industry's first cloud native security detection dual model! Safety dog heavyweight report appears at the digital China Construction Summit

A new paradigm of distributed deep learning programming: Global tensor

Introduction to address book export without code development platform
随机推荐
Vant web app installation reference
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
Console.log() console display... Solution
[physical application] atmospheric absorption loss with matlab code
1.8tft color screen test code (stm32f407ve)
RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
cannot resize variables that require grad
xshell7,xftp7个人免费版官方下载,无需破解,免激活,下载即可使用
Nacos配置热更新的4种方式、读取项目配置文件的多种方式,@value,@RefreshScope,@NacosConfigurationProperties
Applet Download Preview PDF, document cannot open solution
Symbol symbol type
Summary of common formula notes for solving problems in Higher Mathematics
6 个超级良心的开源教程!
Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
Target detection notes fast r-cnn
WebView optimization
Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
Kotlin JVM annotation
Wheel 6: qserialport serial port data transceiver
Mgr.exe virus caused the startup program to fail