[This book](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%E3 % 81% AE% E8% 80% 83% E3% 81% 88% E6% 96% B9% E3% 81% 8C% E3% 81% 8A% E3% 82% 82% E3% 81% 97% E3% 82 % 8D% E3% 81% 84% E3% 81% BB% E3% 81% A9% E8% BA% AB% E3% 81% AB% E3% 81% A4% E3% 81% 8F% E6% 9C% AC -% E5% 95% 8F% E9% A1% 8C% E8% A7% A3% E6% B1% BA% E8% 83% BD% E5% 8A% 9B% E3% 82% 92% E9% 8D% 9B% E3% 81% 88% E3% 82% 88% E3% 81% 86% EF% BC% 81-% E3% 82% A2% E3% 82% B9% E3% 82% AD% E3% 83% BC% E6 % 9B% B8% E7% B1% 8D-% EF% BC% B6% EF% BC% 8E% EF% BC% A1% EF% BD% 8E% EF% BD% 94% EF% BD% 8F% EF% BD% 8E-% EF% BC% B3% EF% BD% 90% EF% BD% 92% EF% BD% 81% EF% BD% 95% EF% BD% 8C-ebook / dp / B00MB2STXK? Ie = UTF8 & btkr = 1 & ref_ = dp-kindle-redirect) How can I write in python? I'm studying the idea of programming.
First, there was a "class declaration" as an issue, so I rewrote it to python.
test.cpp
class sample {
public:
sample();
sample(int num);
int doesSomething(double param);
private:
int intData;
};
When thinking about this, I had never thought about it separately for "public" and "private", so I refer to the reference site and change it below.
test501.py
#!/usr/bin/env python
#coding:utf-8
class Sample(object):
#public:
#C++Sample()
def sample1(self):
print("sample")
#C++Sample(int num)
def sample2(self,r):
return int(r)
#C++Int does Something(double param)
def doesSomething(self,param):
return float(param)
##Corrected below
#private:
__intData = 5 ###The call is_name of the class__Variable name
>>>(Terminal execution)
>>> from test501 import Sample
>>> w = Sample()
>>> w.sample1()
sample
>>> w.sample2(1)
1
>>> w.doesSomething(1.5)
1.5
>>> w._Sample__intData
5
At first, in the c ++ code, sample () and sample (1) were written with the same method name, so if you prepared the same one in python, an error would occur and you can move it by changing the name, so be careful about how to use the method name. I noticed the point to do (´Д`) Themes such as encapsulation will continue, but I would like to continue studying what happens to python class design.
Recommended Posts