私有成員
- 在Java中有
public,private,protected這些訪問修飾符,可控制類class成員的訪問權(quán)限,在Dart中沒有這些訪問修飾符,屬性成員可通過下劃線_,表明該屬性是類class的私有成員,外界不能直接訪問,只能通過方法訪問,注意這里涉及到兩個單獨文件之間的訪問; - person類文件,代碼如下:
class Person {
String name;
int age;
//私有成員
double _weight;
//其中age為可選參數(shù)
Person.init(this.name, this.age, this._weight);
//使用set,get關(guān)鍵字的setter,getter方法
set setName(String name) {
this.name = name;
}
String get getName {
return this.name;
}
double get getWeight {
return this._weight;
}
}
- main函數(shù)文件,代碼如下:
import 'person.dart';
void main(List<String> args) {
Person person1 = Person.init("cccc", 26, 125.5);
//_weight 是私有成員 不能直接訪問
print(person1.getWeight);
}
靜態(tài)成員
- 使用
static關(guān)鍵字來定義靜態(tài)成員,包括靜態(tài)屬性和靜態(tài)函數(shù)方法; - 靜態(tài)成員 通過
類名來調(diào)用;
void main(List<String> args) {
Car car = new Car("bsj", 12500.0);
//靜態(tài)屬性
Car.speed = 125.6;
//實例方法 調(diào)用者為實例對象
car.run();
//靜態(tài)方法 調(diào)用者為類名
Car.walk();
}
class Car {
String name;
static double speed;
double weight;
Car(this.name, this.weight);
void run() {
print("run");
}
static void walk() {
print("walk");
}
}
類的繼承
- Dart中繼承使用
extends關(guān)鍵字,子類中使用super訪問父類,不支持多繼承; - 父類中的所有成員變量和方法都會被繼承,但是
構(gòu)造方法除外; - 重寫方法最好加上 @override 注解;
- 子類的構(gòu)造方法在執(zhí)行前,會
隱式調(diào)用父類的無參數(shù)默認構(gòu)造函數(shù)(沒有參數(shù)且與類同名的構(gòu)造方法); - 如果父類沒有
無參數(shù)默認構(gòu)造函數(shù),則子類的構(gòu)造函數(shù)必須在初始化列表中通過super顯式調(diào)用父類的某個構(gòu)造函數(shù),完成父類中屬性的初始化;
main(List<String> args) {}
class Animal {
int age;
Animal(this.age);
}
class Person extends Animal {
String name;
//必須完成父類屬性age的初始化 在初始化列表中完成
Person(this.name, int age) : super(age);
}
抽象類
- 用
abstract關(guān)鍵字修飾的類,稱之為抽象類; - 在Dart中沒有具體實現(xiàn)的方法,稱之為
抽象方法; - 抽象方法必須存在于抽象類中,抽象方法沒有
abstract關(guān)鍵字進行修飾,但是Java中可以,Java中的抽象類與抽象方法均可使用abstract關(guān)鍵字進行修飾,這是兩者之間的區(qū)別;
void main(List<String> args) {
Circle circle = new Circle();
circle.draw();
circle.show();
}
abstract class Shap {
void draw();
void show() {
print("show");
}
}
//Circle必須實現(xiàn)draw 抽象方法 否則會報錯
class Circle extends Shap {
@override
void draw() {
print("繪制圓形");
}
}
- 抽象類
不能實例化; - 繼承抽象類的子類
必須實現(xiàn)抽象類中定義的抽象方法,否則會報錯;
main(List<String> args) {
//抽象類不能實例化
final s = Shape();
//Map是系統(tǒng)的一個抽象類
//Map能實例化 是因為Map內(nèi)部實現(xiàn)了一個工廠構(gòu)造函數(shù) external factory Map()
final map = Map();
print(map.runtimeType);
}
//Shape是一個抽象類
abstract class Shape {
//getArea是抽象方法 沒有實現(xiàn)體的 由子類去實現(xiàn)
void getArea();
}
//繼承抽象類的子類 必須實現(xiàn)抽象類中定義的抽象方法
class Rectanle extends Shape {
@override
void getArea() {
print("畫矩形");
}
}
- Map是系統(tǒng)的一個抽象類,其能實例化 是因為Map內(nèi)部實現(xiàn)了一個工廠構(gòu)造函數(shù)
external factory Map();
隱式接口
- 在Dart中接口比較特殊,沒有一個專門的關(guān)鍵字來聲明接口;
- 默認情況下,
定義的每個類都相當于默認也聲明了一個接口,可稱之為隱式接口,可以由其他類來實現(xiàn),因為Dart不支持多繼承; - 類可通過
implements關(guān)鍵字,將其他類當成接口,來實現(xiàn)其他類中定義的方法; - 在開發(fā)中,我們通常將用于給別人實現(xiàn)的類 聲明為抽象類;
main(List<String> args) {
}
class Animal {
void eat() {
print("eat");
}
}
class Runnner {
void run() {
print("run");
}
}
class Flyer {
void fly() {
print("fly");
}
}
//當將一個類當作接口使用時,那么實現(xiàn)這個接口的類,必須實現(xiàn)這個接口中的所有方法
class Superman extends Animal implements Runnner, Flyer {
@override
void eat() {
// TODO: implement eat
super.eat();
}
@override
void run() {
// TODO: implement run
}
@override
void fly() {
// TODO: implement fly
}
}
-
Superman類繼承自Animal,并實現(xiàn)Runnner和Flyer這兩個接口,這里Runnner和Flyer本質(zhì)是類class,可當作接口進行使用; - 當將一個
類當作接口使用時,那么實現(xiàn)這個接口的類,必須實現(xiàn)這個接口中的所有方法;
混入mixin
- 當
當前類實現(xiàn)(implements) 隱式接口類,默認必須實現(xiàn) 隱士接口類中 的所有方法; - 現(xiàn)在需要 實現(xiàn)
當前類不想再 實現(xiàn)隱式接口類中的 所有方法,可使用混入mixin語法; - 定義可混入的類時,不能用
class關(guān)鍵字,而是使用mixin關(guān)鍵字; - 當前類使用
with進行混入,使用混入時可以使用super關(guān)鍵字; - 當前類實現(xiàn)接口類 -- 案例代碼:
void main(List<String> args) {
}
abstract class Runner {
void run();
}
abstract class Flyer {
void fly();
}
class Animal {
void eat() {
print("Animal eat");
}
}
//SuperMan實現(xiàn)接口類Runner與Flyer
//必須實現(xiàn)Runner與Flyer中所有方法,否則會報錯
class SuperMan extends Animal implements Runner, Flyer {
@override
void run() {
// TODO: implement run
}
@override
void fly() {
// TODO: implement fly
}
}
SuperMan實現(xiàn)接口類Runner與Flyer;
必須實現(xiàn)Runner與Flyer中所有方法,否則會報錯;
當前類with 混入類 -- 案例代碼:
void main(List<String> args) {}
mixin Runner {
void run() {}
}
mixin Flyer {
void fly() {}
}
class Animal {
void eat() {
print("Animal eat");
}
}
//SuperMan with 混入類Runner與Flyer
//可有選擇的實現(xiàn)Runner與Flyer中的方法
class SuperMan extends Animal with Runner, Flyer {
@override
void run() {
// TODO: implement run
super.run();
}
}
- SuperMan with 混入類Runner與Flyer;
- 可有選擇的實現(xiàn)Runner與Flyer中的方法;
- 使用混入時,Superman實現(xiàn)時可以使用
super關(guān)鍵字,調(diào)用Runner中的實現(xiàn);