面向对象:是把事物理解成一个个对象,然后由对象之间合作分工
面向对象的特性:
1.封装性 2. 继承性 3.多态性
类:是具有相同属性和行为的对象集合
对象:根据类的属性和行为创建的实例化
类:定义对象的属性和方法通过类可以实例化多个该类对象
创建类:ES5
function Student(name,gender,id){
//this:new出来的对象
this.name = name;
this.gender = gender;
this.id = id;
this.study = function(){
console.log("study");
}
//成员函数中使用其他的成员,必须加前缀this
this.showValue = function(){
console.log(this.name,this.gender,this.id);
this.study();
}
}
this作用:
1.与事件连用:代表触发事件的元素
2.与普通方法连用:代表调用该函数的对象
3.与构造方法连用:代表new出来的对象
创建一个对象:
let s = new Student("高少鑫",'M',9527);
使用
s.showValue();
创建类 ES6:
class Student{
//构造方法
//通过new关键字调用的构造方法执行时的代码块
//构造的数属性
constructor(name,gender,id) {
this.name = name;
this.gender = gender;
this.id = id;
}
study(){
console.log("study");
}
showValue(){
console.log(this.name,this.gender,this.id);
this.study();
}
}
创建一个对象:
let s = new Student("凢凢",'M',1);
类与类的关系:
1.组合:在定义一个类时,该类的成员属性时另一个类的对象
2.依赖:一个类的成员方法的参数是另一个类的对象
面向对象分析问题的思路
1.找出该问题有几个对象 分析问题阶段
2.抽象出其行为和属性构造类 编码阶段
3.各个对象各司其职