예제) 명시적 인터페이스(2)

 

 

 

class FlyingSportsCar : IRun, IFly

{

  //public void IRun.DefaultAction() // 접근제한자 사용 금지

  void IRun.DefaultAction()

  {

  Console.WriteLine("Run~~~");

  }

 

  void IFly.DefaultAction()

  {

  Console.WriteLine("Fly~~~");

  }

 

  public void Crash()

  {

  Console.WriteLine("Bang~~~");

  }

}

 

 

 

 

 

 

예제) 명시적 인터페이스(1)

 

 

 

 

 

using System;

 

interface IFly

{

  void DefaultAction();

  void Crash();

}

 

interface IRun

{

  void DefaultAction();

  void Crash();

}

 

 

 

 

 

 

앞 예제에 대한 설명

 

 

 

 

- 인터페이스를 추상클래스로 상속할 수 있다.

   일반적으로, 인터페이스의 모든 메소드를

   다 구현할 수 없을 경우 추상 클래스를 사용한다.

 

   abstract class Airplane : Ifly

 

 

- 다중 상속을 할 수있다. 아래의 예에서 이후에 나오는 SportsCar

   클래스이고, IFly는 인터페이스이다.

   이후 첫번째에는 클래스 또는 인터페이스가 올 수 있고,

   두번째 이후에는 인터페이스만 올 수 있다.

 

   class FlyingSportsCar : SportsCar, Ifly

 

 

 

 

 

 



『 파일 다운로드(Writer/OutputStream사용)

​<소스코드>

​@Controller
public class DownloadController {
    // Writer를 사용하는 경우
    @RequestMapping(value="/downloadFile3", method = {RequestMethod.GET} )
    public void downloadFile3(Writer writer) throws IOException {
       
        writer.append("HelloWorld");
    }
}


 

 

 

 


 

 



『 파일 다운로드(ResponseEntity사용)

​<소스코드>

​@Controller
public class DownloadController {
   
    // ResponseEntity를 사용하는 경우
    @RequestMapping(value="/downloadFile2", method = {RequestMethod.GET} )
    public ResponseEntity<String> downloadFile2() throws IOException {
       
        // HTTP헤더 지정
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "octet-stream"));
        headers.set("Content-Disposition", String.format("filename=\"%s\"", "sample.txt"));
       
        String data = "HelloWorld!";
        return new ResponseEntity<String>(data, headers, HttpStatus.OK);
       
    }
}



 

 

 


 

 

예제) 인터페이스(2)

 

 

 

class FlyingSportsCar : SportsCar, IFly

{

  public void Fly()

  {

  Console.WriteLine("Fly~~~");

  }

}

class Program

{

  public static void Main()

  {

  FlyingSportsCar a=new FlyingSportsCar();

  a.Fly();

  }

}

 

 

 

 

 

 

 

예제) 인터페이스(1)

 

 

 

using System;

interface IFly

{

  void Fly();

}

class SportsCar

{

  public void FastRun(){}

}

abstract class Airplane : IFly

{

  public abstract void Fly();

}

 

 

 

 

 

 

 

 

 간단한 interface의 정의/사용

 

 

 

- interface 키워드를 사용한다.
- 메소드 정의는 ;으로 마무리 해준다. 즉 구현하지 않는다.
- 클래스에서 인터페이스를 구현할 때에는 : 을 쓴다. (클래스 상속과 동일)

 

interface IFly {  // 정의하기

  //string name;  // 필드를 포함할 수 없다.

  void Fly();

}

class FlyingSportsCar : Ifly{  // 구현하기

}

 

 

 

 

 

 

 

 

+ Recent posts