私有成員
- 在Java中有
public
高蜂,private
,protected
這些訪問(wèn)修飾符罕容,可控制類(lèi)class成員的訪問(wèn)權(quán)限备恤,在Dart中沒(méi)有這些訪問(wèn)修飾符,屬性成員可通過(guò)下劃線_
锦秒,表明該屬性是類(lèi)class的私有成員露泊,外界不能直接訪問(wèn),只能通過(guò)方法訪問(wèn)旅择,注意這里涉及到兩個(gè)單獨(dú)文件
之間的訪問(wèn)惭笑; - person類(lèi)文件,代碼如下:
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 是私有成員 不能直接訪問(wèn)
print(person1.getWeight);
}
靜態(tài)成員
- 使用
static
關(guān)鍵字來(lái)定義靜態(tài)成員,包括靜態(tài)屬性
和靜態(tài)函數(shù)方法
柱蟀; - 靜態(tài)成員 通過(guò)
類(lèi)名
來(lái)調(diào)用川蒙;
void main(List<String> args) {
Car car = new Car("bsj", 12500.0);
//靜態(tài)屬性
Car.speed = 125.6;
//實(shí)例方法 調(diào)用者為實(shí)例對(duì)象
car.run();
//靜態(tài)方法 調(diào)用者為類(lèi)名
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");
}
}
類(lèi)的繼承
- Dart中繼承使用
extends
關(guān)鍵字,子類(lèi)中使用super
訪問(wèn)父類(lèi)长已,不支持多繼承畜眨; - 父類(lèi)中的所有成員變量和方法都會(huì)被繼承昼牛,但是
構(gòu)造方法
除外; - 重寫(xiě)方法最好加上 @override 注解康聂;
- 子類(lèi)的構(gòu)造方法在執(zhí)行前贰健,會(huì)
隱式調(diào)用父類(lèi)
的無(wú)參數(shù)默認(rèn)構(gòu)造函數(shù)
(沒(méi)有參數(shù)且與類(lèi)同名的構(gòu)造方法); - 如果父類(lèi)沒(méi)有
無(wú)參數(shù)默認(rèn)構(gòu)造函數(shù)
恬汁,則子類(lèi)的構(gòu)造函數(shù)必須在初始化列表中
通過(guò)super
顯式調(diào)用父類(lèi)的某個(gè)構(gòu)造函數(shù)伶椿,完成父類(lèi)中屬性的初始化;
main(List<String> args) {}
class Animal {
int age;
Animal(this.age);
}
class Person extends Animal {
String name;
//必須完成父類(lèi)屬性age的初始化 在初始化列表中完成
Person(this.name, int age) : super(age);
}
抽象類(lèi)
- 用
abstract
關(guān)鍵字修飾的類(lèi)蕊连,稱(chēng)之為抽象類(lèi)
悬垃; - 在Dart中沒(méi)有具體實(shí)現(xiàn)的方法游昼,稱(chēng)之為
抽象方法
甘苍; - 抽象方法必須存在于抽象類(lèi)中,抽象方法沒(méi)有
abstract
關(guān)鍵字進(jìn)行修飾烘豌,但是Java中可以载庭,Java中的抽象類(lèi)與抽象方法均可使用abstract
關(guān)鍵字進(jì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必須實(shí)現(xiàn)draw 抽象方法 否則會(huì)報(bào)錯(cuò)
class Circle extends Shap {
@override
void draw() {
print("繪制圓形");
}
}
- 抽象類(lèi)
不能實(shí)例化
囚聚; - 繼承抽象類(lèi)的子類(lèi)
必須實(shí)現(xiàn)抽象類(lèi)中定義的抽象方法
,否則會(huì)報(bào)錯(cuò)标锄;
main(List<String> args) {
//抽象類(lèi)不能實(shí)例化
final s = Shape();
//Map是系統(tǒng)的一個(gè)抽象類(lèi)
//Map能實(shí)例化 是因?yàn)镸ap內(nèi)部實(shí)現(xiàn)了一個(gè)工廠構(gòu)造函數(shù) external factory Map()
final map = Map();
print(map.runtimeType);
}
//Shape是一個(gè)抽象類(lèi)
abstract class Shape {
//getArea是抽象方法 沒(méi)有實(shí)現(xiàn)體的 由子類(lèi)去實(shí)現(xiàn)
void getArea();
}
//繼承抽象類(lèi)的子類(lèi) 必須實(shí)現(xiàn)抽象類(lèi)中定義的抽象方法
class Rectanle extends Shape {
@override
void getArea() {
print("畫(huà)矩形");
}
}
- Map是系統(tǒng)的一個(gè)抽象類(lèi)顽铸,其能實(shí)例化 是因?yàn)镸ap內(nèi)部實(shí)現(xiàn)了一個(gè)工廠構(gòu)造函數(shù)
external factory Map()
;
隱式接口
- 在Dart中接口比較特殊料皇,沒(méi)有一個(gè)專(zhuān)門(mén)的關(guān)鍵字來(lái)聲明接口谓松;
- 默認(rèn)情況下,
定義的每個(gè)類(lèi)都相當(dāng)于默認(rèn)也聲明了一個(gè)接口
践剂,可稱(chēng)之為隱式接口
鬼譬,可以由其他類(lèi)來(lái)實(shí)現(xiàn),因?yàn)镈art不支持多繼承逊脯; - 類(lèi)可通過(guò)
implements
關(guān)鍵字优质,將其他類(lèi)當(dāng)成接口
,來(lái)實(shí)現(xiàn)其他類(lèi)中定義的方法军洼; - 在開(kāi)發(fā)中巩螃,我們通常將用于給別人實(shí)現(xiàn)的類(lèi) 聲明為抽象類(lèi);
main(List<String> args) {
}
class Animal {
void eat() {
print("eat");
}
}
class Runnner {
void run() {
print("run");
}
}
class Flyer {
void fly() {
print("fly");
}
}
//當(dāng)將一個(gè)類(lèi)當(dāng)作接口使用時(shí)匕争,那么實(shí)現(xiàn)這個(gè)接口的類(lèi)避乏,必須實(shí)現(xiàn)這個(gè)接口中的所有方法
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
類(lèi)繼承自Animal
,并實(shí)現(xiàn)Runnner
和Flyer
這兩個(gè)接口汗捡,這里Runnner
和Flyer
本質(zhì)是類(lèi)class淑际,可當(dāng)作接口進(jìn)行使用畏纲; - 當(dāng)將一個(gè)
類(lèi)當(dāng)作接口使用時(shí)
,那么實(shí)現(xiàn)這個(gè)接口的類(lèi)春缕,必須實(shí)現(xiàn)這個(gè)接口中的所有方法
盗胀;
混入mixin
- 當(dāng)
當(dāng)前類(lèi)
實(shí)現(xiàn)(implements) 隱式接口類(lèi),默認(rèn)必須實(shí)現(xiàn) 隱士接口類(lèi)中 的所有方法锄贼; - 現(xiàn)在需要 實(shí)現(xiàn)
當(dāng)前類(lèi)
不想再 實(shí)現(xiàn)隱式接口類(lèi)中的 所有方法票灰,可使用混入mixin
語(yǔ)法; - 定義可混入的類(lèi)時(shí)宅荤,不能用
class
關(guān)鍵字屑迂,而是使用mixin
關(guān)鍵字; - 當(dāng)前類(lèi)使用
with
進(jìn)行混入冯键,使用混入時(shí)可以使用super
關(guān)鍵字惹盼; - 當(dāng)前類(lèi)實(shí)現(xiàn)接口類(lèi) -- 案例代碼:
void main(List<String> args) {
}
abstract class Runner {
void run();
}
abstract class Flyer {
void fly();
}
class Animal {
void eat() {
print("Animal eat");
}
}
//SuperMan實(shí)現(xiàn)接口類(lèi)Runner與Flyer
//必須實(shí)現(xiàn)Runner與Flyer中所有方法,否則會(huì)報(bào)錯(cuò)
class SuperMan extends Animal implements Runner, Flyer {
@override
void run() {
// TODO: implement run
}
@override
void fly() {
// TODO: implement fly
}
}
SuperMan實(shí)現(xiàn)接口類(lèi)Runner與Flyer惫确;
必須實(shí)現(xiàn)Runner與Flyer中所有方法手报,否則會(huì)報(bào)錯(cuò);
當(dāng)前類(lèi)with 混入類(lèi) -- 案例代碼:
void main(List<String> args) {}
mixin Runner {
void run() {}
}
mixin Flyer {
void fly() {}
}
class Animal {
void eat() {
print("Animal eat");
}
}
//SuperMan with 混入類(lèi)Runner與Flyer
//可有選擇的實(shí)現(xiàn)Runner與Flyer中的方法
class SuperMan extends Animal with Runner, Flyer {
@override
void run() {
// TODO: implement run
super.run();
}
}
- SuperMan with 混入類(lèi)Runner與Flyer改化;
- 可有選擇的實(shí)現(xiàn)Runner與Flyer中的方法掩蛤;
- 使用混入時(shí),Superman實(shí)現(xiàn)時(shí)可以使用
super
關(guān)鍵字陈肛,調(diào)用Runner中的實(shí)現(xiàn)揍鸟;