js---17继承中方法属性的重写

function F(){};
var f = new F();
f.name = "cf";
f.hasOwnProperty("name");//true
F.prototype.age = 22;
f.hasOwnProperty("age");//false,age是原型对象的属性,name是F对象的属性,不是同一个。hasOwnProperty是对象的方法
F.prototype.isPrototypeOf(f);//true

//多态:编译时多态,运行时多态:方法重载、重写
//js不支持同名的方法
var o = {
run:function(){},
run:function(){},  //js同名会覆盖,run指向的是这个函数对象的地址,地址名加小括号就是这个类对象开始执行。function F(){} 既是类也是对象,new F()说明是一个类,F()函数名加小括号就是函数执行,说明是一个对象。
}

=======================================================================

function demo (a,b) {
    console.log(demo.length);//形参个数,2
    console.log(arguments.length);//实参个数,3
    console.log(arguments[0]);
    console.log(arguments[1]);
}
demo(4,5,6);

function add(){
    var total = 0;
    for (var i = arguments.length - 1; i >= 0; i--) {
        total += arguments[i];
    };
    return total;
}
console.log(add(1));
console.log(add(1,2));//可变长度

function fontSize(){
    var ele = document.getElementById("js");
    if (arguments.length == 0){
        return ele.style.fontSize;
    }else{
        ele.style.fontSize = arguments[0];

    }
}
fontSize(18);
console.log(fontSize());

function setting(){
     var ele = document.getElementById("js");
     if (typeof arguments[0] ==="object"){
         for(p in arguments[0]){//p是key,arguments[p]是value,
            ele.style[p] = arguments[0][p];
        }
    }else{
    ele.style.fontSize = arguments[0];
    ele.style.backgroundColor= arguments[1]
;    }
}
setting(18,"red");
setting({fontSize:20,backgroundColor:"green"});//js里面不能写同名的方法,所以只能够对方法的参数做判断,

==========================================================================

function demo(o){//demo是一个类,o是类的对象属性
     o.run();//调用属性的方法
}
var o = {run:function(){
    console.log("o is running...");
}};
demo(o);//类调用,java里面类不可以调用,这是跟java不一样的。

var p ={run:function(){
    console.log("p is running...");
}};
demo(p);//函数是一个类也是一个对象,函数调用对象就会执行起来。

function F(){};
var f = new F();
F.prototype.run = function(){console.log("111");}//原型区域,对象. 可以访问
f.run();//111
f.run = function(){console.log("222");};//只是给f自己加了一个方法,没有改变类的原型对象,相当于方法的重写。f.什么都是给自己对象加的
f.run();//222
F.prototype.run();//111
f.run = function(){//run指向一个对象的地址
    console.log("222");
    F.prototype.run();//重写父的,并且还要调用父的,
};
f.run();//222 , 111

=======================================================================

function Parent(){
    this.run = function(){//现在把Parent当成类看,run是一个对象的地址,
        console.log("parent is running...");
    }
}

function Child(){
     Parent.call(this);//继承父的方法,相当于父有了一个run方法,this.run = function(){console.log("parent is running...");},但是2个方法不是同一个,只是相当于把父的属性和方法在这里写了一遍。
     var parentRun = this.run;//用this,parentRun指向run函数的地址,
     this.run = function (){//run重新指向,重写,添加同名的子类方法
         console.log("child is running...");
         parentRun();//地址名小括号就是对象的执行
     }
}

var c = new Child();//Child看成是类,c是对象
c.run();//run是函数的地址,地址小括号就是对象执行

========================================================================
function Parent(){
    this.name = "333";//只能通过 对象.name 访问
    age = 34;//给嵌套函数使用
}
var p = new Parent();

console.log(p.name);//333
console.log(Parent.name);//Parent
console.log(p.age);//undefined,
console.log(Parent.age);//undefined, 

Parent.aa = "aa"; //静态属性,对象. 访问不到,类. 访问得到
Parent.prototype.aaa = "asa";//原型公有区域,对象. 访问得到,类. 访问不到

console.log(p.aa);//undefined,
console.log(Parent.aa);//aa
console.log(p.aaa);//asa,
console.log(Parent.aaa);//undefined

p.zz = "zz";//只是加给了对象自己,没有加给类和类的原型
console.log(p.zz);//zz
console.log(Parent.zz);//undefined
console.log(Parent.prototype.zz)//undefin
==========================================================================

function Parent(){

}
Parent.prototype.run = function() {
    console.log("parent");
};

Child.prototype = Object.create(Parent.prototype);//继承
Child.prototype.constructor = Child;//修正
Child.super  = Parent.prototype; //给Child增加静态属性

function Child(){

}

Child.prototype.run=function(){
    console.log(‘child is running...‘);
    Child.super.run();
}

var c = new Child();
c.run();

1.类里面的this.属性给对象用的,静态属性给类用,未加修饰符的给嵌套函数用。(静态属性通过F.xx 添加)

2.对象的静态属性给自己用。(静态属性通过 p.xx 添加)

3.原型里面的都是给对象和原型自己用的。(通过 F.prototype.xx 添加)

时间: 2025-01-08 03:20:55

js---17继承中方法属性的重写的相关文章

java基础疑难点总结之成员变量的继承,方法重载与重写的区别,多态与动态绑定

1.成员变量的继承 1.1要点 子类用extends关键字继承父类.子类中可以提供新的方法覆盖父类中的方法.子类中的方法不能直接访问父类中的私有域,子类可以用super关键字调用父类中的方法.在子类中可以增加域,增加方法或者覆盖超类的方法,然而绝对不能删除继承的任何域和方法. 在一个子类被创建的时候,首先会在内存中创建一个父类对象,然后在父类对象外部放上子类独有的属性,两者合起来形成一个子类的对象.所以所谓的继承使子类拥有父类所有的属性和方法其实可以这样理解,子类对象确实拥有父类对象中所有的属性

Js实现继承的方法

原型的作用:1.将公共部分放入原型中,这样构造出的多个实例对象的公共部分只会占用一个公共空间,实现数据共享和节省内存空间 2.通过原型实现继承:构造函数模拟 "类"这个面向对象的概念,JS基于对象,基于构造函数的原型对象实现继承 如何实现继承?1.改变原型对象的指向:将子类构造函数(B)的prototype指向父类构造函数(A)的一个实例化对象(a),那么这个子类构造函数构造出的实例化对象(b)就可以访问父类(A)的属性和方法 缺陷:由于B的prototype改变,那么保存在原来的B的

5.Java继承中方法的覆盖和重载

在类继承中,子类可以修改从父类继承来的方法,也就是说子类能创建一个与父类方法有不同功能的方法,但具有相同的名称.返回值类型.参数列表. 如果在新类中定义一个方法,其名称.返回值类型和参数列表正好与父类中的相同,那么,新方法被称做覆盖旧方法. 参数列表又叫参数签名,包括参数的类型.参数的个数和参数的顺序,只要有一个不同就叫做参数列表不同. 被覆盖的方法在子类中只能通过super调用. 注意:覆盖不会删除父类中的方法,而是对子类的实例隐藏,暂时不使用. 请看下面的例子: public class D

继承中方法的覆盖

执行下边的代码: class Parent2{ public void Print() { System.out.println("今天是个好日子!!"); } } class Child2 extends Parent2{ public void Print() { //super.Print(); System.out.println("今天不是个好日子!!"); } } public class super1 { public static void main

C#继承中的override(重写)与new(覆盖)用法

刚接触C#编程,我也是被override与new搞得晕头转向.于是花了点时间翻资料,看博客,终于算小有领悟,把学习笔记记录于此. 首先声明一个父类Animal类,与继承Animal的两个子类Dog类与Cat类.父类Animal中有一个Say方法,而子类Dog与Cat分别override(重写)与new(覆盖)了Say方法. 让我们通过实例来看一下这两者有什么相同与不同之处. 1 public class Animal 2 { 3 public virtual void Say() 4 { 5 C

继承中静态方法不能被重写

本文链接:https://blog.csdn.net/gao_zhennan/article/details/72892946 解释的很清楚 答案很明确:java的静态方法不能被重写. 静态成员(方法或属性)是类的成员存放在栈中,类可以直接调用(是属于类的静态成员,当然对象也可以调用,只是说你可以使用而已):实例成员是对象的成员,存放在堆中,只能被对象调用. 重写的目的在于根据创造对象的所属类型不同而表现出多态.因为静态方法无需创建对象即可使用.没有对象,重写所需要的“对象所属类型” 这一要素不

类继承中的 隐藏和重写的 区别

我看先看一个例子: public class A{    public int a = 10;    public virtual void Fun1()    {        Console.WriteLine("1");  -------A1    } public virtual void Fun3()    {        Console.WriteLine(this.a); -------A2 }    public void Fun2()    {        Fun

关于 python 类与继承中方法调用 的 一个小知识点记录

(1)D类,多继承于C类与B类,C类与B类继承于A类.C类中没用__init__(), C类的实例化会先寻找第一个继承类是否存在__init__(),也就是B类的__init__().因为python3中使用的是广度优先的方法,寻找路径为D-->B-->C-->A 关于其他方法的继承,也是这个顺序. class A(object): def __init__(self): print("i am A") def call(self): print("A CA

分享一个js原型继承的方法

function Person(){ this.color = [23,2,3,32] } undefined var p = Object.create(new Person()) undefined p.color.push(1)