본문 바로가기

Daily Report

110725 일일보고서 java 객체지향의 구조 및 장점


프로그램 개요 : 프로그램을 공부하며 지나치는 과정중,,,

 

1. 변수와 상수의 개념

2. 반복문(for, while)

이중 for

  1)Count변수 : 반복문의 반복횟수

  2)flag변수 : 상태가 0으로 떨어지는가?

      -소수구하기

2.5 함수 (call by reference)

===========임베디드 프로그램밍 가능============

 

3. 배열(자료구조)

   1)달팽이배열

   2)마방진

 

4. 포인터

 

5. 구조체

 

============구조적인 프로그램밍 가능============

 

단점 :  생산성이 떨어진다

 

oop 객체지향 프로그램밍(30%향상)

 

1. 캡슐화 : 감출건 감추고 보여줄건 보여주자

2. 다형성 : 오버로딩 오버라이딩(자식클래스를 부모클래스를 가려준다)

3. 상속 : 기존의 만들었던 코드를 재활용하자(인터페이스, 웹퍼클래스(박싱 언박싱))

 

디자인 패턴   I/O Network 등등


객체지향형 프로그램밍의 구조 

A.java 파일

 

public class A{

             //생성자 (디폴트 생성자)

A(){

             this.name = 0;

{

private int name;

 

생성

public static void main(String args[]){

             A a=new A(); //heap메모리

 

}

 
자동차라는 객체를 가지고 java에 응용하여 프로그램을 작성한다면...

 

Car  클래스

 

package kr.ac.busanit;

 

public class Car {

       //맴버변수

       private int speed;

       private String color;

      

       //default 생성자

       //생성자의 역할은 객체의 초기화에 있다!!!

       Car(){

       //     this.color = "흰색";

       }

      

       //인자가 2 있는 생성자

       Car(String color, int speed){

             this.color = color;

             this.speed = speed;

       }

            

       public int getSpeed() {

             return speed;

       }

 

       public void setSpeed(int speed) {

             this.speed = speed;

       }

 

       public String getColor() {

             return color;

       }

 

       public void setColor(String color) {

             this.color = color;

       }

 

       //맴버메소드

       public void run(){

             System.out.println("달리다,");

      

       }

}


CatTest()

 

package kr.ac.busanit;

      

public class CarTest {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

       Car car = new Car(); // 객체생성

       car.run();

       car.setColor("검은색"); //내부클래스라서 에러메시지가 뜨지 않는다

       System.out.println("차의 색상은 " + car.getColor());

      

       Car mycar = new Car("노란색", 100); //인자가 2 있는 생성자를 통하여 생성

       System.out.println("차의 색상은 " + mycar.getColor());

       System.out.println("차의 스피드는 " + mycar.getSpeed());

       }

}


point(정리중)
1) private를 활용하여 다른 클래스의 내용을 가릴수있다
2) private가 적용된 클래스에서는 get/set을 이용화여 입력및 출력을 할수있다
3) default생성자의 역할은 초기화를 하는 것이다
4) 인자가 2개있는 생성자를 활용하여 선언과 동시에 값을 초기화 할수있다
5) 객체를 생성한후 클래스를 활용하도록 한다
6) String = char *

 

===<예제 객체지향을 활용한 은행 계좌 입출력 프로그램>===


package kr.ac.busanit;

 

class Account{

       private String accountNo;

       private String ownerName; 

      

       private int balance;

      

       Account(){

      

       }

       Account(String accountNo){

             this.accountNo = accountNo;

            

       }

      

       public String getAccountNo() {

             return accountNo;

       }

 

       public void setAccountNo(String accountNo) {

             this.accountNo = accountNo;

       }

 

       public String getOwnerName() {

             return ownerName;

       }

 

       public void setOwnerName(String ownerName) {

             this.ownerName = ownerName;

       }

 

       public int getBalance() {

             return balance;

       }

 

       public void setBalance(int balance) {

             this.balance = balance;

       }

 

       void deposit(int amount){

             balance += amount;

       }

       int withdraw(int amount){

             if(balance < amount)

                    return 0;

             balance -= amount;

             return amount;

       }

}

public class AccountTest {

 

 

       public static void main(String[] args) {

             // TODO Auto-generated method stub

             Account myAccount = new Account("111-1111-1111");

            

             //myAccount.setAccountNo(111-1111-1111);

            

             System.out.println("고객님의 계좌 번호는 : " + myAccount.getAccountNo());

             myAccount.deposit(1000000);

            

             System.out.println("현재 통장의  잔액은 : " + myAccount.getBalance());

            

             System.out.println( myAccount.withdraw(100000)+ " 출금하였습니다");

             System.out.println("현재 통장의 잔액은 : " + myAccount.getBalance());

           

       }

 }

 

결과>>

고객님의 계좌 번호는 : 111-1111-1111

현재 통장의  잔액은 : 1000000

100000 출금하였습니다

현재 통장의 잔액은 : 900000