토요일, 12월 21
Shadow

#004 Class & Object

Member variables in a class—these are called fields.
Variables in a method or block of code—these are called local variables.
Variables in method declarations—these are called parameters.

public int cadence;
public int gear;
public int speed;
The fields of Bicycle are named cadence, gear, and speed and are all of data type integer (int). The public keyword identifies these fields as public members, accessible by any object that can access the class.

public modifier—the field is accessible from all classes.
private modifier—the field is accessible only within its own class.
public class Bicycle {

private int cadence;
private int gear;
private int speed;

public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

public int getCadence() {
return cadence;
}

public void setCadence(int newValue) {
cadence = newValue;
}

public int getGear() {
return gear;
}

public void setGear(int newValue) {
gear = newValue;
}

public int getspeed() {
return speed;
}

public void applyBrake(int decrement) {
speed -= decrement;
}

public void speedUp(int increment) {
speed += increment;
}

}
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) {
//do the calculation here
}

The only required elements of a method declaration are the method’s return type, name, a pair of parentheses, (), and a body between braces, {}.
*method declarations have six components, in order
1.Modifiers—such as public, private, and others you will learn about later.
2.The return type—the data type of the value returned by the method, or void if the method does not return a value.
3.The method name—the rules for field names apply to method names as well, but the convention is a little different.
4.The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
5.An exception list—to be discussed later.
6.The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.

Naming a Method
method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:
run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty

Overloaded methods
you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list.
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) {

}
}

method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

Bicycle myBike = new Bicycle(30, 0, 8);

* If another class cannot call a MyClass constructor, it cannot directly create MyClass objects.

public Polygon polygonFrom(Point[] corners) {
// method body goes here
}

public class Circle {
private int x, y, radius;
public void setOrigin(int x, int y) { //method

}
}

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

1.Declaration: The code set in bold are all variable declarations that associate a variable name with an object type. : Point originOne =
2.Instantiation: The new keyword is a Java operator that creates the object. : new
3.Initialization: The new operator is followed by a call to a constructor, which initializes the new object. : Point(23, 94);

The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:
int height = new Rectangle().height;

public class Rectangle {
public int width = 0;
public int height = 0;
public Point origin;

// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}

// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}

// a method for computing the area of the rectangle
public int getArea() {
return width * height;
}
}

objectReference.fieldName

Referencing an Object’s Fields
int height = new Rectangle().height;
This statement creates a new Rectangle object and immediately gets its height. In essence, the statement calculates the default height of a Rectangle. Note that after this statement has been executed, the program no longer has a reference to the created Rectangle, because the program never stored the reference anywhere. The object is unreferenced, and its resources are free to be recycled by the Java Virtual Machine

Calling an Object’s Methods
objectReference.methodName(argumentList);
or
objectReference.methodName();
int areaOfRectangle = new Rectangle(100, 50).getArea();

답글 남기기

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

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