In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Android development Dart Constructors constructor how to use", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Android development Dart Constructors constructor how to use" it!
What is a constructor?
Constructors are special methods for initializing objects. The constructor is called when an object of the class is created.
Default
Final ehe = MyClass (); / / Creates an instanceclass MyClass {MyClass (); / / Fires immediately when created (this guy is cons.)}
There is only one rule in the constructor
In other words,
Name it like its class name!
Okay, we got it! But...
What specific types of constructor types do we have?
The default constructor Class () / / Default Constructor// does nothing by default class User {String name = 'ehe'; User ();} / Constructor with parameters// constructor when the initial variable class User {String name; User (this.name);} / Constructor with the initial method// constructor writes your logical class User {String name User (this.name) {/ / do some magic}} / / Constructor with assertion// use Asserts to check your rule class User {String name; User (this.name): assert (name.length > 3);} / / Constructor with initializer// initializes your variable class User {static String uppercase (String e) = > e.toUpperCase (); String name; User (name): name = yell (name); static String yell (String e) = > e.toUpperCase () } / / Constructor with super () / / override variable class Person {String id; Person (this.id);} class User extends Person {String name; User (this.name, String id): super (id);} / Constructor with this () / / named constructor class User {String name; int salary; User (this.name, this.salary); User.worker (String name): this (name, 10); User.boss (String name): this (name, 9999999) } Private constructor "Class._ ()"
You can use _ to create a private constructor, but what are the benefits?
Let's look at an example!
Class Print {static void log (String message) = > print (message);} Print.log ('ehe'); / / you want to write a util like this, but there is a problem, because you can also create an instance that we don't want. Print (); / / in this case, this is absolutely unnecessary / / how to prevent this situation? The answer is a private constructor! Class Print {Print._ (); / / this will prevent the creation of an instance static void log (String message) = > print (message);} Print (); / / this will give the compile-time error Your instance is safe now!
So basically you can stop creating an instance!
Name the constructor named class.Named ()
You can create different types of instances in one class
For example
For example:
Class User {String name; int salary; User.worker (this.name): salary = 10; User.boss (this.name): salary = 99999999;} private naming constructor named class._Named ()
You can easily clean your instance!
Class User {String name; int salary; User.worker (this.name): salary = 10; User.boss (this.name): salary = 99999999; User._mafia (this.name): salary = 9999999999;}
Apart from jokes, this is very helpful!
For example, you can create a singleton object using a private constructor!
Class User {User._privateConstructor (); static final User instance = User._privateConstructor ();}
Be careful
You can see the internal keyword _ internal in some projects. Nothing special. _ internal construction is just a. _ internal usually gives the class the name of the private constructor (not required). You can create a private constructor using any Class._someName structure.
Const Constructor destroy const Class ()
You can use const constructor! The constructor makes the class immutable!
The constant constructor is an optimization! The compiler makes objects immutable for all Text ('hides') Object. "Frank Treacy
Const user = User ('ehe'); class User {final String name; const User (this.name);} factory constructor factory factory class Class ()
We said the builders were not allowed to come back, and guess what?
Factory builders can!
What else can factory builders do?
You don't need to create a new instance at all! You can call another constructor or subclass, or even return an instance from the cache!
Finally, a little warning to the factory!
Cannot call the superclass constructor (super ())
A simple example
Class User {final String name; User (this.name); factory User.fromJson (Map json) {return User (json ["name"]);} / / Singleton Exampleclass User {User._internal (); static final User _ singleton = Singleton._internal (); factory User () = > _ singleton } Thank you for reading, the above is the content of "how to use the Dart Constructors constructor in Android development". After the study of this article, I believe you have a deeper understanding of how to use the Dart Constructors constructor in Android development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.