Dart objects and classes

/ * 

The three basic features of object-oriented programming (OOP) is: encapsulation, inheritance, polymorphism,       

      encapsulation: a package is the main characteristic of the concept of objects and classes. Package, the objective things packaged as an abstract class, and part of his own properties and methods available to other objects call, while a part of the hidden properties and methods. 
                
      Inheritance: A major feature of object-oriented programming (OOP) language is "inherited." Inheritance refers to such a capability: it can use the functionality of existing classes, without having to rewrite and expand these functions in the case of the original class. 
            
      Polymorphism: subclass pointer type allows type assigned to the parent pointer, the same function call performed have different effects. 


Dart everything is an object, all objects are inherited from the Object class. 

Dart is to use a single inheritance class and object-oriented languages, all objects are instances of classes, and all Object classes are subclasses of 

a class is generally composed of properties and methods. 

* / 

Void main () {     
    
    List  List = new new  List ();
     List . IsEmpty;
     List .add ( 'banana' );
     List .add ( 'Banana. 1' ); 





    the Map m = new new the Map (); 
    m ["username"]="张三";
    m.addAll({"age":20});
    m.isEmpty;

    Object a=123;
    Object v=true;
    print(a);
    print(v);

}
/ * 

Dart is using a single inheritance class and object-oriented languages, all objects are instances of classes, and all classes are subclasses of Object 

* / 

class the Person {
   String name = "John Doe" ; 
  int Age 23 is = ; 
  void getInfo () { 
      // Print ( "$ ---- $ name Age"); 
      Print ( "$ ---- $ {} {this.name this.age}" ); 
  } 
  void setInfo ( Age int) { 
    the this .age = Age; 
  } 

} 
void main () { 

  // instantiate 

  // new new var P1 = the Person (); 
  // Print (p1.name); 
  // p1.getInfo (); 

  the Person P1 = new new the Person ();
   // Print (p1.name); 

  p1.setInfo(28);
  p1.getInfo();
  


}
// class the Person { 
// String name = 'John Doe'; 
// int = 20 is Age; 
// default constructor // 
// the Person () { 
// Print ( 'which is the constructor function of the content of this method examples of the time trigger '); 
//} 
// void printInfo () {    
// Print ( "$ ---- $ {} {this.name this.age}"); 
//} 
//} 

// Wang Wu Zhangsanlisi 

// the Person class { 
// String name; 
// int Age; 
// default constructor // 
// the Person (String name, int Age) { 
// this.name = name; 
// = this.age Age; 
//} 
// void printInfo () {    
// Print ( "$ ---- $ {} {this.name this.age}"); 
//} 
//} 


class the Person {
   Stringname; 
  int Age; 
  // default constructor shorthand 
  the Person (this.name, the this. Age); 
  void printInfo () {    
    Print ( "$ ---- $ {} {this.name this.age}" ); 
  } 
} 


void main () { 
  
  the Person P1 = new new the Person ( 'John Doe', 20 is ); 
  P1 . printInfo (); 


  the Person P2 = new new the Person ( 'John Doe', 25 ); 
  P2 . printInfo (); 

}
/ * 
DART inside the constructor can write more 
* / 
class the Person {
   String name; 
  int Age; 
  // default constructor shorthand for 
  the Person (this.name, the this. Age); 
  
  the Person . Now () {
     Print ( 'I named constructors' ); 
  } 

  the Person .setInfo ( String name, int Age) { 
    the this .name = name; 
    the this .age = Age; 
  } 

  void printInfo () {    
    Print ( "$ ---- $ {} this.name this.age} { " ); 
  } 
} 

void main () { 


  //var d = new DateTime.now (); // instantiate DateTime calling its constructor named 
  // Print (D); 


  // the Person new new P1 = the Person ( 'John Doe', 20); // default instance of the class when the call is the default constructor 

  // Person p1 = new Person.now () ; // constructor named 
  the Person P1 = new new Person.setInfo ( 'John Doe', 30 ); 
  P1 . printInfo (); 

}
import 'lib/Person.dart';

void main(){

  Person p1=new Person.setInfo('李四1',30);
  p1.printInfo(); 

}

Person.dart

class the Person {
   String name; 
  int Age; 
  // default constructor shorthand 
  the Person (. this.name, the this Age); 
  
  the Person . now () {
     Print ( 'I am named constructors' ); 
  } 

  the Person .setInfo ( String name, int Age) { 
    the this .name = name; 
    the this .age = Age; 
  } 

  void printInfo () {    
    Print ( "$ ---- $ {} {this.name this.age}" ); 
  } 
}

/ * 
Dart and other object-oriented languages are not the same, Data is not public private protected access these modifications in line 

but we can use _ to be defined as a private property or method. 

* / 

Import 'lib / Animal.dart' ; 

void main () { 
 
 Animal A = new new Animal ( 'dog',. 3 ); 

 Print (A. GetName ()); 

  A .execRun ();    // indirect call private methods 
 

}
class Animal {
   String the _name;    // private attributes 
  int Age; 
   // default constructor shorthand 
  Animal (. this._name, the this Age); 

  void printInfo () {    
    Print ( "$ ---- $ {} this._name this.age} { " ); 
  } 

  String getName () { 
     return the this. the _name; 
  } 
  void the _run () { 
    Print ( 'which is a private method' ); 
  } 

  execRun () { 
    the this ._run ();   // class call each other inside method 
  } 
}
// class Rect{

//   int height;
//   int width;
 
//   getArea(){
//     return this.height*this.width;
//   } 
// }


// class Rect{
//   num height;
//   num width; 
  
//   Rect(this.height,this.width);
//   area(){
//     return this.height*this.width;
//   }
// }

// void main(){
//   Rect r=new Rect(10,4);
//   print("面积:${r.area()}");   
// }

// class Rect{
//   num height;
//   num width;   
//   Rect(this.height,this.width);
//   get area{
//     return this.height*this.width;
//   }
// }

Void main // () { 
// new new = R & lt Rect Rect (10,2); 
// Print ( "Area: $ {r.area}"); // call the attention area accessed directly access attribute mode 
// } 

class Rect { 
  NUM height; 
  NUM width; 
  
  Rect (the this .height, the this. width); 
  GET Area { 
    return this.height * the this. width; 
  } 
  SET areaHeight (value) { 
    the this .height = value; 
  } 
} 

void main () { 
  Rect R & lt = new new Rect (10,4 );
   // Print ( "area: $ {r.area ()}");    
  r.areaHeight =. 6 ; 

  Print (R & lt. area); 

}
// Dart we may initialize instance variables body running before the constructor 

class Rect { 

  int height; 
  int width; 
  Rect () : height = 2, width = 10 { 
    
    Print ( "$ --- $ {} this.height this.width} { " ); 
  } 
  the getArea () { 
    return this.height * the this. width; 
  } 
} 

void main () { 
  Rect R & lt = new new Rect ();
   Print (R & lt. the getArea ()); 
   
}

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11026722.html