728x90
300x250

[Java] IceCream - Class 구현

 

아이스크림 클래스를 작성하였습니다.
이해를 돕기 위해서 작성하였습니다.

 


interface IceCream{
     abstract void use();
     abstract void destroy();
}

class Bar implements IceCream{
 
      public void use(){
           System.out.println("맛있는 막대바 아이스크림 섭취");
      }
 
      public void destroy()
      { 
           System.out.println("다 먹음(막대바)"); 
      }
}

 

class Cone implements IceCream{
 
      public void use()
      {
           System.out.println("맛있는 콘 타입 아이스크림 섭취");
      }
 
      public void destroy(){
           System.out.println("다 먹음(콘 타입)");
      }
}

public class Output {
 
     public static void main(String args[])
     {
           Bar iceCreamBar = new Bar();
           Cone iceCreamCone = new Cone();
  
           iceCreamBar.use();
           iceCreamCone.use();
  
           iceCreamBar.destroy();
           iceCreamCone.destroy();
     }

 아이스크림을 추상화함. - Output.java

 

(아이스크림 바, 아이스크림 콘)을 먹을 때 공통적으로 할 수 있는 일

두 가지로 생각해봄.

-> 먹는다(use)

-> 버린다(destroy)

반응형
728x90
300x250
[MFC] 윈도우 프로그래밍 기초 - 윈도우 클래스 만들기

 

WinMain 함수에서 첫 번째 단계가 윈도우 클래스를 만드는 과정입니다.

~RegisterClass( & ...... ) 까지 만드는 과정을 윈도우 클래스를 구현한다고 보시면 됩니다.

 

Winuser.h 파일에 윈도우 클래스 생성을 위한 구조체 구조를 소개하겠습니다.

 


1. WNDCLASS 구조체

 

typedef struct tag WNDCLASS{
     UINT style;

     WNDPROC lpfnWndProc;

     int cbClsExtra;

     int cbWndExtra;

     HINSTANCE hInstance;

     HICON hIcon;

     HCURSOR hCursor;

     HBRUSH hbrBackground;

     LPCSTR lpszMenuName;

     LPCSTR lpszClassName;

 winuser.h의 구조체

 

* 클래스를 생성하는 원리만 이해하면 되므로, 현재의 코드는 암기하시거나 할 필요는 없습니다.

 


2. 구현(Implements)

 

 

 #include <Windows.h>
 #include <tchar.h>

 

 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 

 LPTSTR lpszClass = _T("TestApp");

 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParameter, int nCmdShow){

 

      HWND hWnd;
      MSG Message;

      WNDCLASS WndClass;

      WndClass.cbClsExtra = 0;
      WndClass.cbWndExtra = 0;
      WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
      WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
      WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      WndClass.hInstance = hInstance;
      WndClass.lpfnWndProc = (WNDPROC)WndProc;
      WndClass.lpszClassName = lpszClass;
      WndClass.lpszMenuName = NULL;
      WndClass.style = CS_HREDRAW | CS_VREDRAW;

      RegisterClass(&WndClass);

}


LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam){

 

       // 이 영역은 다음에 언급하겠습니다.

 

}

 

 

반응형
728x90
300x250
[Java] 상속에 관한 방법

 

상속 방법 1)

 

class Account{
     String accountNo;
     String ownerName;
     int balance;
  
     void deposit(int amount){
          balance += amount;
     }
 
     int withdraw(int amount) throws Exception{
  
     if ( balance < amount )
           throw new Exception("잔액이 부족합니다.");
     else
           balance -= amount;
  
     return amount;
     }

 

Account.java

 

class CheckingAccount extends Account{

      String cardNo;
 
      int pay ( String cardNo, int amount ) throws Exception{
      if ( !cardNo.equals(this.cardNo) || (this.balance < amount ) )
           throw new Exception("지불이 불가능합니다.");
      return withdraw(amount);
     

      }

 

CheckingAccount.java

 

class InheritanceExample{                                                                              
                                                                                                    
      public static void main(String args[]) 

     {                                                            

          CheckingAccount obj;

          obj = new CheckingAccount();                                                   
          obj.accountNo = "110-22-333333";                                                          
          obj.ownerName = "홍길동";                                                                     
          obj.cardNo = "5555-6666-7777-8888";                 
          obj.deposit(100000);                                                                           
                                                                                                 
          try{                                                                                           

                 int paidAmount = obj.pay("5555-6666-7777-8888", 47000);
                 System.out.println("지불액:" + paidAmount);                          
                 System.out.println("잔액:" + obj.balance);                 
                                                                                            
          } catch(Exception e)                                                                           
          {                                                                                             
                  System.out.println(e.getMessage());            

 

           }              
                             
      }                                                                                                                                               
}                                                                                                     

 

InheritanceExample.java

 

상속 방법 2)

 

class Account{
      String accountNo;
      String ownerName;
      int balance;
  
      void deposit(int amount){
            balance += amount;
      }
 
      int withdraw(int amount) throws Exception{
  
             if (balance < amount)
                   throw new Exception("잔액이 부족합니다.");
             else
                   balance -= amount;
  
             return amount;

       }
 

 

 

Account.java

 

class CheckingAccount extends Account{

       String cardNo;

       CheckingAccount(String accountNo, String ownerName, int balance, String cardNo){
           this.accountNo = accountNo;
           this.ownerName = ownerName;
           this.balance = balance;
           this.cardNo = cardNo;

      }
 
       int pay(String cardNo, int amount) throws Exception{

 

       if ( !cardNo.equals(this.cardNo) || (this.balance < amount))
                  throw new Exception("지불이 불가능합니다.");
  

       return withdraw(amount);

 

  }
 

 

CheckingAccount.java

 

class InheritanceExample{                                                                                                                                                                                                                                          
     public static void main(String args[])

    {                                                                             
        CheckingAccount obj;

        obj = new CheckingAccount("113-333-333333", "홍길동", 100000, "5555-6666-7777-8888");
                                                                                              
       try {
             int paidAmount = obj.pay("5555-6666-7777-8888",47000);
             System.out.println("지불액:" + paidAmount);
             System.out.println("잔액:" + obj.balance);
       } catch(Exception e) {
             System.out.println(e.getMessage());

       } 
   }
}

 

InheritanceExample.java

반응형
728x90
300x250

[C#.NET] 클래스 - Overriding 개념

C# Overriding은 불필요한 공통 변수 선언을 최소화하여 효율성을 높이는 개념입니다.
이전의 글 Override에서 언급한 부모와 자녀의 관계처럼 선언에 대해 관계를 형성하는 것으로 생각하시면 됩니다.


1. 코드

 

 


using System;
class Book
{
    public string title;
    public string editor;
    public string publisher;
    public decimal price;
    public int page;
    public Book(string intitle)
    {
        title = intitle;
    }
    public Book(string intitle, string ineditor)
    {
        title = intitle;
        editor = ineditor;
    }
    public Book(string intitle, string ineditor, string inpublisher, decimal inprice, int inpage)
    {
        title = intitle;
        editor = ineditor;
        publisher = inpublisher;
        price = inprice;
        page = inpage;
    }
    public void showfields()
    {
        Console.WriteLine("제목:" + title + "\n저자:" + editor + "\n출판사:"
            + publisher + "\n가격:" + price + "\n페이지:" + page + "\n");
    }
}
class Program
{
    static void Main()
    {
        Book ba = new Book("부의미래");
        Book bb = new Book("국화와 칼", "루스 베네딕트");
        Book bc = new Book("회계원리", "유관희", "흥문사", 23000, 549);
        ba.showfields();
        bb.showfields();
        bc.showfields();
    }
}
반응형

+ Recent posts