Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
type Person struct {
Name string
}
func (p *Person) Intro() string {
return p.Name
}
type Woman struct {
Person
}
func (w *Woman) Intro() string {
return "Mrs. " + w.Person.Intro()
}
type A struct {
x int
}
func (self *A) F1() () {
fmt.Println("I'm A: ", self.x)
return
}
type B struct {
y int
}
func (self *B) F2() () {
fmt.Println("I'm B: ", self.y)
return
}
type C struct {
A
B
z int
}
func (self *C) F3() () {
fmt.Println("I'm C: ", self.x, " and can call A and B: ")
self.F1()
self.F2()
return
}
class A {
public:
int x;
void F1() {
std::cout << "I'm A: " << x << std::endl;
}
};
class B {
public:
int y;
void F2() {
std::cout << "I'm B: " << y << std::endl;
}
};
class C : public A, public B {
public:
int z;
void F3() {
std::cout << "I'm C: " << x << " and can call A and B" << std::endl;
F1();
F2();
}
};
Является ли Go объектно-ориентированным языком?
Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).
Also, the lack of type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.
data Animal = Animal {
speak :: Int -> String,
move :: Int -> String,
act :: Int -> String
}
base self = Animal {
speak = \repeats -> "",
move = \repeats -> "",
act = \repeats -> (self `move` repeats) ++ (self `speak` repeats)
}
{- One type of animal -}
bird self = let super = base self in Animal {
speak = \repeats -> concat $ replicate repeats "cheep ",
move = \repeats -> concat $ replicate repeats "flap ",
act = (super `act`)
}
{- Another type of animal -}
dog self = let super = base self in Animal {
speak = \repeats -> concat $ replicate repeats "bark ",
move = \repeats -> concat $ replicate repeats "run ",
act = (super `act`)
}
new f = f (new f)
b = new bird
d = new dog
*Main> b `act` 2
"flap flap cheep cheep "
в Go нет наследования, но есть агрегация.
package main
import (
"fmt"
)
// квадрат
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
// круг
type Circle struct {
radius float32
}
func (c Circle) Area() float32 {
return 3.14159 * c.radius * c.radius
}
func main() {
var areaIntf Shaper // объект интерфейса
// создаем объект Квадрат
sq1 := new(Square)
sq1.side = 5
// присваиваем объект интерфейсу и вызываем полиморфно функцию через интерфейс
areaIntf = sq1
fmt.Printf("The square has area: %f\n", areaIntf.Area())
// создаем объект Круг
cr1 := new(Circle)
cr1.radius = 5
// присваиваем объект интерфейсу и вызываем полиморфно функцию через интерфейс
areaIntf = cr1
fmt.Printf("The circle has area: %f\n", areaIntf.Area())
}
Объектно-дезориентированный язык