面向对象的三大特:
1.封装
2.继承
3.多态
封装:
1.隐藏对象的属性对外只提供可以访问属性的方法
export class pet{
//访问修饰符 本类,本包,其他包,子类
//public
public id:number 共有的 任何位置都可以访问
//default默认
name:string
private sex: string
b() {
console.log('默认');
}
私有的 只允许本类访问
private a() {
console.log('私有');
}
constructor(id: number, name: string, sex: string) {
this.id = id
this.name = name
this.sex = sex
}
}
export class Pet {
//访问修饰符 本类,本包,其他包,子类
//public 共有的 任何位置都可以访问
public id: number
//default默认
name: string
private sex: string
b() {
console.log('默认');
}
//私有的 只允许本类访问
private a() {
console.log('私有');
}
constructor(id: number, name: string, sex: string) {
this.id = id
this.name = name
this.sex = sex
}
}
import { Pet } from "../bb/entends";
let P:Pet=new Pet(1,'1','1');
console.log(P.name);
P.b()
//p.a
class Pets {
constructor(id: number, name: string, sex: string, helth: number, love: number) {
this.id = id
this.name = name
this.sex = sex
this.helth = helth
this.love = love
}
1.私有化属性
private id: number
private name: string
private sex: string
private helth: number//健康值
private love: number//亲密度
2.提供一个公有的方法来访问属性 getter/setter
get访问
public getId(): number {
return this.id;
}
public getName(): string {
return this.name;
}
public getSex(): string {
return this.sex;
}
public getHelth(): number {
return this.helth;
}
public getLove(): number {
return this.love;
}
set 设置/赋值
public setId(id: number) {
this.id = id
}
public setName(name: string) {
this.name = name
}
public setSex(sex: string) {
this.sex = sex
}
public setHelth(helth: number) {
this.helth = helth
}
public setLove(love: number) {
this.love = love
}
}
let P: Pets = new Pets(1, '老黑', '公', 100, 100);
//p.helth=99
P.setHelth(90)//调用函数修改属性值
必须使用get函数访问属性
console.log(`序号:${P.getId()},昵称:${P.getName()}`); 全部:
class Pets {
constructor(id: number, name: string, sex: string, helth: number, love: number) {
this.id = id
this.name = name
this.sex = sex
this.helth = helth
this.love = love
}
//1.私有化属性
private id: number
private name: string
private sex: string
private helth: number//健康值
private love: number//亲密度
//2.提供一个公有的方法来访问属性 getter/setter
//get访问
public getId(): number {
return this.id;
}
public getName(): string {
return this.name;
}
public getSex(): string {
return this.sex;
}
public getHelth(): number {
return this.helth;
}
public getLove(): number {
return this.love;
}
//set 设置/赋值
public setId(id: number) {
this.id = id
}
public setName(name: string) {
this.name = name
}
public setSex(sex: string) {
this.sex = sex
}
public setHelth(helth: number) {
this.helth = helth
}
public setLove(love: number) {
this.love = love
}
}
let P: Pets = new Pets(1, '老黑', '公', 100, 100);
//p.helth=99
P.setHelth(90)//调用函数修改属性值
//必须使用get函数访问属性
console.log(`序号:${P.getId()},昵称:${P.getName()}`);
继承 子类继承父类的属性和方法
写法:
class A {
public a1: number
a2: number
private a3: number
constructor(a1: number, a2: number, a3: number) {
this.a1 = a1
this.a2 = a2
this.a3 = a3
}
public getA3(): number { return this.a3 }
public setA3(a3: number) { this.a3 = a3 }
private aa() {
console.log('私有方法');
}
public bb() {
console.log('父类的公有方法');
}
}
子类:
class B extends A {
子类可以有自己的属性和方法
b1: number
b2: number
b3() {
console.log("b的函数");
console.log(this.a1);
console.log(this.a2);
子类无法继承父类的私有属性和方法
//console.log(this.a3);
//this.aa()
this.bb()
}
派生类的构造函数必须包含"super"调用
constructor(a1: number, a2: number, a3: number, b1: number, b2: number) {
//调用父类的构造函数 必须写在构造函数的第一行
super(a1, a2, a3)
this.b1 = b1
this.b2 = b2
}
}
let ba: B = new B(1, 2, 3, 4, 5);
ba.bb()
class B extends A {
//子类可以有自己的属性和方法
b1: number
b2: number
b3() {
console.log("b的函数");
console.log(this.a1);
console.log(this.a2);
//子类无法继承父类的私有属性和方法
//console.log(this.a3);
//this.aa()
this.bb()
}
//派生类的构造函数必须包含"super"调用
constructor(a1: number, a2: number, a3: number, b1: number, b2: number) {
//调用父类的构造函数 必须写在构造函数的第一行
super(a1, a2, a3)
this.b1 = b1
this.b2 = b2
}
}
let ba: B = new B(1, 2, 3, 4, 5);
ba.bb()
1.继承的关键字 extends
2.子类继承法父类 只能继承一个类(单继承)
3.一个父类 可以有多个 子类
4.object 类是所有类的父亲,没有显示继承的类都默认继承object
5.子类必须调用父类的构造函数,构造函数必须在第一行
6.子类不能继承父类的私有属性和方法
7.在子类中调用父类的属性和方法需要使用this关键字
8.子类属性和父类属性同名,默认使用子类的属性
9.方法可以同名,默认调用子类方法,可以使用super调用父类方法
例子:人{姓名,性别,吃喝拉撒}子类 好人{肤色,做好事}坏人{地区,做坏事}
写法:
class Person {
constructor(name: string, sex: string) {
this.name = name
this.sex = sex
}
name: string
sex: string
eat() {
console.log('人的吃喝拉撒');
}
}
class GoodPerson extends Person {
color: string
sex: string
constructor(name: string, sex: string,color:string){
super(name,sex)
this.color=color
this.sex=sex
}
hs(){
console.log(this.sex+'好人在做好事');
}
eat() {
//调用父类的方法需要使用super关键词
super.eat()
console.log('好人的吃喝拉撒');
}
}
let hr1:GoodPerson=new GoodPerson('张三','男','黄');
hr1.hs()
hr1.eat()