JS面向对象的理解

article/2025/9/18 11:29:40

JS面向对象的理解

  • 1.理解对象
    • 1.1.new 操作符 + Object 创建对象
    • 1.2.字面式创建对象
  • 2.创建对象
    • 2.1.工厂模式
    • 2.2.构造函数模式
    • 2.3.原型模式
      • 2.3.1.原型模式
      • 2.3.2.理解原型对象
      • 2.3.3.原型与in操作符
      • 2.3.4.更简单的原型语法
      • 2.3.5.原型的动态性
      • 2.3.6.原型对象的原型
      • 2.3.7.原型对象的问题
    • 2.4.组合使用构造函数模式和原型模式
    • 2.5.动态原型模式
    • 2.6.寄生构造函数模式
    • 2.7.稳妥构造函数模式
  • 3.继承
    • 3.1.原型链
      • 3.1.1.原型链
      • 3.1.2.别忘记默认的原型
      • 3.1.3.确定原型和实例的关系
      • 3.1.4.谨慎的定义方法
      • 3.1.5.原型链的问题
    • 3.2.借用构造函数
    • 3.3.组合继承
    • 3.4.原型式继承
    • 3.5.寄生式继承
    • 3.6.寄生组合式继承
  • 4.学习地址

1.理解对象

1.1.new 操作符 + Object 创建对象

var person = new Object();
person.name = 'lvxin';
person.age = 25,
person.job = 'UI development';
person.sayName =  function() {console.log(this);    		   // Object--> {name: 'lvxin', age:  25, job: 'UI development', sayName:function}console.log(this == person);   //trueconsole.log(this.name);  	   //lvxin	
}
console.log(person.sayName());  

1.2.字面式创建对象

var person = {name: 'lvxin',age: 25,job: 'UI Development',sayName: function() {console.log(this);             // Object--> {name: 'lvxin', age:  25, job: 'UI development', sayName:function}console.log(this == person);   // trueconsole.log(this.name);        //lvxin	}
}
console.log(person.sayName());

可扩展this指向问题。
apply和call的用法和区别
以上两种方法在使用同一接口创建多个对象时,会产生大量重复代码,为了解决此问题,工厂模式被开发。

2.创建对象

2.1.工厂模式

  1. 无法确定对象的类型,每次都是调用Object
  2. 创建多个对象之间没有关联
function createPerson(name,age,job) {var o = new Object();o.name = name;o.age = age;o.job = job;o.sayName = function() {console.log(this.name);}return o;
}
var person1 = createPerson('lvxin',18,'web');
var person2 = createPerson('xuyuqiu',20,'web');console.log(typeof person1);//Object  只能知道是Object类型,不知道是其他具体得Person
console.log(typeof person2);//Object
console.log(person1 === person2);//false
console.log(person1.sayName === person2.sayName)//false
console.log(person1 instanceof Object); //true

工厂模式解决了重复实例化多个对象的问题,但没有解决对象识别的问题(但是工厂模式却无从识别对象的类型,因为全部都是Object,不像Date、Array等,本例中,得到的都是o对象,对象的类型都是Object,因此出现了构造函数模式)。

2.2.构造函数模式

对比工厂模式有以下不同之处:

  1. 没有显式地创建对象
  2. 直接将属性和方法赋给了 this 对象
  3. 没有 return 语句

以此方法调用构造函数步骤 {
1. 创建一个新对象
2. 将构造函数的作用域赋给新对象(将this指向这个新对象)
3. 执行构造函数代码(为这个新对象添加属性)
4. 返回新对象
}

function Person(name,age,job) {this.name = name;this.age = age;this.job = job;this.sayName = function() {console.log(this.name);}
}
var person1 = new Person('lvxin',18,'web');
var person2 = new Person('xuyuqiu',20,'web');console.log(person1.constructor == Person);//true
console.log(person2.constructor == Person);//true
// 创建所有的对象都是Object实例,因为对象均继承Object
console.log(person1 instanceof Person);//true
console.log(person1 instanceof Object);//true
console.log(person2 instanceof Person);//true
console.log(person2 instanceof Object);//true

问题: 多个实例重复创建方法,无法共享,多个实例都有sayName方法,但均不是同一个Function的实例。

  1. 每一个实例都要重新创建一遍,person1和person2都有sayName的方法,但是是两个Function实例。
  2. sayName方法定义到外部来,就是全局的,那么person1和person2就是同一个方法了。
    解决方法:
function Person(name,age,job) {this.name = name;this.age = age;this.job = job;//this.sayName = function() {//	console.log(this.name);//};//this.sayName = new Function("console.log(this.name)");this.sayName = sayName;
}
function sayName() {console.log(this.name);
}
var person1 = new Person('lvxin',18,'web');
var person2 = new Person('xuyuqiu',20,'web');
console.log(person1.sayName == person2.sayName);//true

2.3.原型模式

2.3.1.原型模式

  1. 每个函数(构造函数)都有一个prototype(原型)属性
  2. prototype(原型)属性是一个指针,指向一个对象
  3. prototype(原型)属性的这个对象包括所有特定类型(自己定义的构造函数)的所有实例(new自己定定义的函数)的属性和方法。
//空的构造函数
function Person () {}
//所有Person的实例共享这些属性和方法
Person.prototype.age = 18;
Person.prototype.name = "lvxin";
Person.prototype.jos = "web";
Person.prototype.sayName = function() {console.log(this.name);
}
var person1 = new Person();
var person2 = new Person();
console.log(person1.sayName());//lvxin
console.log(person2.sayName());//lvxin
console.log(person1.sayName() == person2.sayName());

2.3.2.理解原型对象

  1. 所有原型都会自动获得一个constructor(构造函数属性),Person.prototype.constructor.
  2. Person(构造函数)的prototype属性指向Person Prototype(Person的原型)
  3. Person Prototype有构造函数(constructor)指向Person。
  4. person1有prototype属性指向Person Prototype,但没有自己的构造函数。
  5. isPrototypeOf(),返回true/false,判断实例是否拥有某个原型
  6. Object.getPrototypeOf(),返回定义原型的字符名称(Preson.prototpe),得到实例的原型名称
    在这里插入图片描述
function Person() {}Person.prototype.name = 'lvxin';Person.prototype.age = 18;Person.prototype.job = 'web';Person.prototype.sayName = function() {console.log(this.name);}
var person1 = new Person();
var person2 = new Person();
console.log(Person.prototype.isPrototypeOf(person1));//true
console.log(Person.prototype.isPrototypeOf(person2));//trueconsole.log(Object.getPrototypeOf(person1) == Person.prototype);//true
console.log(Object.getPrototypeOf(person1).name);
  1. 寻找属性和方法的过程:从对象实例本身开始,再看原型对象,再看object对象
  2. 不能用对象实例重写原型的值,实例和原型属性同名,实例会屏蔽原型的值。
  3. 可以用 delete删除实例的属性
  4. hasOwnPrototype(),返回true/false,判断是否有自己的原型属性,即实例有自己的属性。
person1.name = "xuyuqiu";
console.log(person1.name);//xuyuqiu 实例值
console.log(person2.name);//lvxin  原型值
delete person1.name;
console.log(person1.name);//lvxin 原型值person1.name = "duanyujie";
console.log(person1.hasOwnPrototype(name));//true;
console.log(person2.hasOwnPrototype(name));//false;
delete person1.name;
console.log(person1.hasOwnPrototype(name));//false;

2.3.3.原型与in操作符

/**
* 1.in用法:判断属性是否存在* 2.in 无论实例自己的属性还是实例用原型的属性,都返回true* 3.通过 in返回true和hasOwnPrototype()返回false,判断是原型上的属性* 4.hasPrototypeProperty(实例,属性),返回true/false,判断是否是原型上的属性。*/function Person() {}Person.prototype.name = 'lvxin';Person.prototype.age = 18;Person.prototype.job = 'web';Person.prototype.sayName = function() {console.log(this.name);}
var person1 = new Person();
var person2 = new Person();person1.name = "xuyuqiu";
console.log('name' in person1);//true;
console.log('name' in person2);//true;console.log(person2.hasOwnProperty(name));//false 判断person2.name 来自原型//console.log(hasPrototypeProperty(person1,'name'));//false;
//console.log(hasPrototypeProperty(person2,'name'));//true;/*** 1.Object.keys(对象),返回数组,找到对象的可以枚举的属性名* 2.Object.getOwnPropertyNames(对象),返回数组,找到对象的可以枚举或者不可枚举的属性名*/
var keysArr = Object.keys(Person.prototype);//[name,age,job,sayName];
console.log(keysArr);person1.name = "duanyujie";
person1.age = 20;
//实例没有的自生属性
var person1KeysArr = Object.keys(person1);//[name,age]
console.log(person1KeysArr);//constructor 不可枚举
var keys = Object.getOwnPropertyNames(Person.prototype);//[constructor,name,age,job,sayName];
console.log(keys);

2.3.4.更简单的原型语法

/*** `1.创建一个函数同时会创建一个prototype对象* 2.赋值整个对象是重构一个对象,一个一个赋值是修改本来的值*/
function Person() {}
Person.prototype = {name:'lvxin',age:18,job:"web",sayName:function() {console.log(this.name);}
}
var person1 = new Person();
console.log(person1 instanceof Person);//true;
console.log(person1 instanceof Object);//true;
console.log(person1.constructor instanceof Person);//false;
console.log(person1.constructor instanceof Object);//true;console.log(Object.getOwnPropertyDescriptor(Person.prototype,"constructor"));//undefined/*** 1.修改实例的constructor指向有两种方法* 		1-1:直接写个属性constructor:Person* 		1-2:Object.defineProperty(对象,属性名,修改的列值)*/
function Person2() {}
Person2.prototype = {constructor:Person2,name:'lvxin',age:18,job:"web",sayName:function() {console.log(this.name);}
}
var person2 = new Person2();
console.log(person2 instanceof Person2);//true;
console.log(person2 instanceof Object);//true;
console.log(person2.constructor instanceof Person2);//false
console.log(person2.constructor instanceof Object);//true;console.log(Object.getOwnPropertyDescriptor(Person2.prototype,"constructor"));//enumerable:true
Object.defineProperty(Person2.prototype,"constructor",{enumerable:false,value:Person2
});
console.log(person2.constructor instanceof Person2);//false;
console.log(Object.getOwnPropertyDescriptor(Person2.prototype,"constructor"));//enumerable:false
console.log(person2.constructor instanceof Person2);//f alse;

2.3.5.原型的动态性

/*** 1.动态性:先实例化,之后还可以加属性和方法,仍然可以访问到* 2.把原型修改成另一个对象,切断了现有的原型和之前已经存在的实例对象。* 3.创建一个构造函数,即有一个构造属性*/
function Person() {}
var person = new Person();
Person.prototype.sayHi = function() {console.log("say Hi");
}
person.sayHi();//say Hi
console.log(person.sayHi());//undefined   无返回值 function Person2() {}
Person2.prototype = {name:'lvxin',age:18,job:"web",sayName:function() {console.log(this.name);}
}
var person2 = new Person();
person2.sayName();//error    person2实例和新的原型Person2.prototype没有关系,person2实例和原来的原型有关

在这里插入图片描述

2.3.6.原型对象的原型

/*** 1.所有原生引用类型(object,Array,String)为原型对象的原型* */
console.log(typeof Array.prototype.sort);//function
console.log(typeof String.prototype.substring);//function//判断字符是否存在
String.prototype.startsWith = function (text) {console.log(this.indexOf(text));//0return this.indexOf(text) == 0;
};var msg = "Hello world!";
console.log(msg.startsWith('Hello'));//true

2.3.7.原型对象的问题

/*** 1.实例取得相同的属性值* 2.实例1修改了值,实例2值也会变*/function Person () {}
Person.prototype = {constructor:Person,name:'lvxin',age:18,job:'web',friends:['xuyuqiu','duanyujie'],sayName:function() {console.log(this.name);}
};
var person1 = new Person();
var person2 = new Person();person1.friends.push('lizhiying');//
console.log(person1.friends);//['xuyuqiu','duanyujie','lizhiying']
console.log(person2.friends);//['xuyuqiu','duanyujie','lizhiying']console.log(person1.friends === person2.friends);//true

2.4.组合使用构造函数模式和原型模式

function Person (name,age,job) {this.name = name;this.age = age;this.job = job;this.friends = ['xuyuqiu','duanyujie'];
}
Person.prototype = {constructor:Person,sayName:function() {console.log(this.name);}
}
//指向新的原型
var person1 = new Person('lvxin',18,'web');
var person2 = new Person('zhengling',19,'test');person1.friends.push('lizhiying');//
console.log(person1.friends);//['xuyuqiu','duanyujie','lizhiying']
console.log(person2.friends);//['xuyuqiu','duanyujie']console.log(person1.friends === person2.friends);//false
console.log(person1.sayName === person2.sayName);//true

2.5.动态原型模式

/*** 判断是否初始化构造函数*/
function Person(name,age,job) {this.name = name;this.age = age;this.job = job;//初次调用构造函数时if(typeof this.sayName != 'function') {//注意,不能使用字变量方式重写Person.prototype.sayName = function() {console.log(this.name);}}
}
var friend = new Person('xuyuqiu',18,'web');
friend.sayName();

2.6.寄生构造函数模式

/*** 1.概念:创建一个函数,封装对象代码,返回新建的代码* 2.不建议用这个方法*/
function Person (name,age,job) {var o = new Object();//var value = new Array();o.name = name;o.age = age;o.job = job;o.sayName = function()  {console.log(this.name);}return o;
}
var friend = new Person('xuyuqiu',19,'web');//xuyuqiu
friend.sayName();//xuyuqiu

2.7.稳妥构造函数模式

/*** 1.没有公共的属性,方法也不引用this对象,返回对象* 2特点:* 	方法不用this引用* 	实例不用new来创造* 3.安全性高,外部只可以通过调用内部函数,来访问属性的值*/
function Person(name,age,job) {//没有属性var o = new Object();o.sayName = function() {//不用thisconsole.log(name);}return o;
}
//不用new
var friend = Person('lvxin',18,'web');friend.sayName();

3.继承

3.1.原型链

3.1.1.原型链

/*** 1.定义:每个构造函数都有个prototype属性,指向原型对象,* 原型对象constructor属性,指向构造函数,实例指向原型对象,具有所有原型对象的方法和属性。* 2.原型链:(原型和实例之间的一个链条),第二个构造函数的原型对象为第一个构造函数的实例。*/
//定义父的构造函数(超类型函数)
function SuperType() {this.prototype = true;
}
SuperType.prototype.getSuperValue = function() {//console.log(this.prototype);return this.prototype;
};
//定义子类的构造函数(子类型函数)
function SubType() {this.subprototype = false;}//继承
SubType.prototype = new SuperType();SubType.prototype.getSubValue = function() {//console.log(this.subprototype);return this.subprototype;
};var instance = new SubType();
console.log(instance.prototype);//flase;
console.log(instance.getSuperValue());//true;
console.log(instance.getSubValue());//flase;
console.log(instance.toString());

在这里插入图片描述

3.1.2.别忘记默认的原型

//定义父的构造函数(超类型函数)
function SuperType() {this.prototype = true;
}
SuperType.prototype.getSuperValue = function() {//console.log(this.prototype);return this.prototype;
};
//定义子类的构造函数(子类型函数)
function SubType() {this.subprototype = false;}//继承
SubType.prototype = new SuperType();SubType.prototype.getSubValue = function() {//console.log(this.subprototype);return this.subprototype;
};var instance = new SubType();
console.log(instance.prototype);//flase;
console.log(instance.getSuperValue());//true;
console.log(instance.getSubValue());//flase;
/*** 所有函数的默认原型对象是Object,所以实例都会* toString();* hasOwnPrototype();* isPrototypeOf();* toLocalString();*/
console.log(instance.toString());//输出 是一个对象,SubType和SuperTpye都没有toString()方法

3.1.3.确定原型和实例的关系

//定义父的构造函数(超类型函数)
function SuperType() {this.prototype = true;
}
SuperType.prototype.getSuperValue = function() {//console.log(this.prototype);return this.prototype;
};
//定义子类的构造函数(子类型函数)
function SubType() {this.subprototype = false;}//继承
SubType.prototype = new SuperType();SubType.prototype.getSubValue = function() {//console.log(this.subprototype);return this.subprototype;
};var instance = new SubType();console.log(instance instanceof Object);//true;
console.log(instance instanceof SuperType);//true;
console.log(instance instanceof SubType);//true;
/*** 原型.isPrototypeOf(实例):判断实例是否是原型上的*/
console.log(Object.prototype.isPrototypeOf(instance));//true
console.log(SuperType.prototype.isPrototypeOf(instance));//true
console.log(SubType.prototype.isPrototypeOf(instance));//true

3.1.4.谨慎的定义方法

/*** 1.先继承父的属性和方法,再去写子类的属性和方法,可以重写父类的方法和属性。* 2.通过原型链继承,不能用字变量的方式创建原型。*/
function SuperType() {this.prototype = true;
};
SuperType.prototype.getSuperValue = function() {return this.prototype;
};
function SubType() {this.subprototype = false;
}
//继承
SubType.prototype = new SuperType();
//重写父的方法
SubType.prototype.getSuperValue = function() {return false;
}
//增加新方法
SubType.prototype.getSubValue = function() {return this.subprototype;
}
var instance = new SubType();
console.log(instance.getSuperValue());//false;先找子的同名函数/*** 1.先继承父的属性和方法,再去写子类的属性和方法,可以重写父类的方法和属性。* 2.通过原型链继承,不能用字变量的方式创建原型。*/function SuperType1() {this.prototype = true;
};
SuperType1.prototype.getSuperValue1 = function() {return this.prototype;
};
function SubType1() {this.subprototype = false;
}
//继承
SubType1.prototype = new SuperType1();
//用字变量的方式
SubType1.prototype = {getSubValue:function() {return this.subprototype;},
};
var instance = new SubType1();
console.log(instance.getSuperValue1());//is no has function 字变量会使继承无效,子中没有父的方法。

3.1.5.原型链的问题

/*** 1.所有方法和属性都全部共享,一改全改值*/
function SuperType() {this.color = ['blue','yellow','red'];
}
function SubType() {}
SubType.prototype = new SuperType();
var instance1 = new SubType();
var instance2 = new SubType();
console.log(instance1.color);//['blue','yellow','red','white']
console.log(instance2.color);//['blue','yellow','red','white']
instance1.color.push('white');
console.log(instance1.color);//['blue','yellow','red','white']
console.log(instance2.color);//['blue','yellow','red','white']

3.2.借用构造函数

/*** 1.在子类构造函数中调用父类的构造函数* 2.可以传递参数给父类* 3.子类新的方法写在调用父类的之后。*/
function SuperType() {this.colors = ['red','bule','yello'];
}
function SubType() {//继承SuperType.call(this);this.age = 29;
}
var instance1 = new SubType();
var instance2 = new SubType();
instance1.colors.push('white');
console.log(instance1.colors);//['blue','yellow','red','white']
console.log(instance1.age);//29
console.log(instance2.colors);//['blue','yellow','red']
console.log(instance2.age);//29function SuperType1(name) {this.colors = ['red','bule','yello'];this.name = name;
}
function SubType1() {//继承SuperType1.call(this,'lvxin');this.age = 29;
}
var instance11 = new SubType1();
var instance21 = new SubType1();console.log(instance11.name);//lvxin
console.log(instance21.name);//lxinconsole.log(instance11.age);//29
console.log(instance21.age);//29console.log(instance11.colors);//['red','bule','yello','white']
instance11.colors.push('white');
console.log(instance21.colors);//['red','bule','yello']

3.3.组合继承

/*** 1.原型链和借用构造来实现对实例属性的继承*/
function SuperType(name) {this.colors = ['red','blue','green'];this.name = name;
}
SuperType.prototype.sayName = function() {return this.name;
};
function SubType(name,age) {SuperType.call(this,name);this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {return this.age;
}var instance1 = new SubType('lvxin',18);
instance1.colors.push('white'); //['red','blue','green','white']
console.log(instance1.colors);
console.log(instance1.sayName());//lvxin
console.log(instance1.sayAge());//18
var instance2 = new SubType('xuyuqiu',19);
console.log(instance2.colors);//['red','blue','green']
console.log(instance2.sayName());//xuyuqiu
console.log(instance2.sayAge());//19

3.4.原型式继承

	/*** 1.值都共享* 2.Object.create(对象)方法也可以用来创建一个实例。*/console.log("-------------------6.3.4原型式继承-----------------------");function object(o) {function F() {}F.prototype = o;return new F();}var person = {name:'lvxin',friends:['duanyujie','xuyuqiu']};//var anotherPerson = new object(person);var anotherPerson = Object.create(person);anotherPerson.name = 'lizhiying';anotherPerson.friends.push('liufan');//var yetAnotherPerson = new object(person);var yetAnotherPerson = Object.create(person);yetAnotherPerson.name = 'xufengfeng';yetAnotherPerson.friends.push('zhengling');console.log(person.friends);//['duanyujie','xuyuqiu','liufan','zhenglin']console.log(person.name);//xufengfeng

3.5.寄生式继承

	function object(o) {function F() {}F.prototype = o;return new F();}function createAnother(original) {var clone = object(original);clone.sayHi = function() {return 'hi';}return clone;}var person = {name:'lvxin',friends:['duanyujie','xuyuqiu']};var anotherPerson = createAnother(person);console.log(anotherPerson.sayHi());//hi

3.6.寄生组合式继承

function object(o) {function F() {}F.prototype = o;return new F();
}function inheritPrototype(subType,superType) {var prototype = object(subType.prototype);prototype.constructor = subType;subType.prototype = prototype;
}function SuperType(name) {this.colors = ['red','blue','green'];this.name = name;
}SuperType.prototype.sayName = function() {return this.name;
};function SubType(name,age) {SuperType.call(this,name);this.age = age;
}inheritPrototype(SubType,SuperType);SubType.prototype.sayAge = function() {return this.age;
}

4.学习地址

【JS】创建对象的6种方式总结
GitHub代码


http://chatgpt.dhexx.cn/article/jEiqIvcU.shtml

相关文章

【java基础】-谈谈对面向对象理解

一 前言 本篇文章的核心知识如下,主要是帮助大家更好的理解面向对象编程; 二面向对象VS面向过程 2.1 面向过程编程 面向过程编程(Process Oriented Programming )其意指是面向过程编程,what?,过程是什…

对Java面向对象的理解

Java的面向对象的理解 1.思想简述: 面向对象是一种思想。它将数据和操作数据的方法封装在对象中,从而使对象有了一些功能,也就是说面向对象是将功能等通过对象来实现,将功能封装进对象之中,让对象去实现具体的细节&am…

如何理解Python中的面向对象

一、认识面向对象是什么 面向过程的程序设计的核心就是过程,就是流水线式的思维,过程就是解决问题的步骤,面向过程的设计就好像一条设计好的流水线,考虑周全什么就处理什么东西。 优点在于极大地降低了写程序的复杂度&#xff0c…

Java面向对象的理解

1. 面向对象 Java 是面向对象的编程语言,对象就是面向对象程序设计的核心。其基本思想是使用对象、类、继承、封装、多态等基本概念来进行程序设计。从现实世界中客观存在的事物(即对象)出发来构造软件系统,并且在系统构造中尽可…

如何理解Java中的面向对象

好几次面试都问到了这个问题,回答的也都不好,暂且总结一下: 我的理解是:面向对象是向现实世界模型的自然延伸,这是一种”万物皆对象”的编程思想。在现实生活中的任何物体都可以归为一类事物,而每一…

对于面向对象的简单理解

引言 一直都想写一篇关于面向对象的文章,但是由于之前自己也是一知半解所以也就一直没有付出行动。关于面向对象的这类文章网上已经有很多大牛写过了,在这里呢我也就不班门弄斧了。就简单的说一下我的理解,话不多说接下来进入主题。 什么是…

js面向对象理解

ECMAScript 有两种开发模式:1.函数式(过程化),2.面向对象(OOP)。面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象。但是,ECMAScript 没有类的概念,因此它的对象…

Python面向对象理解

一,初始面向对象. 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西。 优点是:极大的降低了写程序的复杂度…

面向对象理解——多态

何为多态 定义: 多态是指不同的子类在继承父类后分别都重写覆盖了父类的方法,即父类同一个方法,在继承的子类中表现出不同的形式。系统在运行时(而非编译时),能够根据其类型确定调用哪个重载的成员函数的能…

面向对象的理解及相关概念(封装,继承,多态)

前言 面向对象程序设计(OOP)是当今主流的程序设计范性,它取代了结构化或过程式编程技术。 一、面向对象是什么? 1、定义: 面向对象就是:把数据及对数据的操作方法放在一起,作为一个相互依存…

面向对象的理解

一、什么是面向对象? 对 Java 语言来说,一切皆是对象。把现实世界中的对象抽象地体现在编程世界中,一个对象代表了某个具体的操作。一个个对象最终组成了完整的程序设计,这些对象可以是独立存在的,也可以是从别的对象继…

面向对象理解

1.早期发展 面向对象(Object Oriented,OO)是软件开发方法。面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。面向对象是一种对现实世界理解和抽象的方法…

error: unknown file type '.pyx' (from 'pycocotools/_mask.pyx')

在 YOLO v3模型中运用 cocoapi 进行评估模型,安装时报错显示: building pycocotools._mask extension error: unknown file type .pyx (from pycocotools/_mask.pyx) make: *** [all] Error 1可能的解决方法:   运行的 Python 版本不对。 …

sklearn/tree/_criterion.pyx

注解: criterions分为几类,其中有classification criterions与regression criterions。classification criterions是针对离散的,regression criterions是针对连续分布的 本文主要讲解一下RegressionCriterion。 RegressionCriterion用于回归树(Regression Tree 回归树)中…

python中导入.pyx文件的问题解决

已经几次遇到这种问题了, 里面的region指的是一个.pyx文件。 ImportError: cannot import name region from toolkit.utils (D:\******\RPN\pysot-master\toolkit\utils\__init__.py) 解决方案: 创建一个setup.py文件,一般如果是搬运的话里面会有&…

将pyx文件编译成pyd文件(很多坑,已解决)

项目场景: Faster R-CNN项目,将pyx文件编译成pyd文件(很多坑,请注意) 项目环境 python 3.6 – conda的虚拟环境 TensorFlow 1.15.0 win 10 问题描述 使用网上的教程进行编译 有一个需要被编译的bbox.pyx文件,创建…

python文件中import pyx文件问题

1、首先,pyx文件需要安装Cython,这里先进入自己的虚拟环境,用conda安装Cython conda install Cython 2、需要用到pyx的地方是mattnet中的mask.py 文件,里面 import _mask.pyx 文件,需要先在mask的目录下建立setup文…

成功解决windows下将.pyx文件编译成.pyd文件

在linux上正常跑通的算法,搬到windows下使用就报错了 原来是其中涉及到.pyx文件的编译 在linux下.pyx会被编译成.so的文件,但是在windows下是无法使用.so文件的 需要重新编译成windows下的.pyd格式的才可以直接将python模块成功导入到算法中 所以需要解…

.pyx文件编译出错

文章目录 .pyx文件的编译导入 参考博客(写的超级好,完美解决了问题) 错误 ImportError: Building module dtw failed: ["distutils.errors.CompileError: command D:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\Pr…

python导入pyx文件失败

报错如下: 出现无法导入的情况时,在import之前加上一下两条语句即可解决问题: import pyximport pyximport.install()