Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehRini Cahya Telah diubah "9 tahun yang lalu
1
Lecture 2 Object-oriented Programming Concepts Erick Pranata © Sekolah Tinggi Teknik Surabaya 1
2
» Object » Class » Inheritance » Technical Details 2 © Sekolah Tinggi Teknik Surabaya
3
3
4
4
5
5
6
» Modularity » Information-hiding » Code re-use » Pluggability and debugging ease 6 © Sekolah Tinggi Teknik Surabaya
7
7
8
A class is a blueprint or prototype from which objects are created 8 © Sekolah Tinggi Teknik Surabaya
9
class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } 9 © Sekolah Tinggi Teknik Surabaya
10
void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } 10 © Sekolah Tinggi Teknik Surabaya
11
class BicycleDemo { public static void main(String[] args) { Bicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle(); bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates(); bike2.changeCadence(50); bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.speedUp(10); bike2.changeGear(3); bike2.printStates(); } 11 © Sekolah Tinggi Teknik Surabaya
12
12
13
13 © Sekolah Tinggi Teknik Surabaya
14
class MountainBike extends Bicycle { // new fields and methods defining // a mountain bike would go here } 14 © Sekolah Tinggi Teknik Surabaya
15
15
16
class MyClass extends MySuperClass implements YourInterface { // field, constructor, and // method declarations } 16 © Sekolah Tinggi Teknik Surabaya
17
» Class, Field, Constructor, Method 17 © Sekolah Tinggi Teknik Surabaya ModifierClassPackageSubclassWorld publicYYYY protectedYYYN no modifierYYNN privateYNNN
18
public class DataArtist {... public void draw(String s) {... } public void draw(int i) {... } public void draw(double f) {... } public void draw(int i, double f) {... } 18 © Sekolah Tinggi Teknik Surabaya
19
» Every class can have more than one constructor (overloading constructor) public Bicycle( int startCadence, int startSpeed, int startGear ) { gear = startGear; cadence = startCadence; speed = startSpeed; } public Bicycle() { gear = 1; cadence = 10; speed = 0; } 19 © Sekolah Tinggi Teknik Surabaya
20
public Polygon polygonFrom(Point... corners){ int numberOfSides = corners.length; double squareOfSide1, lengthOfSide1; squareOfSide1 = (corners[1].x -corners[0].x) * (corners[1].x - corners[0].x) + (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y); lengthOfSide1 = Math.sqrt(squareOfSide1); // more method body code follows that // creates and returns a polygon // connecting the Points } 20 © Sekolah Tinggi Teknik Surabaya
21
Point originOne = new Point(23, 94); 21 © Sekolah Tinggi Teknik Surabaya
22
Rectangle rectOne = new Rectangle(originOne, 100, 200); 22 © Sekolah Tinggi Teknik Surabaya
23
» Methods can return value ˃Primitive Data Type ˃Object Point getPoint() { return new Point(5,8); } 23 © Sekolah Tinggi Teknik Surabaya
24
public class Bicycle { private int cadence; private int gear; private int speed; // add an instance variable for the object ID private int id; // add a class variable for the // number of Bicycle objects instantiated private static int numberOfBicycles = 0; } » Can also be “class method” 24 © Sekolah Tinggi Teknik Surabaya
25
» Object-oriented Programming Concepts, http://docs.oracle.com/javase/tutorial/j ava/concepts/object.html http://docs.oracle.com/javase/tutorial/j ava/concepts/object.html 25 © Sekolah Tinggi Teknik Surabaya
26
26
27
» Create Class Mahasiswa » Instantiate Mahasiswa » Manipulate beberapa object Mahasiswa (Reference Concept) » Asisten, inherit Mahasiswa 27 © Sekolah Tinggi Teknik Surabaya
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.