类的简介
// 使用class关键字定义一个类
/**
* 对象中包含两个部分:
* 属性
* 方法
*
*/
class Person{
/**
* 直接定义的属性是实例属性,需要通过对象的实例去验访问:
* const per = new Person()
* per.name
* 使用static开头的属性是静态属性(类属性),可以直接通过类去使用
* person.age
*
* readonly开头的属性表示一个只读的属性
*/
// readonly name: string = "随悟空"
// 在属性前使用static关键字可以定义类属性(静态属性)
// static readonly age: number = 18
// 定义实例属性
name = '随悟空'
age = 22
/**
* 定义方法
*
* 如果方法以satatic开头则方法就是类方法,可以直接通过类去调用
*
* */
sayHello() {
console.log("hello,你好")
}
}
const per = new Person()
console.log(per)
构造函数
class Dog{
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
bark() {
console.log(this.name)
}
}
const dog = new Dog('小黑', 44);
const dog2 = new Dog('小白', 14);
console.log(dog.bark);
console.log(dog2.bark);
继承
(function() {
class Animal{
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
sayHello() {
console.log("动物在叫~");
}
}
class Dog extends Animal {
sayHello() {
console.log("汪汪汪");
};
run() {
console.log("狗崽跑")
}
}
class Cat extends Animal {
sayHello() {
console.log("喵喵喵");
};
}
const dog = new Dog('旺财',25);
console.log(dog);
dog.sayHello()
const cat = new Cat('咪咪', 12)
console.log(cat);
cat.sayHello()
dog.run()
})()
super
(function() {
class Animal {
name: string;
constructor(name: string){
this.name = name;
}
sayhello() {
console.log("动物在叫")
}
}
class Dog extends Animal {
age: number;
constructor(name: string, age: number) {
super(name);
this.age = age;
}
sayhello() {
super.sayhello()
}
}
let dog = new Dog("旺财", 22)
dog.sayhello()
console.log(dog)
})()
抽象类
(function() {
abstract class Animal {
name: string;
constructor(name: string){
this.name = name;
}
abstract sayhello(): void
}
class Dog extends Animal {
sayhello() {
console.log("汪汪汪");
}
}
class Cat extends Animal {
sayhello() {
console.log("喵喵喵");
}
}
let dog = new Dog("旺财")
dog.sayhello()
console.log(dog)
})()
接口
(function() {
type myTytpe = {
name: string,
age: number
};
interface myInterface{
name: string;
age: number;
}
interface myInterface {
gender: string
}
interface myInter{
name: string;
sayhello():void;
}
class myClass implements myInter{
name: string;
constructor(name:string){
this.name = name;
}
sayhello() {
console.log('大家好~~');
}
}
})()
属性的封装
(function(){
class Person{
private _name: string;
private _age: number;
constructor(name: string, age: number) {
this._name = name;
this._age = age;
}
get name() {
console.log('get name() 执行了');
return this._name;
}
set name(value: string) {
this._name = value
}
get age() {
return this._age;
}
set age(value: number) {
if(value >= 0) {
this._age = value;
}
}
}
const per = new Person('金蝉子', 100);
per.name = "猪八戒";
per.age = 118;
console.log(per.name);
class A{
protected num: number;
constructor(num: number) {
this.num = num;
}
}
class B extends A{
test() {
console.log(this.num);
}
}
const b = new B(123);
class C{
constructor(public name:string, public age: number) {
}
}
const c = new C('sss',55)
})()
泛型
function fn<T>(a:T):T{
return a;
}
fn(10);
fn<string>("sss");
function fn2<T,K>(a:T, b:K): T {
console.log(b)
return a;
};
fn2<number, string>(123, 'hello');
interface Inter{
length: number;
}
function fn3<T extends Inter>(a: T):number{
return a.length;
}
fn3({length: 10})
class MyClass<T>{
name: T;
constructor(name:T) {
this.name = name;
}
}
const mc = new MyClass('悟能');