이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Make your own type
- So far you used classes like
StringandMaththat Java gave you. - Now you will write your own class. A class is a plan for an object.
- An object holds data (its own values) and methods (things it can do).
English
Instance variables
- Data that each object keeps is stored in instance variables (also called fields).
- You declare them inside the class, but outside any method.
- Each object gets its own copy of these variables.
- Write
privatebefore each field: only the class's own methods can then reach it. Lesson 7 explains why AP answers do this.
English
public class Dog {
private String name; // instance variable
private int age; // instance variable
}
English
A constructor
- A constructor sets up a new object. It runs when you write
new. - It has the same name as the class and no return type (not even
void). - Use the word
thisto mean "this object's own variable".
English
public class Dog {
private String name;
private int age;
// Constructor: runs when we make a new Dog
public Dog(String name, int age) {
this.name = name; // this.name is the field; name is the parameter
this.age = age;
}
}
English
Instance methods
- An instance method is an action an object can do.
- It can read and change the object's own instance variables.
- You call it on an object with a dot:
myDog.speak().
English
public class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
// Instance method: returns a sentence about this dog
public String describe() {
return this.name + " is " + this.age;
}
}
English
Make and use an object
- Use
new ClassName(...)to build an object. This calls the constructor. - Store it in a variable whose type is the class name.
- Then call its methods with a dot.
English
public class Main {
public static void main(String[] args) {
Dog d = new Dog("Rex", 4);
System.out.println(d.describe()); // Rex is 4
}
}
class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String describe() {
return this.name + " is " + this.age;
}
}
English
Why this?
- A parameter and a field can have the same name (like
name). - Inside the constructor,
namemeans the parameter;this.namemeans the field. thisalways means the object the method was called on.
English
Common mistakes
- A constructor has no return type and matches the class name.
- Use
this.fieldto tell a field from a parameter of the same name.
English
Now you try
- Each task gives you a class skeleton with a TODO. Fill it in so it compiles and works.
- A hidden Harness builds your object and checks the results.
- Use Check answer to compile your code with the supplied tests and view the results. Keep the class and method interfaces shown in the starter.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Counter 클래스를 완성하세요. int 필드 count가 0으로 시작합니다. increment()는 이 값에 1을 더합니다. getCount()는 현재 값을 반환합니다.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Rectangle 클래스를 완성하세요. 컨스트럭터는 너비 w와 높이 h를 받아 저장합니다. area()는 너비에 높이를 곱한 결과를 반환합니다. perimeter()는 (너비 + 높이)의 2배를 반환합니다.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Greeter 클래스를 완성하세요. 컨스트럭터는 String name를 받아 필드 name에 저장합니다. greet()는 텍스트 Hello, 뒤에 이름과 !를 추가하여 반환합니다. ("Ana"인 경우 Hello, Ana!를 반환합니다.)
출력을 보려면 '실행'을 클릭하세요.