week10 - Sakura01210/wp109b GitHub Wiki

物件導向

add(a,b)
var c=[]
for(i=0;i<a.lenght;i++)
{
    c[i]=a[i]+b[i]
}
return c;

加減內積負

class Vector
{
    constructor(array)
    {
        this.a=array;
    }
    add(y)
    {
        var r=[]
        var x=this
        for(var i=0;i<x.a.length;i++)
        {
            r[i]=x.a[i]+y.a[i]
        }
        return new Vector(r)
    }
    sub(y)
    {
        var r=[]
        var x=this
        for(var i=0;i<x.a.length;i++)
        {
            r[i]=x.a[i]-y.a[i]
        }
        return new Vector(r)

    }
    dot(y)
    {
        var r=[]
        var x=this
        var g=0
        for(var i=0;i<x.a.length;i++)
        {
            r[i]=x.a[i]*y.a[i]
            g+=r[i]
        }
        return g
    }
    neg(y)
    {
        var r=[]
        var x=this
        for(var i=0;i<x.a.length;i++)
        {
            r[i]=-x.a[i]
        }
        return new Vector(r)
    }
}

var x=new Vector([1,2,3])
var y=new Vector([1,1,1])
console.log("x.add(y)=",x.add(y))
console.log("x.sub(y)=",x.sub(y))
console.log("x.dot(y)=",x.dot(y))
console.log("x.neg=",x.neg(y))

animal

class Dog extends Animal
{
    constructor()
    {
        super('狗')
    }
  say() {console.log(this.name,'旺旺)}
}

class Animal
{
   constructor(name)
   {
       this.name=name;
   }
   say()  {}
}

home

⚠️ **GitHub.com Fallback** ⚠️