首页 > 其他分享 >class 练习

class 练习

时间:2022-12-28 15:56:37浏览次数:31  
标签:console name 练习 new super class log

class polygon {
        constructor(width,height){
            this.name = '测试构造';
            this.height = height;
            this.width = width;
        }
        sayName(){
            console.log("hello,我是"+this.name+"。");
        }
        sayHistory() {
            console.log('"Polygon" is derived from the Greek polus (many) ' +
              'and gonia (angle).');
          }
    }
    const polygon_ = new polygon(200,300);
    polygon_.sayName();//hello,我是测试构造。
    console.log(polygon_.name+"的宽为:"+polygon_.width);//测试构造的宽为:200
    // =====================================================
    class ploy{
        getployname(){
            console.log("my name is "+ploy.name);
        }
    }
    let init = new ploy();
    init.getployname();//my name is ploy
    // =====================================================
    class square extends polygon{
        constructor(lengths){
            super(lengths,lengths);
            this.name = "square";
        }
        get area(){
            return this.width*this.width;
        }
        set area(value){
            this.area = value
        }
    }
    let s = new square(2);
    s.sayName();//hello,我是square。
    console.log("square is area :" + s.area);//square is area :4
    // ======================================================
    class Rectangle extends polygon{
        constructor(height,width){
            super(height,width);
            this.name = "Rectangle";
        }
        sayName(){
            console.log("haha, my name is " + this.name);
            super.sayHistory();
        }
    }
    let r = new Rectangle(3,2);
    r.sayName();//haha, my name is Rectangle
                //constructor.html:21 "Polygon" is derived from the Greek polus (many) and gonia (angle).
    // ======================================================
    class Triple {
        static triple(n){
            n = n ||1;
            return n*3;
        }
    }
    class BiggerTriple  extends Triple{
        static triple(n){
            return  super.triple(n) * super.triple(n);
        }
    }
    console.log(Triple.triple); // ƒ triple(n){
                                //    n = n ||1;
                                //    return n*3;
                                // } 
    console.log(BiggerTriple.triple(2));//36
    console.log(Triple.triple(3));//9
    // ========================================================
    class myDate extends Date{
        constructor(){
            super();
        }
        getFormattedDate(){
            var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec']
            return this.getDate() +'-'+ months[this.getMonth()] + '-'+this.getFullYear();
        }
    }
    const aData = new myDate();
    console.log(aData.getTime());//1672212971748
    console.log(aData.getFormattedDate());//28-Dec-2022
    // ========================================================
    class ExtendedUint8Array extends Uint8Array {
        constructor(){
            super(30);
            this[0] = 215;
            this[1] = 0xFFA;
        }
    }
    const eua  = new ExtendedUint8Array();
    console.log(eua.byteLength)//30
    // =========================================================
    class Myaudio extends Audio {
        constructor(){
            super();
            this._lyrics  = "";
        }
        get lyrics() {
            return this._lyrics;
        }
        
        set lyrics(str) {
            this._lyrics = str;
        }
    }
    const player = new  Myaudio();
    player.controls  = true;
    player.lyrics = 'Never gonna give you up';
    document.querySelector('body').appendChild(player);
    console.log(player.lyrics);//Never gonna give you up
    // =============================================================
    class stack extends Array{
        constructor(){
            super();
        }
        top(){
            return this[this.length - 1];
        }
    }
    const stack_ = new stack();
    stack_.push("hello");
    stack_.push("world");
    console.log(stack_.top());//world
    console.log(stack_.length);//2

 

标签:console,name,练习,new,super,class,log
From: https://www.cnblogs.com/lisir-blogshare/p/17010299.html

相关文章

  • 青少年CTF练习9
    T9  哥哥打篮球(经典小黑子)题目描述:你想想怎么把篮球和鸡,联系起来?flag格式为qsnctf{}(对此我只能说6)打开靶机    太6了!!!然后在调试器中找到js源代码,直接......
  • 考研数学练习题-2022年12月27日
    数量:10......
  • Kettle 连接 MySql 驱动:Driver class 'org.gjt.mm.mysql.Driver' could not be found
    异常:Driverclass'org.gjt.mm.mysql.Driver'couldnotbefound,makesurethe'MySQL'driver(jarfile)isinstalled.org.gjt.mm.mysql.DriverLink1:https://mvnrep......
  • 青少年CTF练习8
    T8 CheckMe04    题目难度:★题目描述:世界上有一种东西,可以让两个不同的东西相等,你知道是什么吗?(盲猜弱类型)果然,MD5弱类型比较!!!打开靶机 补充知识点:  ......
  • 青少年CTF练习7
    T7  CheckMe03题目难度:★题目描述:可恶!上回让你过关了,这次,我不允许你使用伪协议!!(虽然有跟没有一样,只是你找不到对应的refer)那也没事,只要记住根目录下怎么写就好了打开靶......
  • MMClassification 实践笔记
    1.配置环境参考文档:https://mmclassification.readthedocs.io/zh_CN/dev-1.x/get_started.htmlgitclone-b1.xhttps://github.com/open-mmlab/mmclassification.git......
  • cpp jsoncpp serialize vector class into plain text file and deserialize from pla
    //model/book.h#pragmaonce#ifndef__book_h__#define__book_h__#include<iostream>usingnamespacestd;structbook{uint64_tidx;uint64_tid;......
  • Android学习day02【页面布局的练习】
    在网上找了一些图片,只用最简单的颜色进行区分,目的是熟悉线性布局和相对布局下面是我找到的简单的Android页面,你也可以尝试以下‘  下面是我的实现代码第一个 ......
  • Java入门练习2
    1、循环importjava.util.Scanner;publicclassHello{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);int......
  • 2022.12.26动态规划练习
    洛谷P1091[NOIP2004提高组]合唱队形题目描述\(n\)位同学站成一排,音乐老师要请其中的\(n-k\)位同学出列,使得剩下的\(k\)位同学排成合唱队形。合唱队形是指这样的......