I've only been doing static languages, but I wanted to do machine learning, so I decided to start studying Python. I'm shocked by the culture because there are many differences from Go, Java, and TypeScript that I've been doing so far ... That said, learning a new language is exciting.
For now, I've written the same class and its child classes in both languages so that I can compare Java and Python classes so I don't forget the Python class syntax.
Both create a Phone class. Next, create its child class, the SmartPhone class. Instantiate both and call each method. (The calss method is natural but not instantiated)
By the way, I used the phone and smartphone as the theme because I had an iphone at hand when writing the article (laughs).
I wrote down the detailed processing in the code.
Phone.java
package com.company;
/**
*Phone class
*/
public class Phone {
/**
*phone number
*/
private String number;
/**
*constructor
* @param number
*/
public Phone(String number) {
this.number = number;
}
/**
*Show your phone number
*/
public void showMyNumber(){
System.out.println("This phone's number is " + this.number);
}
/**
*Make a call to the specified person
* @param phonePartner
*/
public void call(String phonePartner) {
System.out.println("Call to " + phonePartner);
}
}
Child class of Phone class
SmartPhone.java
package com.company;
/**
*smartphone
*Child class of Phone class
*/
public class SmartPhone extends Phone {
/**
* os
*/
private String os;
/**
*constructor
*
* @param number phone number
* @param os os
*/
public SmartPhone(String number, String os) {
super(number);
this.os = os;
}
/**
*Make a video call to the specified person
*Overridden
*
* @param phonePartner
*/
@Override
public void call(String phonePartner) {
System.out.println("Video call to" + phonePartner);
}
/**
*Show OS
*/
public void showMyOS() {
System.out.println("his phone's os is" + this.os);
}
/**
*Class method
*Take a photo
*
* @param subject subject
*/
public static void takeAPitcure(String subject) {
System.out.println("Take a picture of " + subject);
}
}
Here, each class is instantiated.
Main.java
package com.company;
/**
*Main
*/
public class Main {
public static void main (String[] args) {
System.out.println("===Normal Phone===");
//Instantiation
Phone normalPhone = new Phone("xxx-xxx-xxx");
normalPhone.showMyNumber();
normalPhone.call("sekky0905");
System.out.println("===Smart Phone===");
//Instantiation
SmartPhone iPhone = new SmartPhone("○○○-○○○-○○○", "ios");
iPhone.showMyNumber();
iPhone.call("sekky0905");
iPhone.showMyOS();
SmartPhone.takeAPitcure("flower");
System.out.println("===Smart Phone2===");
//Instantiation
Phone zenPhone = new SmartPhone("△△△-△△△-△△△", "android");
zenPhone.showMyNumber();
zenPhone.call("sekky0905");
//By the way, of course you can't do anything
// zenPhone.showMyOS();
}
}
===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
main.py
#class
class Phone:
#constructor
def __init__(self, number):
self.__number = number
#Python methods always seem to have one argument
#It seems customary to always set the first argument to self
def show_my_number(self):
print('This phone\'s number is{0}.'.format(self.__number))
#Python methods always seem to have one argument
#It seems customary to always set the first argument to self
def call(self, phone_partner):
print('Call to {0}.'.format(phone_partner))
#Phone child class
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))
#Class method
@classmethod
def take_a_picture(cls, subject):
print('Take a picture of {0}.'.format(subject))
print("===Normal Phone===")
#Instantiation
normalPhone = Phone("xxx-xxx-xxx")
normalPhone.show_my_number()
normalPhone.call("sekky0905")
print("===Smart Phone===")
#Instantiation
iPhone = SmartPhone("○○○-○○○-○○○", "ios")
iPhone.show_my_number()
#Can be used because it inherits Phone
iPhone.call("sekky0905")
iPhone.show_my_os()
#Class method
SmartPhone.take_a_picture("flower")
===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.
I was reluctant to write Python at first, but as I got used to it, I thought it might be easier to write. But writing a static language after touching Python gives me a sense of security when I'm back home.
Learning a new language is fun ~!
9. Classes — Python 3.6.1 Documentation
Python Basic Course (13 classes) --Qiita
Recommended Posts