Creating a Class and Accessing its Fields
Example 1: Simple Example
// Creating Class named Gfg
class Gfg {
// Creating Field inside the class
String geek1 = '';
// Creating Function inside class
void geek()
{
print("Welcome to $geek1");
}
}
void main()
{
// Creating Instance of class
Gfg geek = new Gfg();
// Calling field name geek1 and assigning value
// to it using object of the class Gfg
geek.geek1 = 'w3wiki';
// Calling function name geek using object of the class Gfg
geek.geek();
}
Output:
Welcome to w3wiki
Explanation of the above Program:
Here we have first created the class named Gfg with a field geek1 and a function geek. Now in the main function, we have created an object of the class Gfg of name geek. Using this object we have assigned the value ‘w3wiki‘ to the string variable of the class and then we have called the geek function which has printed the output.
Example 2 :
class GFG {
String name = " ";
String get gfg1 {
return name;
}
void set gfg1(String name) {
this.name = name;
}
void result(){
print(name);
}
}
void main() {
GFG gfg2 = new GFG();
gfg2.name="Geeks for geeks employee no 1 : avinash ,aakanksha";
gfg2.result();
}
Output:
Geeks for geeks employee no 1 : avinash ,aakanksha
Dart – Classes And Objects
Dart is an object-oriented programming language, so it supports the concept of class, object … etc. In Dart, we can define classes and objects of our own. We use the class keyword to do so. Dart supports object-oriented programming features like classes and interfaces.
Let us learn about Dart Classes and Objects in this article.