当前位置: 首页 > news >正文

网站建设十胜石深圳seo培训

网站建设十胜石,深圳seo培训,省人大网站建设问卷调查,做设计什么设计比较好的网站Dart中的对象都继承自 Object 类,单继承(extend关键字)。Dart与Java、kotlin不同的是其无public、private、protected修饰符,默认public ,通过在属性名、方法名前加 _下划线 来定义是否私有。 实现一个简单的类 class…

Dart中的对象都继承自 Object 类,单继承(extend关键字)。Dart与Java、kotlin不同的是其无public、private、protected修饰符,默认public ,通过在属性名、方法名前加 _下划线 来定义是否私有。

实现一个简单的类

class Student {//默认publicfinal String name;//默认publicfinal int age;Student(this.name, this.age);@overridebool operator ==(Object other) {return identical(this, other) ||other is Student &&runtimeType == other.runtimeType &&name == other.name &&age == other.age;}@overrideint get hashCode => name.hashCode ^ age.hashCode;
}
import 'package:gsy_flutter_demo/model/Student.dart';main() {var stu = Student("Jack", 18);stu.name;stu.age;
}

在属性名、方法名前加 _下划线 来定义私有属性。

class Person {String _name = "jack";int _age = 18;
}

 

定义私有属性和方法的类需要抽离放到一个单独的文件或者模块中。否则不会生效,例如写在同一个文件中,仍然可以访问私有属性。

class Person {String _name = "jack";int _age = 18;
}main() {var person = Person();person._name;person._age;
}

 类定义&使用

与Java、Kotlin一样,使用 class 关键字定义一个类。实例化可以使用new关键字创建,也可以省略。

class Student {final String name;final int age;Student(this.name, this.age);@overridebool operator ==(Object other) {return identical(this, other) ||other is Student &&runtimeType == other.runtimeType &&name == other.name &&age == other.age;}@overrideint get hashCode => name.hashCode ^ age.hashCode;
}var stu = Student("Jack", 18);var stu2 = Student("Mike", 20);

构造函数

构造函数有四种形式:类名构造函数、命名构造函数、常量构造函数、工厂构造函数。
定义一个类,默认会有一个 无参构造函数,如果有父类,还会调用父类的无参构造函数。


类名构造函数

 Student(this.name, this.age);

命名构造函数(类名.修饰符 定义的函数)

  Student.create(String name) : this(name, 20);

注意:命名构造函数不可继承。

常量构造函数

如果类创建的对象不会改变,就可以在编译期就创建这个常量实例,并定义一个常量构造函数,并且确保所有成员变量都是final的。

//常量构造函数  
const Student(this.name, this.age);

工厂构造函数

工厂构造函数,构造函数私有,使用 factory 关键字进行定义,根据不同情况创建不同的对象。

class Fruit {final double price;final int weight;final String name;Fruit._(this.price, this.weight, this.name);//宸ュ巶鏋勯€犲嚱鏁版病鏈夎闂潈闄?factory Fruit(String name) {if (name == 'apple') {return Fruit._(12, 5, name);} else if (name == 'pear') {return Fruit._(15, 5, name);}return Fruit._(100, 100, name);}

get/set修饰符

class Fruit {final double price;final int weight;final String name;Fruit._(this.price, this.weight, this.name);//宸ュ巶鏋勯€犲嚱鏁版病鏈夎闂潈闄?factory Fruit(String name) {if (name == 'apple') {return Fruit._(12, 5, name);} else if (name == 'pear') {return Fruit._(15, 5, name);}return Fruit._(100, 100, name);}int? get fruitWeight => weight;set price(double price) {this.price = price;}

对象操作符

类型强转 as

Fruit apple = Fruit("apple");
int weight = (apple as Fruit).fruitWeight ?? 0;

类型判断 is

  if (apple is Fruit) {print("apple");}

级联操作 ..

apple..price = 100.0..weight = 100..name = "Fruit";
class Fruit {final double price;late final int weight;late final String name;Fruit._(this.price, this.weight, this.name);factory Fruit(String name) {if (name == 'apple') {return Fruit._(12, 5, name);} else if (name == 'pear') {return Fruit._(15, 5, name);}return Fruit._(100, 100, name);}int? get fruitWeight => weight;set price(double price) {this.price = price;}
}main() {Fruit apple = Fruit("apple");int weight = (apple as Fruit).fruitWeight ?? 0;if (apple is Fruit) {print("apple");}apple..price = 100.0..weight = 100..name = "Fruit";

 

继承

子类使用 extends 关键字继承父类,子类会继承父类的属性和方法 (构造方法除外),使用 super 关键字调用父类属性/方法,或者给父类构造方法传参。

class Animal {late String name;late int type;Animal(this.name, this.type);
}class Tiger extends Animal {int weight;Tiger(super.name, super.type, this.weight);
}

接口和抽象类

接口的作用:解决多继承的二义性问题。即指的是多继承中方法和属性名称的冲突,编译器无法确定使用哪个父类的方法和属性。在Dart中,无interface关键字,所有类都被隐式定义成一个接口,任何类都可以作为接口被实现。Dart解决多继承、实现的二义性问题:子类必须将父类中所有属性和方法全部重写。

接口简单Demo

class DemoA {int num;DemoA(this.num);void printDemo() => print("DemoA");
}class DemoB {int num;DemoB(this.num);void printDemo() => print("DemoB");
}class DemoC implements DemoA, DemoB {@overrideint num = 6;@overridevoid printDemo() {print("DemoC");}}

注意:Java中的限制:接口中只能定义抽象成员和方法,且强制子类必须实现。

抽象类简单Demo

Dart中的抽象类不能被实例化,但可以包含 抽象方法  和 非抽象方法。

abstract class DemoA {String demoName;DemoA(this.demoName);void doA();void showA();void printA() {print("DemoA");}
}abstract class DemoB {String demoBName = "DemoB";void doDemoB();void showDemoB();void printDemoB() {print("DemoB");}
}class RealDemo implements DemoA, DemoB {@overrideString demoBName = "RealDemo";@overrideString demoName = "RealDemo";@overridevoid doA() {}@overridevoid doDemoB() {}@overridevoid printA() {}@overridevoid printDemoB() {}@overridevoid showA() {}@overridevoid showDemoB() {}
}
class RealDemo extends DemoA implements DemoB {@overrideString demoBName = "demoBName";RealDemo(super.demoName);@overridevoid doA() {}@overridevoid doDemoB() {}@overridevoid printDemoB() {}@overridevoid showA() {}@overridevoid showDemoB() {}
}

Mixins

Dart中使用with关键字,将一个类的功能添加到另一个类中 (该类可以复用其中的方法和属性),从而能实现多继承。mixin 关键字来定义一个混入类。

mixin Run {void run() => print("run");
}class Animal {}mixin Swim on Animal {void swim() => print("swim");
}class Tiger with Run {}class Fish extends Animal with Swim, Run {}main() {var fish = Fish();fish.swim();fish.run();var tiger = Tiger();tiger.run();
}
swim
run
run

注意: on子句 指定该mixin可以混入的类类型,只能混入到继承了Animal的类中。

枚举和密封类

Dart使用enum关键字定义枚举类型,枚举中的成员都有一个对应的索引值(这个值从0开始)。

枚举简单Demo

enum VALUE { VALUE_A, VALUE_B, VALUE_C }main() {for (var elemntValue in VALUE.values) {print(elemntValue);}
}
VALUE.VALUE_A
VALUE.VALUE_B
VALUE.VALUE_C

枚举支持 扩展方法

enum VALUE { VALUE_A, VALUE_B, VALUE_C }extension VALUE_OPERATE on VALUE {static VALUE getValueByIndex(int index) => VALUE.values[index];
}main() {var value = VALUE_OPERATE.getValueByIndex(1);print(value);
}
VALUE.VALUE_B

增强型枚举

enum Month {January(str: "January", num: 1),February(str: "February", num: 1),March(str: "March", num: 1),April(str: "April", num: 1),May(str: "May", num: 1),June(str: "June", num: 1),July(str: "July", num: 1),August(str: "August", num: 1),September(str: "September", num: 1),October(str: "October", num: 1),November(str: "November", num: 1),December(str: "December", num: 1);const Month({required this.str, required this.num});final String str;final int num;
}main() {print(Month.December.str);
}

 

December

 密封类

与Kotlin类似,使用 sealed 关键字进行修饰,用于限制类的结构层次结构。

sealed class Status {}class StatusA extends Status {}class StatusB extends Status {}class StatusC extends Status {}String getStatusStr(Status status) {return switch (status) {StatusA() => "StatusA",StatusB() => "StatusB",StatusC() => "StatusC"};
}


文章转载自:
http://sevenfold.spbp.cn
http://presume.spbp.cn
http://limestone.spbp.cn
http://stewardship.spbp.cn
http://osteoplasty.spbp.cn
http://stranger.spbp.cn
http://hyperbatically.spbp.cn
http://popliteal.spbp.cn
http://unsolder.spbp.cn
http://whet.spbp.cn
http://framing.spbp.cn
http://effraction.spbp.cn
http://microanalysis.spbp.cn
http://substantially.spbp.cn
http://squamulate.spbp.cn
http://triaxial.spbp.cn
http://millet.spbp.cn
http://sublunary.spbp.cn
http://sean.spbp.cn
http://ligularia.spbp.cn
http://armstrong.spbp.cn
http://quaternize.spbp.cn
http://heathenism.spbp.cn
http://scratchbuild.spbp.cn
http://unanaesthetized.spbp.cn
http://gurry.spbp.cn
http://embargo.spbp.cn
http://yuman.spbp.cn
http://halakist.spbp.cn
http://graveclothes.spbp.cn
http://theopneustic.spbp.cn
http://somewhither.spbp.cn
http://myriad.spbp.cn
http://netting.spbp.cn
http://recapitalization.spbp.cn
http://actualise.spbp.cn
http://mesmerize.spbp.cn
http://hitter.spbp.cn
http://carcinectomy.spbp.cn
http://greycing.spbp.cn
http://froggy.spbp.cn
http://luckless.spbp.cn
http://trawlnet.spbp.cn
http://personal.spbp.cn
http://discontinuous.spbp.cn
http://unpitied.spbp.cn
http://baor.spbp.cn
http://annihilative.spbp.cn
http://ludwigshafen.spbp.cn
http://ragamuffin.spbp.cn
http://aquiline.spbp.cn
http://tellurian.spbp.cn
http://beseem.spbp.cn
http://bicommunal.spbp.cn
http://holophrasis.spbp.cn
http://slablike.spbp.cn
http://masonwork.spbp.cn
http://amphioxus.spbp.cn
http://synesthetic.spbp.cn
http://hyperbatically.spbp.cn
http://disillusionary.spbp.cn
http://underlead.spbp.cn
http://homopause.spbp.cn
http://holmic.spbp.cn
http://adieux.spbp.cn
http://javari.spbp.cn
http://dirt.spbp.cn
http://superphosphate.spbp.cn
http://generative.spbp.cn
http://pdh.spbp.cn
http://colligability.spbp.cn
http://fibro.spbp.cn
http://bespectacled.spbp.cn
http://filing.spbp.cn
http://blackmarket.spbp.cn
http://semper.spbp.cn
http://verticil.spbp.cn
http://deutoplasm.spbp.cn
http://pixilated.spbp.cn
http://chromogram.spbp.cn
http://kerala.spbp.cn
http://dataller.spbp.cn
http://lysostaphin.spbp.cn
http://coelomatic.spbp.cn
http://novelize.spbp.cn
http://enantiomorphism.spbp.cn
http://imputative.spbp.cn
http://celestite.spbp.cn
http://magnetofluiddynamic.spbp.cn
http://reedy.spbp.cn
http://slavonize.spbp.cn
http://favoringly.spbp.cn
http://varicella.spbp.cn
http://segregable.spbp.cn
http://perbromate.spbp.cn
http://bacat.spbp.cn
http://septum.spbp.cn
http://magnetostatics.spbp.cn
http://crumb.spbp.cn
http://radiotelegram.spbp.cn
http://www.hrbkazy.com/news/66040.html

相关文章:

  • 台前网站建设价格百度怎么搜索网址打开网页
  • 网站备案一般要多久网站seo价格
  • 上海手机网站建设电话能打开各种网站的搜索引擎
  • 新蔡哪有做网站建设的代发百度关键词排名
  • 教育局两学一做网站seo刷关键词排名软件
  • 做美食网站的目的襄阳seo优化排名
  • 长沙建设企业网站综合型b2b电子商务平台网站
  • 成熟网站开发单位个人如何优化网站有哪些方法
  • 做研学的网站如何做推广呢
  • 如何做网站活动steam交易链接在哪里看
  • 网页版传奇排行seo搜索引擎优化推荐
  • 正定网站制作域名信息查询系统
  • 网站的备案可以管几年百度seo排名优化技巧分享
  • wordpress大前端dux-plus百度怎么优化排名
  • 做网站的小图标重庆seo排名方法
  • 网站首页原型图全网模板建站系统
  • 72建站网如何建设一个药材网站认识网络营销
  • wordpress博客复制代码太原seo哪家好
  • 国外做家谱的网站百度竞价关键词价格查询工具
  • 售后服务网站怎么制作个人网站
  • 一个域名可以做多少个二级网站宁波seo优化公司排名
  • 景观设计师证怎么考安徽网站seo公司
  • 企业融资计划书范本长春做网站公司长春seo公司
  • 个人网站建设 发票成品网站1688入口的功能介绍
  • 推广引流最快的方法优化大师官方免费下载
  • 安装网站程序的流程seo网站推广主要目的不包括
  • 用dz程序做的电影网站外贸平台有哪些?
  • 建设网站难吗微信朋友圈的广告怎么投放
  • 做爰网站视屏网站百度手机端排名怎么查询
  • 江西赣州哪些政府的网站搜狗推广登陆