第三十一章.面向对象编程-补充
function student(name,id){
let obj = {};
obj.name = name;
obj.id = id;
obj.company = "外经贸";
obj.say = function(){
console.log(`我是${this.name},学号:${this.id}`);
};
return obj;
}
let pika = student("皮卡","224");
let ayuan = student("阿远","221");
let weige = student("伟哥","209");
console.log(pika);
console.log(ayuan);
console.log(weige);
pika.say();
ayuan.say();
weige.say();
function goudan(){
this.x = 10;
this.y = 20;
}
console.log(new goudan());
console.log(new goudan());
console.log(new goudan());
console.log(new goudan());
console.log(new goudan());
function Student(name,id){
this.name = name;
this.id = id;
this.university = "外交贸";
this.say = function(){
console.log(`我是${this.name},学号:${this.id}`);
};
}
let pika = new Student("皮卡","224");
let ayuan = new Student("阿远","221");
let weige = new Student("伟哥","209");
console.log(pika);
console.log(ayuan);
console.log(weige);
pika.say();
ayuan.say();
weige.say();
console.log(pika === ayuan);
console.log(pika.say === ayuan.say);
function Fn(){
this.x = 10;
}
Fn.prototype .x = 20;
let a = new Fn();
console.log(a.__proto__ === Fn.prototype);
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype.say = function(){
console.log(this.name+":"+this.age);
};
function Teacher(name,age,id){
Person.call(this,name,age);
this.id = id;
}
Teacher.prototype = new Person();
Teacher.prototype.university = "外经贸";
let a = new Person("小A",18);
console.log(a);
let b = new Teacher("简老师",20,"201801624");
console.log(b);
a.say();
b.say();
function F(){}
F.prototype = Person.prototype;
Teacher.prototype = new F();
Teacher.prototype.constructor = Teacher;
Teacher.prototype.univerity = "外经贸";
let a = new Person("小A",18);
console.log(a);
let b = new Teacher("简老师",20,"201801624");
console.log(b);
a.say();
b.say();
class Person{
constructor(n,a) {
this.name = n;
this.age = a;
}
say(){
console.log(this.name,this.age);
}
}
class Teacher extends Person{
constructor(n,a,id) {
super(n,a);
this.id = id;
}
company(){
return "潭州教育";
}
}
let a = new Person("小A",18);
console.log(a);
let b = new Teacher("简老师",20,"201801624");
console.log(b);
a.say();
b.say();