30人参与 • 2025-02-14 • Javascript
什么是类?
在生活中,类一些具有相似属性和行为的事物抽象出来的概念,比如:人类、球类、汽车类;
在javascript中,类是模板,是用于创建实例对象的模板;相当于实例的原型(prototype);
class 类名 { constructor(){ ... } ... }
// 定义类 class classname { // 构造方法 constructor(name) { this.name = name; // 实例属性 } static author = "zyl"; // 静态属性 #attr = 10; // 私有属性 // 静态方法 static sfn(data) { return `我是静态方法,只能通过类名调用,接收的参数为${data};`; } // 普通方法 fn(data) { console.log(`私有属性的值为${this.#attr};`); // 访问私有属性 return `我是普通方法,通过实例调用,接收的参数为${data};`; } } // 实例化 const class1 = new classname("第一个类"); console.log(class1); // classname {name: '第一个类'} // 访问静态属性 console.log(classname.author); // zyl // 访问实例属性 console.log(class1.name); // 第一个类 // 访问静态方法 console.log(classname.sfn("arg")); // 我是静态方法,只能通过类名调用,接收的参数为arg; // 访问实例方法 console.log(class1.fn("123")); // 私有属性的值为10; 我是普通方法,通过实例调用,接收的参数为123;
javascript类中的属性有:静态属性、实例属性、私有属性;
类的属性,使用static关键字定义,通过【类名.属性名】访问;
定义在构造函数的this上,通过【实例.属性名】访问;
使用【#属性名】的方式定义,只能在类的内部访问;
// 定义类 class classname { // 构造方法 constructor(name) { this.name = name; // 实例属性 } static author = "zyl"; // 静态属性 #attr = 10; // 私有属性 fn() { return this.#attr; } } // 实例化 const class1 = new classname("第一个类"); console.log(class1); // classname {name: '第一个类'} // 访问静态属性 console.log(classname.author); // zyl // 访问实例属性 console.log(class1.name); // 第一个类 // 访问私有属性 console.log(class1.fn()); // 10
javascript类中方法有:构造方法、静态方法、普通方法、私有方法;
构造方法是一种特殊的方法:
// 定义person类 class person { constructor(name, age) { this.name = name; this.age = age; } } // 创建person1实例对象 const person1 = new person("zyl", 18); console.log(person1); // person {name: 'zyl', age: 18}
使用static关键字定义,又叫类方法,它是属于类的,而非实例对象;
静态方法不能继承,不能通过实例调用,只能通过当前类名调用【类名.方法名】(父类的静态方法可以被子类继承);
// 定义品牌类 class brand { constructor(name, type) { this.name = name; this.type = type; } // 静态方法,判断是否为vip用户 static isvip(count) { return count == "zylcount" ? "vip用户" : "新用户"; } // 静态方法,获取品牌折扣价 static dprice(price) { return price > 10 ? price * 0.9 : price * 0.95; } } const brand1 = new brand("zylbrand", "clothing"); console.log(brand1); // brand {name: 'zylbrand', type: 'clothing'} // 调用静态方法,通过类名brand调用,而非实例brand1调用; console.log(brand.isvip("1111111")); // 新用户 console.log(brand.dprice(12)); // 10.8
类中可以定义任意数量的普通方法;
普通方法定义在类的原型上(prototype属性上),会被实例继承;
通过实例调用【实例.方法名】,不能通过类名调用;
// 定义汽车类 class car { constructor(brand, price) { this.brand = brand; this.price = price; } // 定义普通方法 getinfo() { return `该车的品牌是${this.brand};售卖价格是${this.price}`; } } let car1 = new car("volvo", "16.8w"); console.log(car1); // {brand: 'volvo', price: '16.8w'} // 通过实例调用普通方法 console.log(car1.getinfo()); // 该车的品牌是volvo;售卖价格是16.8w // 普通方法是定义在car类的原型上 console.log(car.prototype); // {constructor: ƒ, getinfo: ƒ} console.log(car.prototype.getinfo === car1.__proto__.getinfo); // true
javascript中允许我们在创建一个类的时候,依赖于另一个类;被创建的类称为【子类/派生类】,被依赖的类称为【父类/基类】;子类会继承父类的属性和方法,有利于提高代码的可复用性;
extends关键字,用于类的继承;使创建子类继承一个父类, 子类会继承父类的所有属性和方法;
(1)语法
class 子类 extends 父类 { // 定义子类的属性和方法 ... }
(2)实例
class parentclass { constructor(name, age) { this.name = name; this.age = age; } static attr = 'abc'; static sfn(data){ return `我是定义在父类的静态方法,收到的参数为:${data}`; } getinfo(data) { return `我是定义在父类的普通方法,收到的参数为:${data}`; } } class childclass extends parentclass { } const parent1 = new parentclass('parent', '45'); const child1 = new childclass('zyl', 20); console.log(parent1); // parentclass {name: 'parent', age: '45'} console.log(child1); // childclass {name: 'zyl', age: 20} console.log(childclass); // class childclass extends parentclass {} console.log(childclass.attr); // abc console.log(childclass.sfn('1111')); // 我是定义在父类的静态方法,收到的参数为:1111 console.log(child1.getinfo(123)); // 我是定义在父类的普通方法,收到的参数为:123
super关键字用于调用父类的属性和方法;在调用时,需要指定调用的父类属性或父类方法;
super必须写在this.xxx之前;
(1)语法
// 调用父类的构造方法 super(); // 访问父类属性 super.属性; // 调用父类方法 super.方法名();
(2)实例
// 定义父类 class parentclass { constructor(name, age, address) { this.name = name; this.age = age; this.address = address; } static attr = "abc"; static sfn(data) { return `我是定义在父类的静态方法,收到的参数为:${data}`; } getinfo(data) { return `我是定义在父类的普通方法,收到的参数为:${data}`; } } // 定义子类,继承父类 class childclass extends parentclass { constructor(name, age, address, phone) { // 调用父类的构造方法 super(name, age, address); this.phone = phone; } // 访问父类的静态属性 static attr = super.attr + "def"; static sfn(data) { // 调用父类的静态方法 console.log(`子类通过super调用:${super.sfn("super调用的实参")}`); return `我是定义在子类的静态方法,收到的参数为:${data}`; } getinfo(data) { // 调用父类的普通方法 console.log(`子类通过super调用:${super.getinfo("super调用的实参")}`); return `我是定义在子类的普通方法,收到的参数为:${data}`; } } const parent1 = new parentclass("parent", 45 , "上海"); const child1 = new childclass("child", 20, "上海", '11111111'); console.log(parent1); // parentclass {name: 'parent', age: 45, address: '上海'} console.log(child1); // childclass {name: 'child', age: 20, address: '上海'} console.log(parentclass.attr); // abc console.log(childclass.attr); // abcdef console.log(parentclass.sfn("111")); // 我是定义在父类的静态方法,收到的参数为:111 console.log(childclass.sfn("222")); // 子类通过super调用:我是定义在父类的静态方法,收到的参数为:super调用的实参 // 我是定义在子类的静态方法,收到的参数为:222 console.log(parent1.getinfo(123)); // 我是定义在父类的普通方法,收到的参数为:123 console.log(child1.getinfo(456)); // 子类通过super调用:我是定义在父类的普通方法,收到的参数为:super调用的实参 // 我是定义在子类的普通方法,收到的参数为:456
到此这篇关于javascript中类(class)的介绍和应用的文章就介绍到这了,更多相关js中类class详解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论