Skip to content

Instantly share code, notes, and snippets.

@olaide-ekolere
Last active November 28, 2020 08:56
Show Gist options
  • Save olaide-ekolere/e83dfee6395476af4fe92a2bf1c65285 to your computer and use it in GitHub Desktop.
Save olaide-ekolere/e83dfee6395476af4fe92a2bf1c65285 to your computer and use it in GitHub Desktop.
Inheritance (Mixin) - Introduction to Dart
import "package:flutter/material.dart";
void main() {
//new instance of class
MyNewClass myNewClass = new MyNewClass();
var anotherInstance = new MyNewClass();
print(myNewClass.runtimeType);
anotherInstance.sayHello();
Shape rectangle1 = new Rectangle(20, 30);
print(rectangle1.runtimeType);
print(rectangle1.perimeter());
rectangle1.showPerimeter();
Shape square1 = new Square(35);
print(square1.runtimeType);
print(square1.perimeter());
square1.showPerimeter();
Rectangle(20, 20).showPerimeter();
Square(20).showPerimeter();
Circle circle1 = Circle(radius: 66);
circle1.showPerimeter();
print(circle1.diameter);
circle1.showDiameter();
//Shape shape = new Shape(1, 2);
Square square2 = Square(10);
square2.showSides();
Rectangle rectangle2 = new Rectangle(10, 25);
rectangle2.showSides();
showQuadSides(square2);
showQuadSides(square1 as Square);
showShapePerimeter(square2);
showCircleDiameter(circle1);
}
class MyNewClass {
void sayHello() {
print("hello");
}
}
class Rectangle extends Shape with Quadrilateral {
Rectangle(int length, int breadth) : super(length, breadth) {
sides = 2;
}
void showPerimeter() {
print(
"The perimeter of rectangle with length $length and breadth $breadth is ${perimeter()}");
}
}
class Square extends Shape with Quadrilateral {
Square(int size) : super(size, size);
void showPerimeter() {
print("The perimeter of square with size $length is ${perimeter()}");
}
}
class Circle extends Shape {
Circle({@required int radius}) : super(radius, radius);
@override
int perimeter() {
return (2 * length * 3.14).toInt();
}
int get diameter {
return length * 2;
}
void showDiameter() {
print("the diameter of the circle is $diameter");
}
void showPerimeter() {
print("The perimeter of circle with radius $length is ${perimeter()}");
}
}
abstract class Shape {
int length;
int breadth;
Shape(this.length, this.breadth);
int perimeter() {
return 2 * (length + breadth);
}
void showPerimeter();
//add area
}
mixin Quadrilateral {
int sides = 4;
void showSides() {
print(
"This Quadrilateral is a ${this.runtimeType} with $sides equal sides ");
}
}
void showQuadSides(Quadrilateral quad) {
quad.showSides();
}
void showShapePerimeter(Shape shape) {
shape.showPerimeter();
}
void showCircleDiameter(Circle circle) {
circle.showDiameter();
}
//add 3DShape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment