Last active
July 25, 2017 14:25
-
-
Save sekky0905/893983d530623ed6bb6c5a995e2247fb to your computer and use it in GitHub Desktop.
PythonとJavaでクラスを書いてみた ref: http://qiita.com/Sekky0905/items/00961c7db2a5f0c453f9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
===Normal Phone=== | |
This phone's number is xxx-xxx-xxx | |
Call to sekky0905 | |
===Smart Phone=== | |
This phone's number is ○○○-○○○-○○○ | |
Video call tosekky0905 | |
his phone's os isios | |
Take a picture of flower | |
===Smart Phone2=== | |
This phone's number is △△△-△△△-△△△ | |
Video call tosekky0905 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
===Normal Phone=== | |
This phone's number isxxx-xxx-xxx. | |
Call to sekky0905. | |
===Smart Phone=== | |
This phone's number is○○○-○○○-○○○. | |
Video call to sekky0905. | |
This phone's os is ios. | |
Take a picture of flower. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
/** | |
* メイン | |
*/ | |
public class Main { | |
public static void main (String[] args) { | |
System.out.println("===Normal Phone==="); | |
// インスタンス化 | |
Phone normalPhone = new Phone("xxx-xxx-xxx"); | |
normalPhone.showMyNumber(); | |
normalPhone.call("sekky0905"); | |
System.out.println("===Smart Phone==="); | |
// インスタンス化 | |
SmartPhone iPhone = new SmartPhone("○○○-○○○-○○○", "ios"); | |
iPhone.showMyNumber(); | |
iPhone.call("sekky0905"); | |
iPhone.showMyOS(); | |
SmartPhone.takeAPitcure("flower"); | |
System.out.println("===Smart Phone2==="); | |
// インスタンス化 | |
Phone zenPhone = new SmartPhone("△△△-△△△-△△△", "android"); | |
zenPhone.showMyNumber(); | |
zenPhone.call("sekky0905"); | |
// ちなみに当然だけどこれはできない | |
// zenPhone.showMyOS(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# クラス | |
class Phone: | |
# コンストラクタ | |
def __init__(self, number): | |
self.__number = number | |
# Pythonのメソッドは必ず1つの引数を持つらしい | |
# 第一引数は、必ずselfにするのが慣習らしい | |
def show_my_number(self): | |
print('This phone\'s number is{0}.'.format(self.__number)) | |
# Pythonのメソッドは必ず1つの引数を持つらしい | |
# 第一引数は、必ずselfにするのが慣習らしい | |
def call(self, phone_partner): | |
print('Call to {0}.'.format(phone_partner)) | |
# Phoneの子クラス | |
class SmartPhone(Phone): | |
def __init__(self, number, os): | |
super().__init__(number) | |
self.os = os | |
# Overrideした | |
def call(self, phone_partner): | |
print('Video call to {0}.'.format(phone_partner)) | |
def show_my_os(self): | |
print('This phone\'s os is {0}.'.format(self.os)) | |
# クラスメソッド | |
@classmethod | |
def take_a_picture(cls, subject): | |
print('Take a picture of {0}.'.format(subject)) | |
print("===Normal Phone===") | |
# インスタンス化 | |
normalPhone = Phone("xxx-xxx-xxx") | |
normalPhone.show_my_number() | |
normalPhone.call("sekky0905") | |
print("===Smart Phone===") | |
# インスタンス化 | |
iPhone = SmartPhone("○○○-○○○-○○○", "ios") | |
iPhone.show_my_number() | |
# Phoneを継承しているから使える | |
iPhone.call("sekky0905") | |
iPhone.show_my_os() | |
# クラスメソッド | |
SmartPhone.take_a_picture("flower") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
/** | |
* 電話クラス | |
*/ | |
public class Phone { | |
/** | |
* 電話番号 | |
*/ | |
private String number; | |
/** | |
* コンストラクタ | |
* @param number | |
*/ | |
public Phone(String number) { | |
this.number = number; | |
} | |
/** | |
* 自分の電話番号を表示する | |
*/ | |
public void showMyNumber(){ | |
System.out.println("This phone's number is " + this.number); | |
} | |
/** | |
* 指定された相手に電話をかける | |
* @param phonePartner 電話の相手 | |
*/ | |
public void call(String phonePartner) { | |
System.out.println("Call to " + phonePartner); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
/** | |
* スマホ | |
* Phone classの子クラス | |
*/ | |
public class SmartPhone extends Phone { | |
/** | |
* os | |
*/ | |
private String os; | |
/** | |
* コンストラクタ | |
* | |
* @param number 電話番号 | |
* @param os os | |
*/ | |
public SmartPhone(String number, String os) { | |
super(number); | |
this.os = os; | |
} | |
/** | |
* 指定された相手にTV電話をかける | |
* オーバーライドした | |
* | |
* @param phonePartner 電話の相手 | |
*/ | |
@Override | |
public void call(String phonePartner) { | |
System.out.println("Video call to" + phonePartner); | |
} | |
/** | |
* OSを表示する | |
*/ | |
public void showMyOS() { | |
System.out.println("his phone's os is" + this.os); | |
} | |
/** | |
* クラスメソッド | |
* 写真を取る | |
* | |
* @param subject 被写体 | |
*/ | |
public static void takeAPitcure(String subject) { | |
System.out.println("Take a picture of " + subject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment