Skip to content

Instantly share code, notes, and snippets.

@sekky0905
Last active July 25, 2017 14:38
Show Gist options
  • Save sekky0905/5bd176ee74c429d7eaf55f0fd9b90ec9 to your computer and use it in GitHub Desktop.
Save sekky0905/5bd176ee74c429d7eaf55f0fd9b90ec9 to your computer and use it in GitHub Desktop.
Python3とJavaでクラスを書いてみた ref: http://qiita.com/Sekky0905/items/5b4105a9dcb6e0ff354d
===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
===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.
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();
}
}
# クラス
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")
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);
}
}
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