부모 클래스

class TV 

{

int channel = 9;

int volumn = 10;

boolean power = false;

TV(){

System.out.println("TV()생성자 실행됨");

}

TV(int channel, int volumn){

this.channel = channel;

this.volumn = volumn;

System.out.println("TV(int, int) 생성자메소드 실행됨");

}


//채널

void channelUp(){

channel++;

if(channel<=1){

channel = 1;

}

}

void ChannelDown(){

channel--;

if(channel<=0){

channel = 13;

}

}


//볼륨

void volumnUp(){

volumn++;

if(volumn>=30){

volumn=30;

}

}

void volumnDown(){

volumn--;

if(volumn<0){

volumn=0;

}

}


//파워

void powerOnOff(){

if(power==true){

power = false;

}else{

power=true;

}

}

}


=====================================================================

자식 클래스

class TvWhite30 extends TV

{

String color = "white";

int size = 30;

int volumn = 20;

//생성자 메소드

TvWhite30(){

super(1, 1); //상위 클래스의 생성자를 호출하는 super()는 첫번째 줄에만 사용할 수 있다.

System.out.println("TvWhite30()생성자 실행됨");

}

//overriding : 상위 클래스에서 정의된 메소드를 하위 클래스에서 재정의

//   메소드명과 인자가 같아야 한다.

void volumnUp(){

volumn += 2;

if(volumn>=50){

volumn=50;

}

}

void volumnUpOld(){

super.volumnUp(); //상위 클래스에 있는 메소드 호출

System.out.println("볼륨 : "+super.volumn);

}

//this : 자신의 클래스

//super : 상위 클래스


public static void main(String ar[]){

TvWhite30 tw = new TvWhite30();

//채널

tw.channelUp();

System.out.println("채널번호 : "+tw.channel);


//볼륨 변경

tw.volumnUp();

System.out.println("볼륨 : "+tw.volumn);


tw.volumnUpOld();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

package ComFile  (0) 2016.12.01
상속 관계에서 객체 형변환  (0) 2016.12.01
[객체] 로또번호 생성기  (0) 2016.11.30
생성자메소드에서 다른 생성자메소드 호출하는 법  (0) 2016.11.30
오버로딩(overloading)  (0) 2016.11.30
Posted by Hyun CHO
,

import java.util.Scanner;

import java.util.Random;

import java.util.Arrays;


class  LottoObject

{

LottoObject(){

Random ran = new Random();

Scanner scan = new Scanner(System.in);

System.out.print("게임수 = ");

int cnt = scan.nextInt();

for(int p=1; p<=cnt; p++){

int lottoNum[] = new int[6];

for(int i=0; i<lottoNum.length; i++){

lottoNum[i] = ran.nextInt(45)+1;

for(int chk=0; chk<i; chk++){

if(lottoNum[i] == lottoNum[chk]){

i--;

}

}

}

Arrays.sort(lottoNum);

System.out.println(p+"게임="+Arrays.toString(lottoNum));

}

}

public static void main(String[] args) 

{

LottoObject lo = new LottoObject();

}

}

'응용 SoftWare > JAVA' 카테고리의 다른 글

상속 관계에서 객체 형변환  (0) 2016.12.01
클래스 상속  (0) 2016.12.01
생성자메소드에서 다른 생성자메소드 호출하는 법  (0) 2016.11.30
오버로딩(overloading)  (0) 2016.11.30
[객체] 구구단 만들기  (0) 2016.11.30
Posted by Hyun CHO
,

class ConstractorTest 

{

String name;

String tel;

int age;

//생성자메소드의 다른 생성자메소드 호출하는 법

ConstractorTest(){

//name = "guest";

this("guest");

}

ConstractorTest(String name){

this.name = name;

}

ConstractorTest(String name, String tel){

//this.name = name;

this(name);

this.tel = tel;

}

ConstractorTest(String name, String tel, int age){

//this.name = name

//this.tel = tel;

//같은 클래스의 다른생성자를 호출할 때는 this()를 사용한다.

//첫번째 줄에만 사용할 수 있다.

this(name, tel);

this.age = age;

}


public static void main(String[] args) 

{

ConstractorTest ct1 = new ConstractorTest("조현");

ConstractorTest ct2 = new ConstractorTest("조현", "010-9142-1348", 35);

System.out.println("ct2.name="+ct2.name);

System.out.println("ct2.tel="+ct2.tel);

System.out.println("ct2.age="+ct2.age);

ConstractorTest ct3 = new ConstractorTest();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

클래스 상속  (0) 2016.12.01
[객체] 로또번호 생성기  (0) 2016.11.30
오버로딩(overloading)  (0) 2016.11.30
[객체] 구구단 만들기  (0) 2016.11.30
[객체] 임의의 수를 입력받아 여러 수식을 만들어라.  (0) 2016.11.30
Posted by Hyun CHO
,