[Flutter] Classes and objects in Dart

1. Write in front

Introduced in the previous articleDartmiddleanonymous function,Closure, then continue to study nowDartmiddlekindandobjecthow it is defined and used.
Flutter

[Flutter] Apple Mac computer configuration flutter development environment

[Flutter] Android Studio installs a third-party emulator - Netease MuMu

[Flutter] Project running error Failed to find Build Tools revision 29.0.2

【Flutter】flutter doctor 报错Android license status unknown. Run `flutter doctor --android-licenses‘

[Flutter] How to create a new project and run your first flutter project

[Flutter] Basic use of var, final and const in Dart

[Flutter] num of Dart data type

[Flutter] String of Dart data type

[Flutter] Dart's data type list&Map (array and dictionary)

[Flutter] Dart's method and arrow function

[Flutter] Optional parameters and methods in Dart's methods are passed as parameters

[Flutter] Anonymous functions and closures in Dart

2. Class

useclasskeyword to declare a class, much like Swift, you can usenewkeyword to callConstructor,allobjectboth inheritObjectclass, this andOCA bit like.

2.1 Defining a class

  • define a class
class JPPerson { 
  // property 
  String name =  "reno" ; 
  int ? age ; 
  //method 
  void  info ( )  { 
    print ( "name:$name age:$age" ) ; 
  } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

If the property does not assign a default value, you can use?Represents nullable, type inSwiftoptional inoptiontype.

2.2 Class initialization

  • class initialization
void  main ( )  {

  JPPerson person = JPPerson();
  person.age = 18;
  person.info();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

JPPersonThe class does not have a constructor, but the class has a constructor by default.

2.3 Methods and properties

Properties and method calls, like Swift, use dot syntax.

  • code running result

operation result

  1. Getter and setter methods are generated by default in dart
  2. Properties and methods are accessed via dot syntax
  3. final modified properties must define an initial value

 // final String nationality = "China"; 
  final String nationality ; 
  String name =  "reno" ; 
  int ? age ; 
  JPPerson ( this . age , this . nationality ) ; // Constructor
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

finalIf the modified one does not give a default value, it can be written in the constructor, and it can be assigned when initializing the class object.

2.4 Methods cannot be overloaded

  • Methods in dart cannot be overloaded

Methods cannot be overloaded

The method cannot be overloaded, even if the parameters are different, because the method name is the only identifier and cannot be the same.

existdartin no.hand.mfile, then how to distinguish which are private and which can be used externally?

2.5 Private Properties and Methods

existdartThere are no private keywords in it. If you use the underscore prefix directly, it is private and cannot be accessed from the outside. Then create a new class file now. Be careful not to use the camel case name, otherwise it will warn you ⚠️,jp_person.dartcode show as below.

class JPPerson {
  final String nationality;
  String? _name;
  int? height;
  int? _age;
  JPPerson(this._age,this.nationality,this.height);//构造方法

  void _info() {
    print("name:$_name age:$_age");
  }
  void printP(){
    _info();
    print("height:$height");
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

The private one cannot be accessed, and an error will be reported, as follows:

Private properties and methods cannot be accessed

  • Anything without an underscore at the beginning is accessible externally

Example

2.6 Global methods

  • Global methods in the same class file can access the private properties and methods of the class, as follows:

code example

3. Write at the back

Follow me, more content will continue to be output

🌹 If you like it, give it a like👍🌹

🌹 If you think you have something to gain, you can come to a wave of favorites + follow, so as not to find me next time😁🌹

🌹Welcome everyone to leave a message to exchange, criticize and correct,ForwardPlease indicate the source, thank you for your support! 🌹

Related: [Flutter] Classes and objects in Dart