ArkTS的类,接口

一、类

1. 基本的定义和实例化

相关概念:

  • 字段:可以理解为在类里面定义的成员变量,分为静态字段和实例字段

  • 方法:在类里面定义的函数,但是不要把构造函数也归纳到这个里面

  • 构造函数:construct( ) 是一个用来对类里面的成员实例(字段)进行快捷赋值的函数方法,在实例化对象时自动触发,

示例:

class Animal{
	//定义字段,即成员变量
	name: string
	color: string
	age: number
	static version: string = '1.0'
	
	//构造函数,不允许其别的名字,这个是固定的
	constructor(name: string, color: string, age: number){
		this.name = name
		this.color = color
		this.age = age
	}
	
	//定义方法,即成员方法
	getColor(){
		return this.color;
	}
}

let cat: Animal = new Animal('猫', '白色', 1)
console.log(cat.name,cat.color,cat.age)	   //输出:猫 白色 1
console.log('猫的颜色是', cat.getColor())	//输出:猫的颜色是 白色

2. 类的继承

类与类之间的继承靠的是 extends 来继承

子类调用父类的构造函数是靠super来使用

子类要是想重写父类的方法,只需要方法名相同就可以

class Animal {
  name: string;
  age: number;
  static version: string = '1.0';

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

class Cat extends Animal {
  color: string;
  voice: string;

  constructor(name: string, color: string, age: number, voice: string) {
    super(name, age);		// 只用于构造函数当中
    this.color = color;
    this.voice = voice;
  }
}

let cat: Cat = new Cat('猫', '黑色', 5, '喵');
console.log(cat.name, cat.color, cat.age, cat.voice, Animal.version);	// 输出:猫 黑色 5 喵 1.0

3. 类的修饰符

用来限制对类的成员的访问权限

与其他语言不同的是,ArkTS提供了四个限制

readonly(只读不可改),private(私有的),protected(受保护的),public(公有的)

示例:

class Person {
  readonly version: string = '1.0';
  private country: string;
  name: string;
  age: number;

  constructor(name: string, age: number, country: string) {
    this.name = name;
    this.age = age;
    this.country = country;
  }

  getCountry() {
    return this.country
  }
}

let person: Person = new Person('小明', 18, '中国人')
// person.version = '1.1'  // 报错,只读变量不能修改
console.log(person.name, person.age, person.version)
// console.log(person.country)  // 报错,私有变量外部不可以通过 . 来访问
console.log('他的国家是', person.getCountry())

4. 静态属性和方法

使用static关键字定义

class MathUtil {
    static PI: number = 3.14;

    static areaOfCircle(radius: number): number {
        return MathUtil.PI * radius * radius;
    }
}

console.log(MathUtil.PI); // 输出: 3.14
console.log(MathUtil.areaOfCircle(5)); // 输出: 78.5

I.静态变量

局部静态变量:作用于函数内部,但生命周期为整个程序执行期间。

全局静态变量:作用于定义它的文件,其他文件无法访问。

他们的值,在没有使用const定义时,可以被修改

II.静态函数

只能在定义它的文件中使用,其他文件无法调用,帮助封装实现细节。

二、接口

使用interface来定义,与类有点相似,只是接口更多的是更抽象的东西,只是定义了成员的类型相关的

1. 简单定义

//定义接口
interface Person{
	name: string;
	age: number;
	weight: number;
}

//定义对象
let xh: Person = {
	name: 'xiaohong',
	age: 18,
	weight: 90
}

从上面例子可以看出,接口的定义其实就类似于创建了一个数据类型,用它来规范一个对象的内容。

注:

接口里面用的是 ; 对象里面用的是 ,

2. 接口的继承和实例化

I. 接口的继承采用的是extend

II. 接口的实现,也就是靠类来实现接口用的是implements

示例:

interface Style {
  color: string
}

// 继承接口包含被继承接口的所有属性和方法
// 还可以添加自己的属性和方法。
interface Area extends Style {
  width: number
  height: number

  // 方法的声明
  someMethod(): void;
  // someMethod: () => void; //也可以这样定义
}

class Rectangle implements Area {
  // 可以新增属性,但是接口的属性必须实现
  // 1.接口属性通过直接声明实现
  width: number;
  height: number;
  // 2.接口属性通过getter、setter方法实现,等价于上面方法1
  private _color: string = ''

  constructor(width:number, height: number) {
    this.width = width
    this.height = height
  }
  get color(): string {
    return this._color
  }

  set color(x: string) {
    this._color = x
  }

  // 接口方法实现
  someMethod(): void {
    console.log('someMethod called')
  }
}

const rectangle = new Rectangle(20,)
rectangle.someMethod()
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐