본문 바로가기
ONLINE COURSES/Java

인프런 자바기초강의_9,10,11,12,13강

by jono 2021. 7. 17.

9강. 조건문

9-1 if -> 양자택일

9-2 switch -> 다자택일

switch(변수명) {
case 조건1:
	System.out.println("조건1");
    break;
    
case 조건2:
    System.out.println("조건2");
    break;

case 조건3:
  System.out.println("조건3");
  break;
  
default;
	break;
    
}

10강. 반복문

10-1. for문

# input값을 받아 구구단 출력하기

System.out.print("곱하고 싶은 값을 입력하세요-> ");
Scanner scanner = new Scanner(System.in);
int inputNum = scanner.nextInt();

for(i=1;i<10;i++){

System.out.printf("%d곱하기 %d는 %d입니다.\n",inputNum, i, (inputNum*i));

}

10-2. while 문

System.out.print("type input number: ");
Scanner scanner = new Scanner(System.in);
int inputNum = scanner.nextInt();
int i = 1;
while(i<10){

System.out.printf("%d곱하기 %d == %d입니다.\n", inputNum, i, (inputNum*i));
i++;

}

10-3. do while 문

int num = 10;
		
        do {
        System.out.printf("무조건 %d번은 실행함\n", num);
        num++;
        } while(num<13);

11강. 객체지향 프로그래밍이란?

11-1. 객체
: 프로그래밍에서 속성과 기능을 가지는 프로그램 단위.

11-2. 클래스
: 객체를 생성하기 위한 틀. 모든 객체는 클래스로부터 형성된다.
즉, 클래스에서부터 객체를 복제, 변형해서 만들어낸다.

11-3. 클래스 구성요소


12강. 클래스 제작과 객체 생성

12-1. 클래스 제작

클래스는 멤버변수(속성), 메서드(기능), 생성자 등으로 구성된다.

12-2. 객체 생성

12-3. 생성자

# 클래스 만들기

public class Grandeur {  
	
	//속성 
	public String color;
	public String gear;
	public int price;
	
	// 생성자(constructor)
	public Grandeur() {
		System.out.println("this is Grandeur constructor");
		
	
	//기능
	public void run() {
		System.out.println("--run");
	}
    
	public void stop() {
		System.out.println("--stop");
	}
    
	public void info() {
		System.out.println("---information---");
		System.out.println("color: " + color);
		System.out.println("gear is: " + gear);
		System.out.println("price is: " + price);
	}

}

# 위에서 만든 클래스를 메인클래스에 추가해 객체생성하기

public class MainClass {
	public static void main(String[] args) {
		
		Grandeur myCar1 = new Grandeur();
		myCar1.color="red";
		myCar1.gear="manual";
		myCar1.price=5000;
		
		myCar1.run();
		myCar1.stop();
		myCar1.info();
		
		System.out.println("----------------------------------");
        
}

***클래스를 만들때 생성자의 괄호 안에 매개변수를 지정해 객체의 코드를 더 깔끔하게 할 수 있다

// 클래스생성시
public class Grandeur {  
	
	//속성 
	public String color;
	public String gear;
	public int price;
	
	// 생성자(constructor)
	public Grandeur(String c, int p,String g) {
		this.color = c;
		this.price = p;
		this.gear = g;
	}
    
// 메인클래스 
Grandeur myCar1 = new Grandeur("red",5000,"manual");

=> 메인클래스 코드가 한줄로 깔끔하게 만들어졌다.


13. 메서드

13-1. 메서드 선언과 호출

-메서드도 변수와 같이, 선언 및 정의 후 필요시에 호출해서 사용한다.

13-2. 매개변수(parameter)
-필요시에만 사용하면 된다.
- 매개변수는 자료형과 변수(지역변수)로 이루어진다.

public void serInfo(int i, boolean b, double d, char c, String s){...}

13-3. 중복 메서드(overloading)
- 이름은 같고, 매개변수의 개수 or 타입이 다른 메서드를 만들 수 있다.

public void getInfo(){...]
public void getInfo(int x, String y){...}

13-4. 접근자
- 메서드를 호출할 때 접근자에 따라서 호출이 불가능할 수도 있다.

=> private메서드의 경우.


14강. 객체와 메모리

14-1. 메모리에서 객체생성(동적생성)

- 객체는 메모리에사 동적으로 생성되며, 객체가 더이상 필요 없게 되는 GC(garbage collector)에 의해서 제거된다.

14-2. 레퍼런스

14-3. 자료형이 같아도 다른 객체이다.

14-4. null과 NullPointerException
: 객체를 null로 지정하면 레퍼런스가 없어지면서 연결이 끊기게된다.
-> 'NullPointerException' 이라는 오류가 뜸

 

 

 

 

 

 

 

 

댓글