Movatterモバイル変換


[0]ホーム

URL:


Loading video player…

Sample Classes in Python vs Java

00:00Welcome to lesson one of Object-Oriented Programming in Python versus Java.In this lesson,we will look at a simple class written in both Java and in Python.

00:12The Java class is going to be in a file calledCar.javaand it will have the typical organization of aJava class with its fields, constructor, and some get methods.

00:26So, here we have a classCar. We define three fields—the color, model, and its year.We have a constructor assigning values to each of those fieldsand some typical get methods to obtain the values of those fields.

00:47Python classes, you’ll notice, are going to be a lot smaller.There’s no requirement that the filename match the class namenor that there be only one class in a particular Python file.

01:00All of our attributes are defined in what’s called an.__init__() method.

01:07So, comparing our Java class to our Python class,we define classCarand then we have our.__init__() method,which is similar to a constructor. Behind the scenesthey’re not really the same,but if you want to view writing a Java constructor,it would go into this dunder method—a method that begins and ends with two underscores (__) is referred to as a dundermethod—called.__init__().

01:39We don’t have to declare the fields ahead of time.The fields are defined when we assign them a value.So when we sayself.color = color,that defines the field.color.

01:53Similarly, this statement defines the field.model and this statementdefines the field.year. In the remainder ofthis course, we will dig deeper into these differences—why it’s so much smaller, why we’re missing things likepublic andprivatebut the first thing we’re going to be taking a look at will be the declarationand initialization of fields.

02:21That will be in your next lesson.

Become a Member to join the conversation.

Course Contents

Overview
12%

[8]ページ先頭

©2009-2026 Movatter.jp