변수와 메소드로 정의된 클래스로 생성되어 메모리 상에
존재하는 클래스의 인스턴스를 뜻한다.
Rectangle rec = new Rectangle();
==========================
public class Rectangle {
int height;
int width;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getArea() {
return width * height;
}
}
임의의 번지에 메모리가 할당되며, 변수나 메소드에 상관없이 4byte씩 할당된다.
클래스는 객체를 위한 설계도, 객체는 클래스로 생성된 인스턴스이다.
1 부터 num 까지 합을 구하는 class 를 작성하도록 하시오.
class Sum{
int sum = 0;
public int getSum(int num){
for(i=1; i<=num; i++){
sum+=i;
}
return sum;
}
}
StraPrint strPrint = new StarPrint();
strPrint.printTriangle(3);
System.out.println();
strPrint.printReverseTriangle(3);
Class StarPrint{
int num1;
public PrintStar(int num1) {
this.num1 = num1;
}
public void printTriangle(int number) {
for (int i = 1; i <= number; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public void printReverseTriangle(int number) {
for (int i = number; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
출력 결과
*
**
***
***
**
*
클래스의 이름과 같은 함수이며,
객체를 생성할때 원하는 값으로 초깃값을 설정해주는 함수를
생성자라고 한다.
객체가 생성될때, 사용자가 초깃값을 설정해주지 않으면 컴파일러가
자동으로 생성해주는 생성자를 뜻한다.
디폴트 생성자는 사용자로부터 인수를 전달받지 않으므로, 매개변수를 가지지 않는다.
객체의 생성과 함께 초깃값을 설정해주기 위함이다.
null은 참조형 타입의 기본 value이다.
모든 기본형 타입이 초깃값을 가지듯이, 참조형 타입은
기본적으로 null(비어있는)값을 가지는 것이다.
노래 한 곡을 나타내는 Song 클래스를 작성하라.
Song은 다음 필드로 구성된다.
또한 Song 클래스에 다음 생성자와 메소드를 작성하라.
song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력하라.
1978년 스웨덴국적의 ABBA가 부른 Dancing Queen
소스코드 Song.java
public class Song {
String title;
String artist;
int year;
String country;
Song() {}
Song(String title, String artist, int year, String country) {
this.title = title;
this.artist = artist;
this.year = year;
this.country = country;
}
public void show() {
System.out.println(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
}
}
Grade me = new Grade(90, 70, 100);
System.out.println("평균은 "+me.average());
Class Grade {
int score1, score2, score3;
double average;
double average() {
return (score1 + score2 + score3)/3;
}
}
다음 main() 메소드를 실행하였을 때 예시와 같이 출력되도록 TV 클래스를 작성하라.
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32); //LG에서 만든 2017년 32인치
myTV.show();
}
출력 결과
LG에서 만든 2017년형 32인치 TV
소스코드
class TV{
String brand_name;
int year;
int inch;
TV(String brand_name, int year, int inch){
this.brand_name = brand_name;
this.year = year;
this.inch = inch;
}
public void show() {
System.out.println(brand_name + "에서 만든 "+year+"년형 "+inch+"인치 TV");
}
BankAccount ref1 = new BankAccount();
BankAccount ref2 = ref1;
Gugudan gugudan = new Gugudan();
gugudan.printGugu(10); //1단부터 10단까지 출력
gugudan.printGugu(20); //1단부터 20단까지 출력
소스코드
public class Gugudan {
public static void main(String[] args) {
PrintGugudan gugudan = new PrintGugudan();
gugudan.printGugu(10);
gugudan.printGugu(20);
}
}
class PrintGugudan {
int number1;
PrintGugudan() {
}
PrintGugudan(int number1) {
this.number1 = number1;
}
public void printGugu(int dan) {
for (int i = 1; i <= dan; i++) {
for (int j = 1; j <= 9; j++) {
System.out.println(i + "*" + j + "=" + (i * j));
}
}
}
}
출력결과
1*1=1
1*2=2
1*3=3 ......
20*9=180