---
# System prepended metadata

title: オブジェクト指向
tags: [Algorithm]

---

###### tags: `Algorithm`

# オブジェクト指向
変数やメソッドを定義したclassのことをオブジェクトと呼ぶ．

## 例
### 分数の計算
```python=
def gcd(a,b):
    if b!=0:
        return gcd(b,a%b)
    else:
        return a

class Rational:
    def __init__(self,num,den):
        self.num = num
        self.den = den
    ##### 特殊メソッド
    def __str__(self):
        M = gcd(self.num,self.den)
        #print(M)
        return "{}/{}".format(self.num//M,self.den//M)
    
    def __float__(self):
        return float(self.num)/float(self.den)
    
    def __mul__(self,other):
        return Rational(self.num*other.num,self.den*other.den)
    
    def __add__(self,other):
        ans_num = self.num*other.den + other.num*self.den
        ans_den = self.den*other.den
        return Rational(ans_num,ans_den)
    
    def __sub__(self,other):
        ans_num = self.num*other.den - other.num*self.den
        ans_den = self.den*other.den
        return Rational(ans_num,ans_den)
    
    def __floordiv__(self,other):
        return Rational(self.num*other.den,self.den*other.num)
    
    def __truediv__(self,other):
        return float(Rational(self.num*other.den,self.den*other.num))
    
    def show(self):
        print("{}/{}".format(self.num,self.den))

#def mul(self):
        
#Rational(5,3).show()
print (Rational(8,20))

## 25/55, 22/55
print (Rational(5,11)*Rational(8,20))

print (Rational(5,11)+Rational(8,20))

print (Rational(5,11)-Rational(8,20))

print (Rational(5,11)//Rational(8,20))

print (Rational(5,11)/Rational(8,20))

print (type(Rational(5,11))==Rational)
```
#### 出力
```
2/5			# 8/20
2/11			# *
47/55			# +
3/55			# -
25/22			# //
1.1363636363636365	# /
True			#Type of Rational(5,11) is Rational
```



### 複素数の計算
```python=
class Complex:
    def __init__(self,re,im):
        self.re = re
        self.im = im
        self.real = re
        self.imaginary = im
    def __str__(self):
        if self.im>=0:
            return "{}+{}j".format(self.re,self.im)
        else:
            return "{}-{}j".format(self.re,-self.im)
    
    def __abs__(self):
        return self.re**2 + self.im**2
    def __add__(self,other):
        real = self.re+other.re
        imaginary = self.im+other.im
        return Complex(real, imaginary)
    def __sub__(self,other):
        real = self.re-other.re
        imaginary = self.im-other.im
        return Complex(real, imaginary)
    def __mul__(self,other):
        real = self.re*other.re-self.im*other.im
        imaginary = self.re*other.im+self.im*other.re
        return Complex(real, imaginary)
    def __truediv__(self,other):
        con = Complex(other.re, -other.im)
        div = other.re**2 + other.im**2
        real = (con.re*self.re-con.im*self.im)/div
        imaginary = (con.re*self.im+con.im*self.re)/div
        print(Complex(real, imaginary))
    def conjugate(self):
        return Complex(self.re, -self.im)
```


## 疑問
違う型同士の計算をどう定義するか？
ex 5/3+1
ex 3+4i+5

## メモ
やること
「継承」、「カブセル化」、「ポリモーフィズム」


https://qiita.com/kaitolucifer/items/926ed9bc08426ad8e835
