언어별 문법 정리
2019년 8월 29일 초안 작성
C++, Java, Python, Go, TypeScript 순서로 정리한다.
Loop
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
# Case #1
sum = 0
for i in range(1, 10 + 1):
sum += i
# Case #2
sum = sum(i for i in range(1, 10 + 1))
var sum int = 0
for i := 1; i <= 10; i++ {
sum += i
}
let sum: number = 0;
for (let i = 1; i <= 10; i++) {
sum += i;
}
Templates, Generics
template<class T, class U>
bool are_equal(T a, U b) {
return (a == b);
}
are_equal(10, 10.0)
public static<T, U> boolean are_equal(T a, U b) {
return a == b;
}
are_equal(10, 10.0);
# Case #1
def are_equal(a, b):
return a == b
are_equal(10, 10.0)
# Case #2
from typing import TypeVar
T = TypeVar('T')
U = TypeVar('U')
def are_equal(a: T, b: U) -> bool:
return a == b
are_equal(10, 10.0)
// Not Supported. But probably plan to support in Go 2 version.
// <https://blog.golang.org/why-generics>
function are_equal<T, U>(a: T, b: U): boolean {
return +a == +b;
}
are_equal<number, number>(10, 10.0);
Iterate through Array
std::string foo[] = {"A", "B", "C"};
for (std::string f : foo) {
std::cout << f << std::endl;
}
String[] foo = new String[]{"A", "B", "C"};
for (String f : foo) {
System.out.println(f);
}
foo = ['A', 'B', 'C']
for f in foo:
print(f)
var foo = []string{"A", "B", "C"}
for _, f := range foo {
fmt.Println(f)
}
let foo: string[] = ['A', 'B', 'C'];
for (const f of foo) {
console.log(f);
}
Structs
struct Product {
int weight;
double price;
};
Product apple;
apple.price = 10;
class Product {
private int weight;
private double price;
public void setPrice(double price) {
this.price = price;
}
}
Product apple = new Product();
apple.setPrice(10);
from dataclasses import dataclass
# Data Classes module is only work in Python 3.7 and above.
# A backport of the module for Python 3.6 <https://pypi.org/project/dataclasses/>
@dataclass
class Product:
weight: int = None
price: float = None
apple = Product()
apple.price = 10
type Product struct {
weight int
price float64
}
var apple Product
apple.price = 10
interface Product {
weight: number,
price: number,
}
let apple = {} as Product;
apple.price = 10;
Classes
class Rectangle {
int width, height;
public:
Rectangle(int, int);
int area();
};
Rectangle::Rectangle(int x, int y) {
width = x;
height = y;
}
int Rectangle::area() {
return width * height;
}
Rectangle rect(3, 4);
std::cout << rect.area() << std::endl;
class Rectangle {
int width;
int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int area() {
return this.width * this.height;
}
}
Rectangle rect = new Rectangle(3, 4);
System.out.println(rect.area());
from dataclasses import dataclass
@dataclass
class Rectangle:
width: int
height: int
def area(self):
return self.width * self.height
rect = Rectangle(3, 4)
print(rect.area())
type Geometry interface {
area() int
}
type Rectangle struct {
width int
height int
}
func (r *Rectangle) area() int {
return r.width * r.height
}
var rect Geometry = &Rectangle{3, 4}
fmt.Println(rect.area())
class Rectangle {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
area(): number {
return this.width * this.height;
}
}
const rect = new Rectangle(3, 4);
console.log(rect.area());
Inheritance
class Animal {
protected:
std::string name;
public:
Animal(std::string n) {
name = n;
}
void move(int);
};
void Animal::move(int distanceInMeters = 0) {
std::cout << name << " moved " << distanceInMeters << "m." << std::endl;
}
class Horse : Animal {
public:
Horse(std::string n) : Animal(n) {}
void move(int);
};
void Horse::move(int distanceInMeters = 45) {
std::cout << "Galloping..." << std::endl;
Animal::move(distanceInMeters);
}
Horse tom("Tommy");
tom.move(34);
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void move(int distanceInMeters) {
System.out.printf("%s moved %dm.%n", this.name, distanceInMeters);
}
}
public class Horse extends Animal {
public Horse(String name) {
super(name);
}
public void move(int distanceInMeters) {
System.out.println("Galloping...");
super.move(distanceInMeters);
}
}
Animal tom = new Horse("Tommy");
tom.move(34);
from dataclasses import dataclass
@dataclass
class Animal:
name: str
def move(self, distanceInMeters: int = 0):
print('%s moved %sm.' % (self.name, distanceInMeters))
class Horse(Animal):
def move(self, distanceInMeters: int = 45):
print('Galloping...')
super().move(distanceInMeters)
tom: Animal = Horse('Tommy')
tom.move(34)
type Animal struct {
name string
}
func (a *Animal) move(distanceInMeters int) {
fmt.Printf("%s moved %d.\n", a.name, distanceInMeters)
}
type Horse struct {
*Animal
}
func (h *Horse) move(distanceInMeters int) {
fmt.Println("Galloping...")
h.Animal.move(distanceInMeters)
}
var tom *Horse = &Horse{&Animal{name: "Tommy"}}
tom.move(34)
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Horse extends Animal {
constructor(name: string) {
super(name);
}
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
let tom: Animal = new Horse("Tommy");
tom.move(34);