Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.

📝 [Flutter] learning to form a record, [programmer essential knowledge]

📔 — Classes and objects in Flutter Dart!

1. Write at the front

After introducing anonymous functions and closures in Dart in the previous article, let’s move on to learning how classes and objects in Dart are defined and used.

The Apple Mac is configured with the Flutter development environment

Android Studio installs a third-party simulator for Flutter — netease MuMu

Failed to find Build Tools Revision 29.0.2

Android license status unknown. Run ‘Flutter doctor — Android – Licenses’

How to create a Flutter project and run your first Flutter project

Dart uses var, final, and const

Dart Indicates the num of the data type

Dart String of data type

Dart data type list&Map

Dart method and arrow function

Dart’s method passes optional parameters and methods as parameters

Dart, Dart, Dart, Dart

Class 2.

Use the class keyword to declare a class, much like Swift. You can use the new keyword to call constructors. All objects inherit from the Object class, much like OC.

2.1 Define a class

  • Define a class
class JPPerson {
  / / property
  String name = "reno";
  int? age;
  / / method
  void info(a) {
    print("name:$name age:$age"); }}Copy the code

If the property does not have a default value, you can use? Indicates that it can be null and is of type optional in Swift.

2.2 Class initialization

  • Class initialization
void main(a) {

  JPPerson person = JPPerson();
  person.age = 18;
  person.info();
}
Copy the code

The JPPerson class does not write constructors, but classes do by default.

2.3 Methods and Attributes

Property and method calls, like Swift, use point syntax.

  • Code run result

  1. Getter and setter methods are generated by default in DART
  2. Properties and methods are accessed via point syntax
  3. A final modified property must define an initial value

 // Final String nationality = "China ";
  final String nationality;
  String name = "reno";
  int? age;
  JPPerson(this.age,this.nationality);// constructor
  
Copy the code

If the final modifier is not given a default value, it can be written to the constructor and assigned when the class object is initialized.

2.4 Methods cannot be overloaded

  • Dart methods cannot be overloaded

Methods cannot be overloaded, even with different arguments, because the name of the method is the only indication that it cannot be the same.

There are no. H and. M files in DART, so how do you tell which are private and which are externally usable?

2.5 Private Properties and methods

Dart: There are no private keywords in dart. If you use the underscore prefix, it is private and not externally accessible. Now create a new class file.

class JPPerson {
  final String nationality;
  String? _name;
  int? height;
  int? _age;
  JPPerson(this._age,this.nationality,this.height);// constructor

  void _info() {
    print("name:$_name age:$_age");
  }
  void printP(a){
    _info();
    print("height:$height"); }}Copy the code

Some private access can not be, will report an error, as follows:

  • Anything that does not begin with an underscore is externally accessible

2.6 Global Methods

  • A global method in the same class file can access the class’s private properties and methods as follows:

3. Write in the back

Follow me, more content continues to output

  • CSDN
  • The Denver nuggets
  • Jane’s book

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹