Data Structure Style
type Square struct {
length float
}
type Circle struct {
length float
}
func Area(g GeometricObject) float {
switch (g.type()):
case Circle:
return
case Square:
return ...
}
func Circumference(g GeometricObject) float {
...
}
length float
}
type Circle struct {
length float
}
func Area(g GeometricObject) float {
switch (g.type()):
case Circle:
return
case Square:
return ...
}
func Circumference(g GeometricObject) float {
...
}
Object-Oriented Style
class Square {
length float
constructor(length float) {
this.length = length
}
func Area() float {
return this.length * this.length
}
func Circumference() float {
return 4 * this.length
}
}
class Circle {
radius float
constructor(radius float) {
this.radius = radius
}
func Area() float {
return PI * this.radius * this.radius
}
func Circumference() float {
return 2 * PI * this.radius
}
}
length float
constructor(length float) {
this.length = length
}
func Area() float {
return this.length * this.length
}
func Circumference() float {
return 4 * this.length
}
}
class Circle {
radius float
constructor(radius float) {
this.radius = radius
}
func Area() float {
return PI * this.radius * this.radius
}
func Circumference() float {
return 2 * PI * this.radius
}
}