관리 메뉴

IT창고

자바(JAVA)실습 - 클래스사용하기 본문

프로그래밍/JAVA

자바(JAVA)실습 - 클래스사용하기

방구석여포 2017. 9. 2. 15:38


조건 

1. 클래스와 상속사용하기

2. 서브클래스2개 이상사용하기

3. 메인클래스는 활용최소화


소스코드

Main 


public class Main {

public static void main(String[] args) {

System.out.println("자동차 목록 확인\n");

Control con = new Control();

con.strat();

System.out.print("특별이벤트!\n"+"전 차량 90% 할인");

}

}

------------------------------------------------

Control


public class Control  {

public void strat(){

Sub1 bo = new Sub1("부가띠", 1000, "무지개색");

System.out.println("차량명칭: "+bo.getName());

System.out.println("차량속도: "+bo.getMaxspeed()+"km");

bo.col();

System.out.println("\n");


Sub2 bo2 = new Sub2("람보르기니", 900, "딥다크");

System.out.println("차량명칭: "+bo2.getName());

System.out.println("차량속도: "+bo2.getMaxspeed()+"km");

bo2.col();

System.out.println("\n");

Sub3 bo3 =new Sub3("mycar", -100, 3435);

System.out.println("차량명칭: "+bo3.getName());

System.out.println("차량속도: "+bo3.getMaxspeed()+"km");

bo3.nu();

System.out.println("\n");

}

}

------------------------------------------------

Super


public class Super {

private String name;

private int maxspeed;

public Super(String name, int maxspeed){    

this.name = name;

this.maxspeed = maxspeed;

}

public void info(){

System.out.println("차종"+name);

System.out.println("최고속도"+maxspeed);

}

public String getName() {

return name;

}

public int getMaxspeed() {

return maxspeed;

}

------------------------------------------------

Sub1


public class Sub1 extends Super {

private String color;

public Sub1(String name, int maxspeed, String color){

super(name, maxspeed);

this.color =color;

}

public void col(){

System.out.println("차량색깔: "+color);

}

}

------------------------------------------------

Sub2


public class Sub2 extends Super {

private String color;

public Sub2(String name, int maxspeed, String color){

super(name, maxspeed);

this.color =color;

}

public void col(){

System.out.println("차량색깔: "+color);

}

}

------------------------------------------------

Sub3


public class Sub3 extends Super {

private int num;

public Sub3(String name, int maxspeed, int num){

super(name,maxspeed);

this.num = num;

}

public void nu(){

System.out.println("차량번호: "+num);

}

}


코딩하면서 Super클래스를 상속하고 다른 서브클래스를 연결하고 컨트롤러 클래스에서 구성하는데 Super클래스에서 함수를 받아오거나 메인클래스로 컨트롤러클래스를 연결시키는 부분이 어려웠는데 명령어를 알고나서 문제를 해결하게되었습니다.  


결과화면

메인에서 System.out.println으로 알기 쉽게 글을 넣어주고 Super클래스에서 서브클래스로 상속받아 제대로 출력이 되는지 확인하였습니다.

Comments