목요일, 3월 28
Shadow

#005 What is Class?

dogs have state (name, color, breed, hungry)
behavior(barking, fetching, wagging tail)

computer have variable(state).
it’s item of data named by an identifier

object implements its behavior with methods.
method is a function

event is a common object used
particular object is called an instance.
object may expose some of its variables or hide some of its methods

Message?
When object A wants object B to perform one of B’s methods, object A sends a message to object B
The next figure shows the three parts of a message:

The object to which the message is addressed (YourBicycle) -call by referense
The name of the method to perform (changeGears)
Any parameters needed by the method (lowerGear)

Messages provide two important benefits:

An object’s behavior is expressed through its methods, so (aside from direct variable access) message passing supports all possible interactions between objects.
Objects don’t need to be in the same process or even on the same machine to send messages back and forth and receive messages from each other.

class ?
Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle’s state is independent of and can be different from that of other bicycles.
A class definition defines instance and class variables and methods
A class is a blueprint that defines the variables and the methods common to all objects of a certain kind.

instance ?
An object of a particular class
class is created using the new operator followed by the class name
*class is not shaded because it represents a blueprint of an object rather than the object itself. In comparison, an object is shaded, indicating that the object exists and that you can use it. *

interface?
a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between individuals of different ranks.
When a class implements an interface, the class agrees to implement all the methods defined in the interface

value name?
In the Java programming language, the following must hold true for a simple name:

It must be a legal identifier. Recall from the preceding section that an identifier is an unlimited series of Unicode characters that begins with a letter.
It must not be a keyword, a boolean literal (true or false), or the reserved word null.
It must be unique within its scope. A variable can have the same name as a variable whose declaration appears in a different scope. In some situations, a variable can share names with another variable, which is declared in a nested scope. Scope is covered in the next section.

Variable names begin with a lowercase letter and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter (for example, isVisible). The underscore character ( _ ) is acceptable anywhere in a name, but by convention it is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited).

Scope—-
member variable
local variable
method parameter
exception-handler parameter
member variable is a member of a class or an object. It is declared within a class but outside of any method or constructor. A member variable’s scope is the entire declaration of the class. However, the declaration of a member needs to appear before it is used when the use is in a member initialization expression
You declare local variables within a block of code.
value of a final variable cannot change after it has been initialized.
final int aFinalVar = 0;
————————ok————
final int blankfinal;
. . .
blankfinal = 0;

—————————-error———-
summary—————————-
A variable of primitive type contains a value
reference type contains a reference to a value. Arrays, classes, and interfaces are reference types.
final variable cannot change after it has been initialized

operator?
++ is a unary operator that increments the value of its operand by 1
An operator that requires two operands is a binary operator
= is a binary operator that assigns the value from its right operand to its left operand
ternary operator is one that requires three operands. which is a shorthand if-else statement
operator op             //prefix notation
op operator             //postfix notation
op1 operator op2        //infix notation
op1 ? op2 : op3         //infix notation

+ op1 + op2  Adds op1 and op2; also used to concatenate strings
– op1 – op2  Subtracts op2 from op1
* op1 * op2  Multiplies op1 by op2
/ op1 / op2  Divides op1 by op2
% op1 % op2  Computes the remainder of dividing op1 by op2
long Neither operand is a float or a double (integer arithmetic); at least one operand is a long.
int Neither operand is a float or a double (integer arithmetic); neither operand is a long.
double At least one operand is a double.
float At least one operand is a float; neither operand is a double.

+  +op  Promotes op to int if it’s a byte, short, or char
–  -op  Arithmetically negates op

Two shortcut arithmetic operators are ++, which increments its operand by 1, and –, which decrements its operand by 1. Either ++ or — can appear before (prefix) or after (postfix) its operand. The prefix version, ++op/–op, evaluates to the value of the operand after the increment/decrement operation. The postfix version, op++/op–, evaluates to the value of the operand before the increment/decrement operation

++  op++  Increments op by 1; evaluates to the value of op before it was incremented
++  ++op  Increments op by 1; evaluates to the value of op after it was incremented
—  op–  Decrements op by 1; evaluates to the value of op before it was decremented
—  –op  Decrements op by 1; evaluates to the value of op after it was decremented

>  op1 > op2  Returns true if op1 is greater than op2
>=  op1 >= op2  Returns true if op1 is greater than or equal to op2
<  op1 < op2  Returns true if op1 is less than op2
<=  op1 <= op2  Returns true if op1 is less than or equal to op2
==  op1 == op2  Returns true if op1 and op2 are equal
!=  op1 != op2  Returns true if op1 and op2 are not equal

&&  op1 && op2  Returns true if op1 and op2 are both true; conditionally evaluates op2
||  op1 || op2  Returns true if either op1 or op2 is true; conditionally evaluates op2
!  !op  Returns true if op is false
&  op1 & op2  Returns true if op1 and op2 are both boolean and both true; always evaluates op1 and op2; if both operands are numbers, performs bitwise AND operation
|  op1 | op2  Returns true if both op1 and op2 are boolean and either op1 or op2 is true; always evaluates op1 and op2; if both operands are numbers, performs bitwise inclusive OR operation
^  op1 ^ op2  Returns true if op1 and op2 are different ? that is, if one or the other of the operands, but not both, is true
ex>
0 <= index && index < NUM_ENTRIES == (0 <= index) && (index < NUM_ENTRIES)

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.