class 练习

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

 

posted @ 2022-12-28 15:40  奔跑的哈密瓜  阅读(32)  评论(0编辑  收藏  举报