dart語言,Dart基礎第9篇:對象、類

 2023-12-25 阅读 28 评论 0

摘要:文章目錄一丶 面向對象的介紹 以及Data內置對象二丶 創建類使用類三丶 自定義類的默認構造函數四丶 自定義類的命名構造函數五丶 把類單獨抽離成一個模塊六丶 私有方法 和私有屬性七丶getter和setter修飾符的用法八丶 類中的初始化列表 一丶 面向對象的介紹 以及Data內置對象

文章目錄

    • 一丶 面向對象的介紹 以及Data內置對象
    • 二丶 創建類使用類
    • 三丶 自定義類的默認構造函數
    • 四丶 自定義類的命名構造函數
    • 五丶 把類單獨抽離成一個模塊
    • 六丶 私有方法 和私有屬性
    • 七丶getter和setter修飾符的用法
    • 八丶 類中的初始化列表

一丶 面向對象的介紹 以及Data內置對象

dart語言?面向對象編程(OOP)的三個基本特征是:封裝、繼承、多態

封裝:封裝是對象和類概念的主要特性。封裝,把客觀事物封裝成抽象的類,并且把自己的部分屬性和方法提供給其他對象調用, 而一部分屬性和方法則隱藏。

繼承:面向對象編程 (OOP) 語言的一個主要功能就是“繼承”。繼承是指這樣一種能力:它可以使用現有類的功能,并在無需重新編寫原來的類的情況下對這些功能進行擴展。

python類和對象、多態:允許將子類類型的指針賦值給父類類型的指針, 同一個函數調用會有不同的執行效果 。

Dart所有的東西都是對象,所有的對象都繼承自Object類。

Dart是一門使用類和單繼承的面向對象語言,所有的對象都是類的實例,并且所有的類都是Object的子類

object類的方法,一個類通常由屬性和方法組成。

void main(){    List list=new List();list.isEmpty;list.add('香蕉');list.add('香蕉1');Map m=new Map();m["username"]="張三";m.addAll({"age":20});m.isEmpty;Object a=123;Object v=true;print(a);print(v);
}

二丶 創建類使用類

Dart是一門使用類和單繼承的面向對象語言,所有的對象都是類的實例,并且所有的類都是Object的子類

class Person{String name="張三";int age=23;void getInfo(){// print("$name----$age");print("${this.name}----${this.age}");}void setInfo(int age){this.age=age;}
}
void main(){//實例化var p1=new Person();print(p1.name);p1.getInfo();Person p1=new Person();print(p1.name);p1.setInfo(28);p1.getInfo();}

三丶 自定義類的默認構造函數

class Person{String name='張三';int age=20; //默認構造函數Person(){print('這是構造函數里面的內容  這個方法在實例化的時候觸發');}void printInfo(){   print("${this.name}----${this.age}");}
}//張三  李四  王五class Person{String name;int age; //默認構造函數Person(String name,int age){this.name=name;this.age=age;}void printInfo(){   print("${this.name}----${this.age}");}
}class Person{String name;int age; //默認構造函數的簡寫Person(this.name,this.age);void printInfo(){   print("${this.name}----${this.age}");}
}void main(){Person p1=new Person('張三',20);p1.printInfo();Person p2=new Person('李四',25);p2.printInfo();}

四丶 自定義類的命名構造函數

dart里面構造函數可以寫多個

class Person{String name;int age; //默認構造函數的簡寫Person(this.name,this.age);Person.now(){print('我是命名構造函數');}Person.setInfo(String name,int age){this.name=name;this.age=age;}void printInfo(){   print("${this.name}----${this.age}");}
}void main(){var d=new DateTime.now();   //實例化DateTime調用它的命名構造函數print(d);Person p1=new Person('張三', 20);   //默認實例化類的時候調用的是 默認構造函數Person p1=new Person.now();   //命名構造函數Person p1=new Person.setInfo('李四',30);p1.printInfo(); 
}

五丶 把類單獨抽離成一個模塊

Person.dart

class Person{String name;int age; //默認構造函數的簡寫Person(this.name,this.age);Person.now(){print('我是命名構造函數');}Person.setInfo(String name,int age){this.name=name;this.age=age;}void printInfo(){   print("${this.name}----${this.age}");}
}

main.dart

import 'lib/Person.dart';void main(){Person p1=new Person.setInfo('李四1',30);p1.printInfo(); 
}

六丶 私有方法 和私有屬性

Animal.dart

class Animal{String _name;   //私有屬性int age; //默認構造函數的簡寫Animal(this._name,this.age);void printInfo(){   print("${this._name}----${this.age}");}String getName(){ return this._name;} void _run(){print('這是一個私有方法');}execRun(){this._run();  //類里面方法的相互調用}
}

main.dart

/*
Dart和其他面向對象語言不一樣,Data中沒有 public  private protected這些訪問修飾符合但是我們可以使用_把一個屬性或者方法定義成私有。*/import 'lib/Animal.dart';void main(){Animal a=new Animal('小狗', 3);print(a.getName());a.execRun();   //間接的調用私有方法
}

七丶getter和setter修飾符的用法


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(){Rect r=new Rect(10,2);print("面積:${r.area}");      //注意調用直接通過訪問屬性的方式訪問area
}class Rect{num height;num width; Rect(this.height,this.width);get area{return this.height*this.width;}set areaHeight(value){this.height=value;}
}void main(){Rect r=new Rect(10,4);// print("面積:${r.area()}");   r.areaHeight=6;print(r.area);}

八丶 類中的初始化列表

// Dart中我們也可以在構造函數體運行之前初始化實例變量class Rect{int height;int width;Rect():height=2,width=10{print("${this.height}---${this.width}");}getArea(){return this.height*this.width;} 
}void main(){Rect r=new Rect();print(r.getArea()); }

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/194778.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息