Cannot access ‘androidx.lifecycle.HasDefaultViewModelProviderFactory’ which is a supertype of ‘com.example.a19
ES5 继承先定义一个父类function SuperType () { // 属性 this.name = 'SuperType';}// 原型方法SuperType.prototype.sayName = 'SuperType'; // 父类属性}SuperType.prototype.sayName = function () { // 父类原型方法 return this.name;};// 子类 ; // true// 注意SubType instanceof SuperType; // falseSubType.prototype instanceof SuperType ; // true图片特点 = "SuperType"; } // 子类 function SubType () {} // 原型链继承 SubType.prototype = new SuperType(); // return this.name;};// 子类function SubType (name, subName) { // 调用 SuperType 构造函数 SuperType.call(this
ES5 继承 先定义一个父类 function SuperType () { // 属性 this.name = 'SuperType'; } // 原型方法 SuperType.prototype.sayName = 'SuperType'; // 父类属性 } SuperType.prototype.sayName = function () { // 父类原型方法 return this.name; } ; // true // 注意 SubType instanceof SuperType; // false SubType.prototype instanceof SuperType ; // true () { // 调用 SuperType 构造函数 SuperType.call(this, 'SuperType'); // 在子类构造函数中,向父类构造函数传参 // 为了保证子父类的构造函数不会重写子类的属性 构造函数 SuperType.call(this, name); // ----第二次调用 SuperType,继承实例属性---- this.subName = subName; }; /
原型链继承 function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function ()); // 继承了 SuperType 的 getSuperValue 方法,打印 true 缺点 如果 SuperType 存在一个引用类型的属性,而 SubType 的原型对象变为 SuperType // 继承了 SuperType SuperType.call(this); // 通过 apply() 或 call() 执行 SuperType 的构造函数 } var instance1 , age){ // 执行 SuperType 的构造函数,继承 SuperType 的属性 SuperType.call(this, name); this.age = age ; } // 将 SuperType 的实例赋给 SubType 的原型对象,这样 SubType 可继承 SuperType 原型中的方法 SubType.prototype = new SuperType
function SuperType () { this.property = true; } SuperType.prototype.getSuperValue = function () { function SuperType () { this.property = true; } SuperType.prototype.getSuperValue = function () { function SuperType () { this.property = true; } SuperType.prototype.getSuperValue = function () { 实现 经典继承 SuperType.call(this); } let instance1 = new SuperType(); instance1.colors.push('black'); function inheritPrototype (subType, superType) { let prototype = Object.create(superType.prototype)
function SuperType () { this.property = true; } SuperType.prototype.getSuperValue = function () { 但是你注意到了吗,这里我们把父类方法也写在了SuperType()构造函数里面,可以像前面一样写在SuperType.prototype上吗? 答案是不可以,必须写在SuperType()构造函数里面。 调用了两次父类构造函数,一次通过SuperType.call(this)调用,一次通过new SuperType()调用。 4. /第二次调用 SuperType() this.age = age; } SubType.prototype = new SuperType(); //第一次调用 SuperType() SubType.prototype.constructor
function SuperType() { this.b = [1, 2, 3]; } function SubType() {} SubType.prototype = new SuperType function SuperType(name) { this.name = name; this.b = [1, 2, 3]; } SuperType.prototype.say = function function SuperType(name) { this.name = name; this.a = "HZFE"; this.b = [1, 2, 3, 4]; } SuperType.prototype.say 第二次调用 SuperType } SubType.prototype = new SuperType(); // 第一次调用 SuperType SubType.prototype.constructor function inheritPrototype(subType, superType) { var prototype = Object.create(superType.prototype);
下面是一个实现原型链的基本方法: function SuperType() { this.property = true } SuperType.prototype.getSuperValue = 两种类型,每个类型分别有一个属性和一个方法,SubType 通过改写原型对象的方式实现对 SuperType 的继承。 原来存在于 SuperType 中的属性和方法,现在也存在于 SubType.prototype 中。 SuperType 的实例所拥有的全部属性和方法,而且其内部还有一个指针,指向了 SuperType 的原型。 __proto__ === SuperType.prototype //true instance.__proto__.__proto__ === SuperType.prototype //true
第一种:组合式继承: function SuperType(name) { this.name = name; this.colors = ["red", "blue ", "green"]; } SuperType.prototype.sayName = function() { console.log(this.name); }; function SubType(name, age) { //通过call()调用SuperType的构造函数,继承SuperType属性 SuperType.call = new SuperType(); //第一次调用 SubType.prototype.sayAge = function() { console.log(this.age function inheritPrototype(subType, superType) { var prototype = object(superType.prototype);
// 原型链 function SuperType() { this.property = true; } SuperType.prototype.getSuperValue 和SubType,这里SubType就是继承了SuperType。 这个赋值重新更改了SUbType的最初原型,替换成了SuperType实例。这样意味着SuperType实例可以访问所有的属性和方法也存在与SubType.protoype。 这样一来,SubType 的实例不仅能从 SuperType 的实例中继承属性和方法,而且还与 SuperType 的原型挂上了钩。 SuperType.prototype。
function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; }; function SubType() { this.subproperty = false; } // 继承SuperType SubType.prototype function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return ) { } // 继承SuperType SubType.prototype = new SuperType(); let instance1 = new SubType(); instance1.
function SuperType() {} function SubType() {} SubType.prototype = new SuperType(); var instance = new function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return 的实例,因此,新原型具有作为SuperType实例所拥有的全部实现和方法,并且指向SuperType的原型,因此,instance实例具有subproperty属性,SubType.prototype具有 function SuperType() { this.colors = ["red","blue","green"] } function SubType() { SuperType.call function SuperType(name) { this.name = name; } function SubType() { SuperType.call(this,"Nicholas
function SuperType() { this.name = "张三"; } SuperType.prototype.getSuperName = function() { return ,又因为SubType的原型对象又指向SuperType原型对象的属性,因此可得,instance继承了SuperType原型的所有属性。 SubType.prototype = new SuperType(); 这里调用完之后,SubType.prototype会从SuperType继承到2个属性:name和colors。 重点就在于,不需要为了定义SubType的原型而去调用SuperType构造函数,此时只需要SuperType原型的一个副本,并将其赋值给SubType的原型即可。 在于,「只需要调用一次SuperType构造函数」。
__proto__ === SuperType.prototype // true // 继承自上帝(天赋) SuperType. function SuperType() { } function SubType() { SuperType.call(this) // 构造函数继承 } SuperType.prototype.fn function SuperType() {} function SubType() {} SuperType.prototype.fn = ()=>{} SubType.prototype = new SuperType() // 原型链继承 let s1 = new SubType() console.log(s1.fn) // ()=>{} function SuperType() { SubType(name, age){ SuperType.call(this, name) // 构造函数继承 this.age = age; } SuperType.prototype.sayName
超类型 23 * @return {null} 24 */ 25 function inheritPrototype(subType, superType) { 26 var prototype = object(superType.prototype); // 创建对象 27 prototype.constructor = subType; // 增强对象 28 subType.prototype = prototype; // 指定对象 29 } 30 31 function SuperType(name) { 32 this.name = name ; 33 this.colors = ["red", "blue", "green"]; 34 } 35 36 SuperType.prototype.sayName = function() { 37 alert(this.name); 38 }; 39 40 function SubType(name, age) { 41 SuperType.call(this, name
function SuperType () { this.superName = 'superName' } SuperType.prototype.sayName = function () 的一个实例指定为了SubType的原型,因此subA与new SuperType()与 SuperType.prototype之间就有了一条原型链,subA因此可以访问到链条能触及的所有属性和方法。 构造函数,将SuperType中的属性复用到SubType中。 但是,如只是简单的提取,则会产生一个问题,SuperType中的this在SuperType被定义时,即已确定。 为了让SuperType在调用时,其包含的属性能够正确的复用到SubType中,只需要将SuperType的执行环境绑定到SubType上即可。
写个简单的例子: // 父类function SuperType() { this.property = "super"; }// 在父类的原型上定义方法SuperType.prototype.getSuperVal () -> SuperType.prototype -> Object.prototype -> null。 代码实现: // 父类function SuperType() { this.users = ["Jack", "Tom"]; }// 子类function SubType() { // 继承 示例代码: // 父类function SuperType(company) { this.company = company; this.staffs = ["Jack", "Tom"]; SuperType(name) { this.name = name; }// 在父类的原型上定义方法SuperType.prototype.getName = function () {
实现原型链的基本模式 function SuperType(){ this.property=true; } SuperType.prototype.getSuperValue=function( 在例子代码中,定义了两个对象,subType和superType。 两个对象之间实现了继承,而这种继承方式是通过创建SuperType的实例并将该实例赋给subType.prototype实现的。 这样subType.prototype中就会存在一个指针指向superType的原型对象。也就是说,存在superType的实例中的属性和方法现在都存在于subType.prototype中了。 这个例子中的SuperType 构造函数定义了一个colors 属性,该属性包含一个数组(引用类型值)。SuperType 的每个实例都会有各自包含自己数组的colors 属性。 当SubType 通过原型链继承了SuperType 之后,SubType.prototype 就变成了SuperType 的一个实例,因此它也拥有了一个它自己的colors 属性——就跟专门创建了一个
(){ this.property = true; } SuperType.protoype.getSuperValue = function(){ return function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue SubType.prototype = new SuperType(); // 添加新方法 SubType.prototype.getSubValue = function(){ SuperType.call(this,name); // 第二次调用SuperType this.age = age; } SubType.prototype = new SuperType(); // 第一次调用 SuperType SubType.prototype.constructor = SubType; subType.prototype.sayAge
; }; function SubType(){ this.subproperty = false; } //继承了SuperType SubType.prototype = new SuperType 于是,新原型不仅具有作为一个SuperType 的实例所拥有的全部属性和方法,而且其内部还有一个指针,指向了SuperType 的原型。 /继承了SuperType SuperType.call(this); } var instance1 = new SubType(); instance1.colors.push("black"); 借用构造函数向超类传递参数: function SuperType(name){ this.name = name; } function SubType(){ //继承了SuperType,同时还传递了参数 SuperType 的原型定义了一个方法sayName()。SubType 构造函数在调用SuperType 构造函数时传入了name 参数,紧接着又定义了它自己的属性age。