dart語言?面向對象編程(OOP)的三個基本特征是:封裝、繼承、多態
封裝:封裝是對象和類概念的主要特性。封裝,把客觀事物封裝成抽象的類,并且把自己的部分屬性和方法提供給其他對象調用, 而一部分屬性和方法則隱藏。
繼承:面向對象編程 (OOP) 語言的一個主要功能就是“繼承”。繼承是指這樣一種能力:它可以使用現有類的功能,并在無需重新編寫原來的類的情況下對這些功能進行擴展。
python類和對象、多態:允許將子類類型的指針賦值給父類類型的指針, 同一個函數調用會有不同的執行效果 。
Dart所有的東西都是對象,所有的對象都繼承自Object類。
Dart是一門使用類和單繼承的面向對象語言,所有的對象都是類的實例,并且所有的類都是Object的子類
object類的方法,一個類通常由屬性和方法組成。
void main(){ List list=new List();list.isEmpty;list.add('香蕉');list.add('香蕉1');Map m=new Map();m["username"]="張三";m.addAll({"age":20});m.isEmpty;Object a=123;Object v=true;print(a);print(v);
}
Dart是一門使用類和單繼承的面向對象語言,所有的對象都是類的實例,并且所有的類都是Object的子類
class Person{String name="張三";int age=23;void getInfo(){// print("$name----$age");print("${this.name}----${this.age}");}void setInfo(int age){this.age=age;}
}
void main(){//實例化var p1=new Person();print(p1.name);p1.getInfo();Person p1=new Person();print(p1.name);p1.setInfo(28);p1.getInfo();}
class Person{String name='張三';int age=20; //默認構造函數Person(){print('這是構造函數里面的內容 這個方法在實例化的時候觸發');}void printInfo(){ print("${this.name}----${this.age}");}
}//張三 李四 王五class Person{String name;int age; //默認構造函數Person(String name,int age){this.name=name;this.age=age;}void printInfo(){ print("${this.name}----${this.age}");}
}class Person{String name;int age; //默認構造函數的簡寫Person(this.name,this.age);void printInfo(){ print("${this.name}----${this.age}");}
}void main(){Person p1=new Person('張三',20);p1.printInfo();Person p2=new Person('李四',25);p2.printInfo();}
dart里面構造函數可以寫多個
class Person{String name;int age; //默認構造函數的簡寫Person(this.name,this.age);Person.now(){print('我是命名構造函數');}Person.setInfo(String name,int age){this.name=name;this.age=age;}void printInfo(){ print("${this.name}----${this.age}");}
}void main(){var d=new DateTime.now(); //實例化DateTime調用它的命名構造函數print(d);Person p1=new Person('張三', 20); //默認實例化類的時候調用的是 默認構造函數Person p1=new Person.now(); //命名構造函數Person p1=new Person.setInfo('李四',30);p1.printInfo();
}
Person.dart
class Person{String name;int age; //默認構造函數的簡寫Person(this.name,this.age);Person.now(){print('我是命名構造函數');}Person.setInfo(String name,int age){this.name=name;this.age=age;}void printInfo(){ print("${this.name}----${this.age}");}
}
main.dart
import 'lib/Person.dart';void main(){Person p1=new Person.setInfo('李四1',30);p1.printInfo();
}
Animal.dart
class Animal{String _name; //私有屬性int age; //默認構造函數的簡寫Animal(this._name,this.age);void printInfo(){ print("${this._name}----${this.age}");}String getName(){ return this._name;} void _run(){print('這是一個私有方法');}execRun(){this._run(); //類里面方法的相互調用}
}
main.dart
/*
Dart和其他面向對象語言不一樣,Data中沒有 public private protected這些訪問修飾符合但是我們可以使用_把一個屬性或者方法定義成私有。*/import 'lib/Animal.dart';void main(){Animal a=new Animal('小狗', 3);print(a.getName());a.execRun(); //間接的調用私有方法
}
class Rect{int height;int width;getArea(){return this.height*this.width;}
}class Rect{num height;num width; Rect(this.height,this.width);area(){return this.height*this.width;}
}void main(){Rect r=new Rect(10,4);print("面積:${r.area()}");
}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,2);print("面積:${r.area}"); //注意調用直接通過訪問屬性的方式訪問area
}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=6;print(r.area);}
// Dart中我們也可以在構造函數體運行之前初始化實例變量class Rect{int height;int width;Rect():height=2,width=10{print("${this.height}---${this.width}");}getArea(){return this.height*this.width;}
}void main(){Rect r=new Rect();print(r.getArea()); }
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态