Last active
November 28, 2020 08:40
-
-
Save olaide-ekolere/e93876bf4b59bcefa1855bffe67ca9ca to your computer and use it in GitHub Desktop.
Classes - Object oriented programming
This file contains 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
void main() { | |
//new instance of class | |
MyNewClass myNewClass = new MyNewClass(); | |
var anotherInstance = new MyNewClass(); | |
print(myNewClass.runtimeType); | |
anotherInstance.sayHello(); | |
Rectangle rectangle1 = new Rectangle(20, 30); | |
print(rectangle1.runtimeType); | |
print(rectangle1.perimeter()); | |
rectangle1.showPerimeter(); | |
rectangle1.length = 43; | |
rectangle1.showPerimeter(); | |
rectangle1.breadth = 50; | |
rectangle1.showPerimeter(); | |
var square1 = new Square(35); | |
print(square1.runtimeType); | |
print(square1.perimeter()); | |
square1.showPerimeter(); | |
Rectangle(20, 20).showPerimeter(); | |
Square(20).showPerimeter(); | |
// Circle(radius: 66).showPerimeter(); | |
} | |
class MyNewClass { | |
void sayHello() { | |
print("hello"); | |
} | |
} | |
class Rectangle { | |
int length; | |
int breadth; | |
Rectangle(this.length, this.breadth); | |
int perimeter() { | |
return 2 * (length + breadth); | |
} | |
void showPerimeter() { | |
print("Perimeter is ${perimeter()}"); | |
} | |
} | |
class Square{ | |
int length; | |
Square(this.length); | |
int perimeter() { | |
return 4 * length; | |
} | |
void showPerimeter() { | |
print("Perimeter is ${perimeter()}"); | |
} | |
} | |
//Inheritance | |
/* | |
class Square extends Rectangle{ | |
Square(int size) : super(size, size); | |
} | |
class Circle extends Rectangle{ | |
Circle({@required int radius}) : super(radius, radius); | |
@override int perimeter() { | |
return (2 * length * 3.14).toInt(); | |
} | |
} | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment