Detailed explanation of dart class in java
- 2021-08-17 00:10:05
- OfStack
dart is an object-oriented language; Object-oriented
Inheritance Encapsulation PolymorphismEverything in dart is an object, and all objects are inherited from the object class
1 class is usually composed of properties and methods
In dart, if you want to customize a class, put this class outside the main function
Class name uses big hump method name uses small hump
1. Define the properties and methods of this class
// Definition 1 Properties and Methods of Classes
class Person {
String name = ' Zhang 3';
int age = 19;
void getInfo() {
// print(' My name is $name, This year $age'); No. 1 1 Species
// It is recommended to write like this through this To visit
print("${this.name}====${this.age}");
}
}
// Using properties and methods in a class
void main() {
// Call Perso This class You first need to instantiate this class
// Person p = new Person(); // Equivalence var p=new Person
var p = new Person();
print(p.age);
p.getInfo();
}
2. Changing properties and methods in a class
class Person {
String name = ' Zhang 3';
int age = 19;
void getInfo() {
// It is recommended to use this writing
print("${this.name}====${this.age}");
}
// Restrict types and change properties and methods in classes
SetUserinfo(int age) {
this.age = age;
}
}
void main() {
// Call Perso This class You first need to instantiate this class
Person p = new Person(); // Equivalence var p=new Person
print(p.age);//19
p.getInfo();// Zhang 3====19
p.SetUserinfo(100);
p.getInfo();// Zhang 3====100
}
3. Default constructor
class Person {
// This is 1 Default constructors
Person() {
print(' The contents of the default constructor, this method will automatically start when instantiating, ha ');
}
}
void main() {
// When instantiated, the default constructor is automatically triggered
Person p = new Person(); // Equivalence var p=new Person
}
4. Dynamic passing, multiple instantiation of classes, printing different values
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void getuserInfo() {
print(this.name);
print(this.age);
}
}
void main() {
// When instantiated, it will automatically trigger
Person p = new Person(' Zhang 3', 10); // Equivalence var p=new Person
p.getuserInfo();
// This class can be instantiated several times
Person v = new Person(' Zhang 31', 100); // Equivalence var p=new Person
v.getuserInfo();
}
5. Abbreviation of default constructor
class Person {
String name;
int age;
// Person(String name, int age) {
// this.name = name;
// this.age = age;
// }
// The code annotated above is equivalent to Person(this.name, this.age);
Person(this.name, this.age);
void getuserInfo() {
print(this.name);
print(this.age);
}
}
6. Named constructor, named constructor can be written multiple; But the default constructor can only write 1
class Person {
// I am the default constructor
Person() {
print(' I am the default constructor ');
}
// Named constructor
Person.mynow() {
print(' I am a named constructor ');
}
}
void main() {
// When instantiated, the construction automatically triggers by default
Person p = new Person(); // Equivalence var p=new Person
Person v = new Person.mynow(); // Triggering a named constructor
}
7. Pull out the class and put it in a separate folder
Create in the root directory of the project 1 Folders lib
In lib Folder to create 1 Files xxx.dart The contents are as follows
class Person {
// I am the default constructor
Person() {
print(' I am the default constructor ');
}
// Named constructor
Person.mynow() {
print(' I am a named constructor ');
}
}
Introduce in a file that is required
import 'lib/xxx.dart';
void main() {
// When instantiated, the construction automatically triggers by default
Person p = new Person(); // Equivalence var p=new Person
Person v = new Person.mynow(); // Triggering a named constructor
}
8. dart modifier
dart is different from other object-oriented languages. There is no access modifier to public private
However, we can use _ to define a property or method as private (that is, add _ before the variable or method; And to separate him out into a file in the lib directory, so that the outside can not be accessed);
But I think this has no real meaning. If you want to access this private method, add a common method to the class; To access this private method; Access back at return; You can access this private method private method, only in the current class to access the animal. dart file in the lib directory;
The contents are as follows
class Animal {
String _name = 'dog';// This is a private method, which can only be accessed by the current class; Other classes are inaccessible, ha ~ ;
// If you have to access this class, you can access it through a common method
// Access this class through public methods; Indirect access
getName(){
print(this._name);
}
int age = 19;
void getInfo() {
// It is recommended to use this writing
print("${this._name}====${this.age}");
}
// Restrict types and change properties and methods in classes
SetUserinfo(int age) {
this.age = age;
}
}
9. Calculate the area of rectangle instead of get method
class Rect {
num width;
num height;
Rect(this.width, this.height);
getUserINfo() {
return (this.width * this.height);
}
}
void main() {
// When instantiated, the construction automatically triggers by default
Rect p = new Rect(10, 4); // Equivalence var p=new Person
print(p.getUserINfo());
}
Change the above code to read get Method
class Rect {
num width;
num height;
Rect(this.width, this.height);
// getUserINfo() {
// return (this.width * this.height);
// }
// Change the above code to get
get getUserINfo {
return (this.width * this.height);
}
}
void main() {
// When instantiated, the construction automatically triggers by default
Rect p = new Rect(10, 4);
// print(p.getUserINfo());
// Change the above code to read get Method call
print(p.getUserINfo);// When calling, the method is accessed directly by accessing the property
}
10. set method in dart
class Rect {
num width;
num height;
Rect(this.width, this.height);
// When setting the value. We used set Method
set areaHeight(value) {
this.height = value;
}
get area {
return this.height;
}
}
void main() {
// When instantiated, the construction automatically triggers by default
Rect p = new Rect(10, 4);
p.areaHeight = 6; // When 1 The interior of the class uses the set Method. We use assignment directly;
print(p.area); // Internally used get We get the value directly through the attribute
}
11. Before instantiating, initialize values inside the class
class Person {
String name = ' Zhang 3';
int age = 19;
void getInfo() {
// It is recommended to use this writing
print("${this.name}====${this.age}");
}
// Restrict types and change properties and methods in classes
SetUserinfo(int age) {
this.age = age;
}
}
void main() {
// Call Perso This class You first need to instantiate this class
Person p = new Person(); // Equivalence var p=new Person
print(p.age);//19
p.getInfo();// Zhang 3====19
p.SetUserinfo(100);
p.getInfo();// Zhang 3====100
}
0