类的定义
class 类名{
属性的定义;
.....
方法的定义;
......
}
示例
void main() {
Dog dog = Dog();
dog.eat();
}
class Animal{
late String name;
late String height;
void eat(){
}
}
//继承
class Dog extends Animal{
void eat() {
print("撕咬");
}
}
//继承
class Fish extends Animal{
void eat() {//多态
print("吸");
}
}
构造方法
(1)默认构造方法
//无需声明,就能调用
Dog dog = Dog();
class Dog {
void eat() {
print("撕咬");
}
}
(2)类名构造方法
void main() {
Animal animal = Animal("L", "80");
}
class Animal {
late String name;
late String height;
Animal(String name, String height) {
this.name = name;
this.height = height;
}
void eat() {}
}
(3)命名构造方法
class 类名{
类名.标识名(参数){
}
}
void main() {
Animal animal2 = Animal.create("A");
}
class Animal {
late String name;
late String height;
Animal.create(String n) {
name = n;
}
void eat() {}
}
存储器和访问器
Dart语言中使用set关键字定义存储器,使用get关键字定义访问器。
一般不建议使用,能看懂就行。一般情况就直接使用。
set 属性名(参数){
}
返回值类型 get 属性名{
return 返回值;
}
void main() {
Animal animal2 = Animal();
animal2.setName = "1111";
print(animal2.mName);
}
class Animal {
late String name;
late String height;
set setName(String name) {
this.name = name;
}
get mName {
return name;
}
}
继承的定义
使用extends关键字继承一个类,子类会继承父类可见的属性和方法。不会继承构造方法。
只能有一个父类。
class 子类名 extends 父类名{
}
示例
class Shape {
late String name;
void show() {
print(name);
}
}
class Circle extends Shape {
late double r;
double area() {
return 3.14 * r * r;
}
}
重写子类方法
void main() {
Circle circle = Circle();
circle.name = "圆";
circle.r = 2;
circle.show();
circle.showArea();
}
class Shape {
late String name;
void show() {
print(name);
}
void showArea(){
print("面积");
}
}
class Circle extends Shape {
late double r;
double area() {
return 3.14 * r * r;
}
void showArea(){//重写
print("面积" + area().toString());
}
}
I/flutter ( 2416): 圆
I/flutter ( 2416): 面积12.56
多态
多态是指同一行为作用于不同对象时,可以表现出多种不同的形式和结果来。
void main() {
Dog dog = Dog();
dog.name = "小黑";
dog.call();
Car car = Car();
car.name = "咪咪";
car.call();
}
class Animal{
late String name;
void call(){
}
}
class Dog extends Animal{
void call(){
print("小狗$name叫:汪汪");
}
}
class Car extends Animal{
void call(){
print("小猫$name叫:喵喵");
}
}
I/flutter ( 2416): 小狗小黑叫:汪汪
I/flutter ( 2416): 小猫咪咪叫:喵喵
构造方法的调用
如果父类中有无参构造方法,则子类的构造方法会默认调用父类的无参构造方法。
void main() {
Dog dog = Dog();
dog.name = "小黑";
dog.call();
}
class Animal {
late String name;
Animal() {
print("Animal");
}
void call() {
}
}
class Dog extends Animal {
void call() {
print("小狗$name叫:汪汪");
}
Dog(){
print("Dog");
}
}
I/flutter ( 2416): Animal
I/flutter ( 2416): Dog
I/flutter ( 2416): 小狗小黑叫:汪汪
如果父类没有无参构造方法,则需要在构造方法参数后使用“:”显式调用父类的自定义构造方法。
void main() {
Dog dog = Dog();
dog.name = "小黑";
dog.call();
}
class Animal {
late String name;
Animal(var s) {
print("Animal____$s");
}
void call() {
}
}
class Dog extends Animal {
void call() {
print("小狗$name叫:汪汪");
}
Dog() : super("aaa"){
print("Dog");
}
}
I/flutter ( 2416): Animal____aaa
I/flutter ( 2416): Dog
I/flutter ( 2416): 小狗小黑叫:汪汪
如果类中的一个构造方法调用类中的其他构造方法,则这种构造方法称之为重定向构造方法。
void main() {
Animal animal = Animal("鸡", 2);
print(animal.name);
Animal dog = Animal.proDog(4);
print(dog.name);
print(dog.legs);
}
class Animal {
late String name;
late int legs;
Animal(this.name, this.legs);
Animal.proDog(int legs) : this("Dog", legs);
void call() {}
}
I/flutter ( 2416): 鸡
I/flutter ( 2416): Dog
I/flutter ( 2416): 4
抽象类
abstract class 类名{
}
示例
abstract class Animal {
void go();//没有方法体
}
class Dog extends Animal{
void go() {
print("在路上跑");
}
}
class bird extends Animal{
void go() {
print("在天上飞");
}
}
接口
dart中没有interface,直接使用抽象类作为接口。
abstract class Animal {
void go() ;
}
class Sparrow implements Animal{
void go() {
// TODO: implement go
}
}
混入
能够将一个或者多个父类的功能添加到子类中,而无需继承这些父类。
void main() {
FeiShu feiShu = FeiShu();
feiShu.go();
}
//dart版本至少3.0
mixin class Dog {
void go() {
print("在路上跑");
}
}
mixin class Bird {
void go() {
print("在天上飞");
}
}
class FeiShu extends Dog with Bird{
}
//等同
/*class FeiShu with Dog,Bird{
}*/
I/flutter ( 2416): 在天上飞
泛型
class 类名<T>{
T t;
}
示例
void main() {
Info<int> info1 = Info(10);
info1.printInfo();
}
class Info<T>{
T value;
Info(this.value);
void printInfo(){
print("$value is $T");
}
}
I/flutter ( 2416): 10 is int
泛型接口
class MySQL {}
class MsSql {}
class MoSql {}
void main() {
DB<MySQL> mySql = DB();
DB<MoSql> moDB = DB();
}
abstract class OperateDB<T> {
//接口
late T currentRecords;
T getRecord(int index);
bool insertRecord(int index, T mySql);
}
class DB<T> implements OperateDB<T> {
DB() {
print("正在使用的是$T 类型的数据库");
}
late T currentRecords;
T getRecord(int index) {
// TODO: implement getRecord
throw UnimplementedError();
}
bool insertRecord(int index, T mySql) {
// TODO: implement insertRecord
throw UnimplementedError();
}
}
泛型方法
返回类型 方法名<类型参数>(参数列表) {
// 方法体
}
void main() {
List<int> intList = [1, 2, 3, 4, 5, 6, 7];
PrintList printList = PrintList();
printList.show(intList);
}
//定义一个可以显示数组元素的泛型方法,并且数组元素可以是任何类型的数据。
class PrintList {
void show<T>(List<T> name) {
for (var element in name) {
print(element);
}
}
}
I/flutter (19868): 1
I/flutter (19868): 2
I/flutter (19868): 3
I/flutter (19868): 4
I/flutter (19868): 5
I/flutter (19868): 6
I/flutter (19868): 7