OOP – Basic concepts & Java Code

Class, Object, Field, & Value

Objects(red circle with 5 diameter) are created from classes(circle class). The class describes the kind of object; the objects represent individual instantiations of the class. When you are inspecting different objects, you will notice that objects of the same  class are all having the same fields(diameter, xPosition, yPosition, etc) while the actual value of each field in each object may be different.

Methods & Parameters

We can communicate with objects by invoking methods on them. Objects usually do something if we invoke a method. (e.g moveRight(2) is a method) Methods can have parameters to provide additional information for a task. (e.g what inside the parenthesis is parameter, which is 2 in method moveRight(2))

Data Types

Parameters have types. The type defines what kinds of values a parameter can take.

1.5 Enter an unknown color.

屏幕快照 2016-12-09 上午11.03.29.png%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-09-%e4%b8%8a%e5%8d%8810-55-45

The system will output black color when an unknown color is inputed.

1.6 Enter a parameter without quote.

屏幕快照 2016-12-09 上午10.57.43.png

Error: cannot find symbol – variable green.” This kind of error is called Pitfall which is a common error of forgetting the double quotes when typing in a data value of type String.

Multiple instances

1.7 Change positions, sizes, and colors.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-09-%e4%b8%8a%e5%8d%8810-51-56

So interesting, isn’t it?

Multiple instances refer to the fact that many similar objects can be created from a single class. (e.g The green big circle and the yellow small circle are multiple instances of circle class.)

State

1.8 Inspect the object

屏幕快照 2016-12-09 上午11.09.37.png

1.9 Different ways to draw this

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-09-%e4%b8%8a%e5%8d%8810-51-56%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-09-%e4%b8%8a%e5%8d%8810-52-07

There are no orders of changing different shapes so the order of drawing varies. For the position these four people. I used moveLeft() and moveRight() methods, however, I can also use slowMoveHorizontal(int distance) methods.

Java Code

屏幕快照 2016-12-09 上午11.30.08.png

屏幕快照 2016-12-09 上午11.29.36.png

Show Terminal

To call a method on an object, we write the name of the object, followed by a dot, followed by the name of the method. The sentence ends with a parameter list (write an empty pair of parentheses if there are no parameters).

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%883-36-53

屏幕快照 2016-12-10 下午3.36.28.png

Show Code Pad

Object Interaction

此幻灯片需要JavaScript支持。

The Picture  class is written so that, when you create an instance, the instance creates two square objects, a triangle, and a circle; moves them around; and changes their color and size, until the canvas looks like the picture we see above.

Objects can create other objects, and they can call each other’s methods. In a normal Java program, you may well have hundreds or thousands of objects. The user of a program just starts the program (which typically creates a first object), and all other objects are created—directly or indirectly—by that object. There is a concept called Method calling which means objects can communicate by calling each other’s methods.

Source Code

Source code of a class determines the fields and methods of each of the objects within that class.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-20-28

此幻灯片需要JavaScript支持。

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-31-30%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-31-03   %e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-31-20%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-30-46

屏幕快照 2016-12-10 下午4.35.43.png

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-35-38

屏幕快照 2016-12-10 下午4.36.48.png

屏幕快照 2016-12-10 下午4.38.42.png

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-39-33

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-51-08

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-10-%e4%b8%8b%e5%8d%884-51-15

屏幕快照 2016-12-10 下午4.51.00.png

屏幕快照 2016-12-20 下午4.07.01.png

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-00-34%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-06-03

Key Concepts

屏幕快照 2016-12-20 下午4.06.25.png屏幕快照 2016-12-20 下午4.06.37.png

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-16-38

private String name;

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-16-45

public void setSend(String newSend){

send=newSend;

}

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-16-51

public int getAverage(){

return int average1;

return int average2;

}

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-16-59

Book is a class. Its subject might be subject, pages, author, etc.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-20-%e4%b8%8b%e5%8d%884-17-05

No. A class can have several different objects.

Works Cited

Barnes, David. “OBJECTS FIRST WITH JAVA A PRACTICAL INTRODUCTION USING BLUEJ (5TH EDITION).”Objects first with Java. Object first with Java, 16 December 2016.

留下评论