Điều này trong lớp javascript
Ngày đăng:
11/12/2022
Trả lời:
0
Lượt xem:
132
Lớp để tạo ra các đối tượng (thực chất là một cách thức khác để tạo ra các đối tượng mà Javascript đã biết). Đối tượng trong nó có các thuộc tính, phương thức. Tạo đối tượng bằng cách sử dụng từ khóa //Khai báo một lớp có tên Product class Product { //Hàm khởi tạo constructor(name, price) { this.name = name; this.price = price; this.infomation = `${name} - ${price}`; } //Khai báo một phương thức checkStore(storeid) { console.log(this.name + ' in store ' + storeid); } //Hàm getter get info() { return this.infomation; } //Hàm setter set info(i) { this.infomation = i; } //Phương thức tĩnh static convertMoney(m) { console.log(m); return m + ' đồng'; } } //SỬ DỤNG LỚP //Tạo một đối tượng từ lớp bằng new let sanpham = new Product('Iphone', 1000); //truy cập thuộc tính đối tượng sanpham.name console.log(sanpham.name); //gọi một phương thức của đối tượng sanpham.checkStore(100); //Gọi setter sanpham.info = 'Thông tin sản phẩm ...'; //Gọi getter console.log(sanpham.info); //Gọi một hàm tĩnh Product.convertMoney(100000); Từ đoạn mã trên lưu ý vài vấn đề
Tính kế thừa của lớpGiống như nhiều ngôn ngữ lập trình hướng đối tượng, JS cung cấp khả năng tạo ra một lớp mới có kế thừa thuộc tính, phương thức của một lớp khác. To create a class con next from the layer cha used from class Computer extends Product { constructor(name, price, store) { super(name, price); this.store = store; } set info(i) { //super.info(i) - nếu muốn thi hành phương thức của lớp cha this.infomation = name + ':'+i; } totalInStore() { console.log('totalInStore'); } } //Sử dụng let sanpham = new Computer('Dell', 2000); console.log(sanpham.name); sanpham.checkStore(200); sanpham.info = 'Thông tin sản phẩm ...'; console.log(sanpham.info); sanpham.totalInStore();2 class Computer extends Product { constructor(name, price, store) { super(name, price); this.store = store; } set info(i) { //super.info(i) - nếu muốn thi hành phương thức của lớp cha this.infomation = name + ':'+i; } totalInStore() { console.log('totalInStore'); } } //Sử dụng let sanpham = new Computer('Dell', 2000); console.log(sanpham.name); sanpham.checkStore(200); sanpham.info = 'Thông tin sản phẩm ...'; console.log(sanpham.info); sanpham.totalInStore(); Ở ví dụ trên bạn đã tạo ra một lớp mới có tên class Computer extends Product { constructor(name, price, store) { super(name, price); this.store = store; } set info(i) { //super.info(i) - nếu muốn thi hành phương thức của lớp cha this.infomation = name + ':'+i; } totalInStore() { console.log('totalInStore'); } } //Sử dụng let sanpham = new Computer('Dell', 2000); console.log(sanpham.name); sanpham.checkStore(200); sanpham.info = 'Thông tin sản phẩm ...'; console.log(sanpham.info); sanpham.totalInStore();3 kế thừa lớp cha là class Computer extends Product { constructor(name, price, store) { super(name, price); this.store = store; } set info(i) { //super.info(i) - nếu muốn thi hành phương thức của lớp cha this.infomation = name + ':'+i; } totalInStore() { console.log('totalInStore'); } } //Sử dụng let sanpham = new Computer('Dell', 2000); console.log(sanpham.name); sanpham.checkStore(200); sanpham.info = 'Thông tin sản phẩm ...'; console.log(sanpham.info); sanpham.totalInStore();4, do đó lóp con có các thuộc tính, phương thức như Lớp cha và khai báo thêm các thuộc tính của phương thức riêng |