Created
May 2, 2023 12:50
-
-
Save beisong7/66b362825d3cdfb87a9d35ce2ff14329 to your computer and use it in GitHub Desktop.
class dart code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A person. The implicit interface contains greet(). | |
class Person { | |
// In the interface, but visible only in this library. | |
final String _name; | |
// Not in the interface, since this is a constructor. | |
Person(this._name); | |
// In the interface. | |
String greet(String who) => 'Hello, $who. I am $_name.'; | |
} | |
// An implementation of the Person interface. | |
class Impostor implements Person { | |
String get _name => ''; | |
String greet(String who) => 'Hi $who. Do you know who I am?'; | |
} | |
//function that returns a string | |
String greetBob(Person person) => person.greet('Bob'); | |
class Car { | |
final String _name; | |
Car(this._name); | |
String horn() => 'Hoooorn! $_name is passing'; | |
} | |
String greet(Person person, String secondPerson) => person.greet(secondPerson); | |
void main() { | |
print(greetBob(Person('Kathy'))); | |
// print(Person('James Bond').greet('007')); | |
print(greetBob(Impostor())); | |
// print(greetBob(Car('Toyota'))); | |
var human = new Person('Udeme'); | |
// print(human.greet('Ben')); | |
print(greet(human, 'Katty')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment