자바

 

 

 


* 오버로딩


- 같은 클래스 내에서 method 이름 중복

    void print(int a);
    void print(String a);


* 오버라이딩

 


- 상속 관계
- mehtod 재정의


* 무명 클래스 (anonymoun class) - 이름이 없는 클래스

 

- 인터페이스, 추상크래스 자료형

new 자료형(){

    // 추상 메소드 구현
    
}

무명 클래스로 생성된 객체는 재사용을 할 수 없음

 

 

* 람다식

 

나중에 실행될 목적으로 다른 곳에 전달될 수 있는 함수
이름이 없는 메소드
무명 클래스를 쉽게 작성할 수 있다.
JDK 1.8부터 사용 가능
인터페이스에  2개 이상의 method 가 있는 경우에는 사용할 수 없음


A   ->    B
method   body

()-> System.out.println("....");
(String s) -> {System.out.println(s) ;}
()->{ return 100;}

 

 

* 컬렉션( 배열의 단점을 보완)

 

 

배열의 장점 : 빠르다.
배열의 단점 : 1. 사이즈 고정 2.자료형 제한 3. 추가/삭제가 어려움

    1 2 3 4 5 6 7 8 9 10

ArrayList

 

FlyingCar2 

package java09;

import java.awt.FlowLayout;

// class 클래스 extends 부모 클래스 implements 인터페이스
class Car{
	int speed;
	public void setSpeed(int speed){
		this.speed=speed;
	}
	
}

public class FlyingCar2 extends Car implements Flyable{

	@Override
	public void fly() {
		
		System.out.println("날고 있음..." );
		
	}

	public static void main(String[] args) {
		FlyingCar2 obj =new FlyingCar2();
		obj.setSpeed(300);
		obj.fly();
	}
	
	
}

 

class AnonymousClassTest

 

package java09;

import java08.RemoteControl;

public class AnonymousClassTest {

	public static void main(String[] args) {
		RemoteControl obj =new RemoteControl(){

			@Override
			public void turnOn() {
				System.out.println("전원을 켭니다.");
			}

			@Override
			public void turnOff() {
				System.out.println("전원을 끕니다");
			}
		};
		
	}
	
	
}

 

class FlyingCar3

package java09;

public class FlyingCar3 {

	public static void main(String[] args) {
    	//인터페이스는 객체를 생성할 수 없음
		//Drivable d =new Drivable() ;
		
		//무명객체 = 익명 객체
		Drivable d =new Drivable() {		
			@Override
			public void dirve() {
				// TODO Auto-generated method stub
				System.out.println("운전중 ..");
			}
		};
		
		d.dirve();
		
	}
	
	
	
}

 

class FlyingCar1

package java09;

import java.awt.FlowLayout;

interface Drivable{
	void dirve();
}

interface Flyable{
	void fly();
}

//클래스 - 다중상속 금지, 인터페이스 - 다중구현 가능
// Add unimplemented methods : 구현되지 않은 method 추가
//Make type 'FlyingCar' abstract : 현재 클래스를 추상클래스로 선언
public class FlyingCar1 implements Drivable, Flyable {
	
	public static void main(String[] args) {
		
		FlyingCar1 obj=new FlyingCar1();
		obj.dirve();
		obj.fly();
	}
	
	@Override
	public void fly() {
		System.out.println("날고 있음...");
	}

	@Override
	public void dirve() {
		System.out.println("운전 중....");
	}

	
	
}


 

 

class CallbackTest

 

package java09;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

/*class Myclass implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
			System.out.println("beep");
	}

}*/

public class CallbackTest {

	public static void main(String[] args) {
	//	ActionListener listener =new Myclass();
	//	Timer t=new Timer(1000, listener);
	
		
		//Timer(int delay, ActionListener listener)
		Timer t=new Timer(2000, e -> System.out.println(1000));
		
		Timer t2=new Timer(2000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		t.start();
		
		for(int i=0; i<1000; i++){
			try{
				Thread.sleep(1000);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}
	
	
}

 

class LambdaTest1

 

package java09;

@FunctionalInterface //method 를 1개만 가지는 인터페이스
interface MyInterface{
	void sayHello();
}

public class LambdaTest1 {

	public static void main(String[] args) {
		//인터페이스는 객체를 만들 수 없음
		
//		MyInterface hello =new MyInterface() {
//			
//			@Override
//			public void sayHello() {
//				System.out.println("Hello...");
//				
//			}
//		};

		MyInterface hello= () -> System.out.println("Hello...");
		
		hello.sayHello();
	}
	
}

 

 MyInterface2

package java09;


interface MyInterface2{

 public void calculate(int  x, int y);
}


public class LambdaTest2 {

	public static void main(String[] args) {
		
/*		MyInterface2 hello =new MyInterface2() {
			
			@Override
			public void calculate(int x, int y) {
				
				int result=x*y;
				System.out.println("계산 결과 : " + result);
			}
		};
		*/
		
		MyInterface2 hello =(x, y)->{
			int result=x*y;
			System.out.println("계산 결과 : " + result);
		};
		
		hello.calculate(5, 10);
	}
}



 

class LambdaTest3

 

package java09;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LambdaTest3 {

	public static void main(String[] args) {
		List<String> list=new ArrayList<>();
		list.add("AAA");
		list.add("bbb");
		list.add("ccc");
		list.add("DDD");
		list.add("eee");
	 //대소문자 구별한 정렬
		Collections.sort(list);
		System.out.println(list);
	
		//대소문자 구분없는 정렬
		Collections.sort(list, new Comparator<String>(){

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o1.compareToIgnoreCase(o2);
			}
			
			
		});
		System.out.println(list);
		
		//람다식
		Collections.sort(list, (o1, o2)-> o1.compareToIgnoreCase(o2));
		System.out.println(list);
		
		
//		출력값
/*    
 *  	[AAA, DDD, bbb, ccc, eee]
		[AAA, bbb, ccc, DDD, eee]
		[AAA, bbb, ccc, DDD, eee]
*/
		
	}
	
	
	
	
	
	
}

 

class ListTest

package java09;

import java.util.ArrayList;
import java.util.List;

public class ListTest {

	public static void main(String[] args) {
		List list =new ArrayList<>();
		//자료형의 제한이 없고 사이즈가 유동적
		list.add(100);
		list.add("hello");
		list.add(true);
		
		
		for(Object obj: list){
			System.out.println(obj);
		}
		
	}
}

 

 

class ObjectTest

package java09;


//Object : 모든 클래스의 조상, 최상위 클래스
public class ObjectTest {

	public static void main(String[] args) {
		ObjectTest obj=new ObjectTest();
		//해쉬코드
		System.out.println(obj);
		
		//Integer a=100;
		Object a=new Integer(100);
		System.out.println(a);
		
	}
}

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

남을 속이는 일보다 자기 자신을 속이는 일이 더 어렵다. -탈무드-

댓글 ( 4)

댓글 남기기

작성