I wrote a class in Python3 and Java

I wrote a class in Python3 and Java

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.

Explanation of what you are doing

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.

First from Java

Phone class

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);
    }
}

Smart Phone class

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);
    }

}

Main class

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();

    }

}

Java execution result

===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

Then the Python code

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")

Python execution result

===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.

Impressions

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 ~!

The site that I used as a reference

9. Classes — Python 3.6.1 Documentation

Python Basic Course (13 classes) --Qiita

Recommended Posts

I wrote a class in Python3 and Java
I created a class in Python and tried duck typing
A memo that I wrote a quicksort in Python
I wrote python in Japanese
I compared Java and Python!
I wrote Fizz Buzz in Python
I wrote the queue in Python
I wrote the stack in Python
I tried programming the chi-square test in Python and Java.
Overlapping regular expressions in Python and Java
I made a payroll program in Python!
Differences in syntax between Python and Java
Generate a class from a string in Python
I created a password tool in Python.
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
I was addicted to confusing class variables and instance variables in Python
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I thought a Python class variable was an instance variable and died
I wrote python3.4 in .envrc with direnv and allowed it, but I got a syntax error
Organize python modules and packages in a mess
I wrote a code to convert quaternions to z-y-x Euler angles in Python
case class in python
I want to create a window in Python
I tried playing a typing game in Python
[python] Difference between variables and self. Variables in class
I wrote "Introduction to Effect Verification" in Python
Why I'm a Java shop and start Python
[Memo] I tried a pivot table in Python
I wrote a design pattern in kotlin Prototype
[Python] I forcibly wrote a short Perlin noise generation function in Numpy.
I tried adding a Python3 module in C
I wrote a Japanese parser in Japanese using pyparsing.
I wrote FizzBuzz in python using a support vector machine (library LIVSVM).
Class notation in Python
I made a Caesar cryptographic program in Python.
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
Note that I understand the least squares algorithm. And I wrote it in Python.
I want to easily implement a timeout in python
I made a prime number generation program in Python
I wrote a design pattern in kotlin Factory edition
I wrote a design pattern in kotlin Builder edition
I want to write in Python! (2) Let's write a test
I wrote a design pattern in kotlin Singleton edition
I wrote a design pattern in kotlin Adapter edition
I tried to implement a pseudo pachislot in Python
I wrote a design pattern in kotlin, Iterator edition
I want to randomly sample a file in Python
I want to work with a robot in python.
I made a LINE BOT with Python and Heroku
I made a prime number generation program in Python 2
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
I wrote a design pattern in kotlin Template edition
Python: I tried a liar and an honest tribe
Sample of getting module name and class name in Python
I implemented a Vim-like replacement command in Slackbot #Python
Python: Class and instance variables
Create a function in Python
Create a dictionary in Python