Dart中的面向对象

面向对象的三个基本特征

面向对象编程(OOP)的三个基本特征是:封装、继承、多态

封装:封装是对象和类概念的主要特性。封装,把客观事物封装成抽象的类,并且把自己的部分属性和方法提供给其他对象调用, 而一部分属性和方法则隐藏。

继承:面向对象编程(PPO)语言的一个主要功能就是“继承”。“继承”是指这样一种能力:它可以使用现有类的功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。

多态:允许将子类类型的指针赋值给父类类型的指针,同一个函数调用会有不同的执行结果。

Dart所有的东西都是对象,所有的对象都继承自Object类。

Dart是一门使用类和单继承的面向对象语言,所有的对象都是类的实例,并且所有的类都是Object的子类
一个类通常由属性和方法组成。

定义一个类

class Person {
	String name="张三";
	int age =23;
	void getInfo(){
		print("$name---$age");
		print("${this.name}");
	}
}

void main(){
	var p1 = new Person();
	print(p1.name);
	p1.getInfo();
}

默认构造函数,名称和类名一样

class Person {
	String name;
	int age;
	Person(String name,int age ){
		this.name=name;
		this.age=age;
	}
	void getInfo(){
		print("$name---$age");
		print("${this.name}");
	}
}

void main(){
	Person p1 = new Person('张三',20);
}

构造函数的简写,作用和上面的是一样的

class Person {
	String name;
	int age;
	Person(this.name,this.age);
	void getInfo(){
		print("$name---$age");
		print("${this.name}");
	}
}

命名的构造函数,命名构造函数可以有多个(其实就是类似java中的静态方法)

class Person {
	String name;
	int age;
	Person(this.name,this.age);
	Person.now(){
		print('我是命名构造函数');
	}
	void getInfo(){
		print("$name---$age");
		print("${this.name}");
	}
}

//默认实例化类的时候调用的是 默认构造函数
//使用命名构造函数实例化类,如下
void main(){
	Person p1 = new Person.now();
}

可以将上面的Person类抽离到单独的一个dart文件中,然后在使用到的地方引入类

import 'lib/Person.dart';

初始化操作

就是在new一个对象,在调用构造函数之前初始化;这种写法用的不多

class Rect{
	num height;
	num width;
	Rect():height=2,width=10{
		print("${this.height}---${this.width}")};
	getArea(){
		return this.height*this.width;
	}
}

void main(){
	Rect r = new Rect(10,4);
}

私有属性

Dart中没有public、private、protect等修饰符
但是可以在属性名称前面加下划线代表私有(私有的属性或者方法只能在当前类里面才可以访问)
注意:要单独抽离到一个dart文件中才有效

class Animal{
	String _name;
	int age;
	Animal(this._name,this.age);
	void printInfo(){
		print("${this._name)---${this.age}");
	}
}
import 'lib/Animal.dart';
void main(){
	Aniaml a = new Animal('小狗',3);
	print(a._name);
}

get方法

get方法没有小括号
调用get方法的时候,当成访问属性的形式

class Rect{
	num height;
	num width;
	Rect(this.height,this,width);
	get area{
		return this.height*this.width;
	}
}

void main(){
	Rect r = new Rect(10,4);
	print("面积:${r.area}");
}

set方法

访问的时候当成是属性赋值的形式

class Rect{
	num height;
	num width;
	Rect(this.height,this,width);
	get area{
		return this.height*this.width;
	}
	set areaHeight(value){
		this.height=value;
	}
}

void main(){
	Rect r = new Rect(10,4);
	//print("面积:${r.area}");
	r.areaHeight=2;
}

静态成员

使用static 关键字来实现类级别的变量和函数
静态方法不能访问非静态成员,非静态方法可以访问静态成员

class Person {
	static String name = "张三";
	static void show (){
		print(name);
	}
	void printInfo(){  //非静态方法可以访问静态成员以及非静态成员
		print(name);  //访问静态属性,不用加this
		print(this.age); //访问非静态属性
	}
}

main(){
	var p = new Person();
	print(Person.name);
	print(Person.show());
}

Dart中的对象操作符

?   条件运算符
as  类型转换
is  类型判断
..  级联操作(连缀)

as的使用例子

var p1;
p1='';
p1 = new Person('张三',20);
// p1.printInfo();
(p1 as Person).printInfo();

…级联操作的使用例子

Person p1 = new Person('张三',20);
p1.printInfo();

p1..name='李四'
  ..age=30
  ..printInfo();

Dart中的类的继承

1、子类使用extends关键词来继承父类
2、子类会继承父类里面可见的属性和方法 但是不会继承构造函数
3、子类能复写父类的方法 getter和setter

class Person {
	String name;
	num age;
	Person(this.name,this.age);
	void printInfo(){
		print("${this.name}---${this.age}");
	}
}

class Web extends Person{
	Web(String name, num age):super(name,age);
	//覆写父类的方法
	@override  //可以写也可以不写,建议在覆写父类方法的时候加上
	void printInfo(){
		print("姓名:${this.name}---年龄:${this.age}");
	}
}

main(){
	Web w = new Web('张三',12);
	w.printInfo();
}

猜你喜欢

转载自blog.csdn.net/weixin_44679078/article/details/106984456