private

응용 SoftWare/JAVA 2016. 12. 1. 14:55

class  SchoolInfor

{

//private 으로 선언된 변수는 같은 클래스 내에서만 접근가능

//private 으로 선언된 변수는 상속하지 않는다.

private String name = "SchoolInfor 조현";

private int kor = 90;


SchoolInfor(){}


void output(){

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

}


void setName(String name){

this.name = name;

}

void setKor(int kor){

this.kor = kor;

}

String getName(){

return name;

}

int getKor(){

return kor;

}

}


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

메인

class SchoolMain extends SchoolInfor

{

SchoolMain(){

}

void output(){

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

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

}

public static void main(String[] args) 

{

SchoolMain sm = new SchoolMain();

sm.output();

}

}



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

final  (0) 2016.12.01
static  (0) 2016.12.01
public과 protected  (0) 2016.12.01
package ComFile  (0) 2016.12.01
상속 관계에서 객체 형변환  (0) 2016.12.01
Posted by Hyun CHO
,

하위폴더 1 : public

package com.nate; //컴파일 명령어 cmd> 해당경로 접근 후 javac -d . 파일명.java


public class Information 

{

String name = "Information 조현";

int age = 35;


public Information(){}


public void output(){

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

}

public String getName(){

return name;

}

}


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


하위폴더 2 : public + protected

package com.nate;

public class  Student

{

String name = "Student 조현";

String school = "가산초등학교";


public Student(){}


protected void prn(){

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

}

}


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

동일폴더
class  SchoolInfor
{
String name = "SchoolInfor 조현";
int kor = 90;

SchoolInfor(){}

void output(){
System.out.println(name+" = "+kor);
}

void setName(String name){
this.name = name;
}
void setKor(int kor){
this.kor = kor;
}
String getName(){
return name;
}
int getKor(){
return kor;
}
}

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

메인

import com.nate.Information;

import com.nate.Student;


class StartMain extends Student

{

StartMain(){}

void test(){

prn();

}

public static void main(String[] args) 

{

SchoolInfor si = new SchoolInfor(); //동일 폴더에 있는 객체 : 무조건 실행

si.output();


Information i = new Information(); //하위 폴더에 있는 객체 : public 필요(범용)

i.output();


//System.out.println("i.name = "+i.name);

System.out.println("i.getName = "+i.getName());


Student s = new Student(); //하위 폴더에 있는 객체 : public과 protected 활용

//s.prn(); //메소드가 protected이기 때문에 상속받아 사용할 수 있다.

StartMain sm = new StartMain();

sm.prn();

}

}



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

static  (0) 2016.12.01
private  (0) 2016.12.01
package ComFile  (0) 2016.12.01
상속 관계에서 객체 형변환  (0) 2016.12.01
클래스 상속  (0) 2016.12.01
Posted by Hyun CHO
,

package com.nate; //컴파일 명령어 cmd> 해당경로 접근 후 javac -d . 파일명.java


public class Information 

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

private  (0) 2016.12.01
public과 protected  (0) 2016.12.01
상속 관계에서 객체 형변환  (0) 2016.12.01
클래스 상속  (0) 2016.12.01
[객체] 로또번호 생성기  (0) 2016.11.30
Posted by Hyun CHO
,

상위 클래스

class  Parent

{

String name = "조현";

int age = 35;


void setName(String name){

this.name = name;

}

void setAge(int age){

this.age = age;

}

String getName(){

return name;

}

int getAge(){

return age;

}

}


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


하위 클래스

class  Child extends Parent //하위 클래스

{

String name = "조현II";

String tel = "010-9142-1348";

String getName(){ //overiding

return "["+name+"]";

}

void prn(){

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

}

}


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

메인
class  InheritanceTest
{
public static void main(String[] args) 
{
//상속 관계에서 객체 형변환
Child c1 = new Child();
System.out.println("이름 = "+c1.getName());
c1.prn();
//클래스의 형변환
//크래스의 형변환은 상속관계에서 하위 클래스 객체를 상위 클래스로 형변환 할 수 있다.
//상위 클래스에서 하위 클래스로 형변환은 불가능하다.
//하위 클래스에만 있는 메소드는 상위 클래스로 형변환시 하위 클래스의 메소드는 소멸한다.
Parent p1 = (Parent)c1;
System.out.println("이름 = "+p1.getName());
//p1.prn(); //하위 클래스에만 있는 메소드
System.out.println("tel="+c1.tel);
//System.out.println("tel="+p1.tel);
}
}


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

public과 protected  (0) 2016.12.01
package ComFile  (0) 2016.12.01
클래스 상속  (0) 2016.12.01
[객체] 로또번호 생성기  (0) 2016.11.30
생성자메소드에서 다른 생성자메소드 호출하는 법  (0) 2016.11.30
Posted by Hyun CHO
,

부모 클래스

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
,

class OverloadingTest 

{

//오버로딩

//같은 이름의 메소드가 하나의 클래스에서 여러개가 존재한다.

void output(){

System.out.println("output()..");

}

void output(String msg){

System.out.println("output("+msg+")");

}

void output(int n){

System.out.println("output("+n+")");

}

void output(int a, int b){

System.out.println("output("+a+","+b+")");

}

public static void main(String[] args) 

{

OverloadingTest ot = new OverloadingTest();

ot.output();

ot.output(100, 200);

System.out.println(ot);

}

}



Posted by Hyun CHO
,

import java.util.Scanner;

class  GugudanObject

{

GugudanObject(){}

void start(){

int dan = danInput("단입력");

for(int i=2; i<=9; i++){

print(dan, i);

}

}


//단입력

int danInput(String msg){

Scanner scan = new Scanner(System.in);

System.out.print(msg+" = ");

return scan.nextInt();

}


//계산

int multiple(int dan, int num){

return dan*num;

}


//출력

void print(int dan, int num){

System.out.println(dan+" * "+num+" = "+multiple(dan, num));

}


//메인

public static void main(String[] args) 

{

GugudanObject gu = new GugudanObject();

gu.start();

}

}

/*

생성자메소드에서는 다른메소드 호출하지 않는다.

멤버변수가 없다.

단입력 = __


__ * 2 = __

__ * 3 = __

__ * 4 = __

__ * 5 = __

__ * 6 = __

__ * 7 = __

__ * 8 = __

__ * 9 = __

*/

Posted by Hyun CHO
,

import java.util.Scanner;

class  ClassEx1

{

//int num;

ClassEx1(){

//numberInput();

//allHap();

}


//콘솔에서 값을 입력받는 메소드

int numberInput(){

Scanner scan = new Scanner(System.in);

System.out.print("임의의 정소 입력 = ");

//int num = scan.nextInt();

//return num;

return scan.nextInt();

}

//합

void hap(int num){

int sum = 0;

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

sum += i;

}

numberOutput("합", sum, num);

}

void oddHap(int num){

int sum = 0;

for(int i=1; i<=num; i+=2){

sum += i;

}

numberOutput("홀수의 합",sum, 10);

}

void evenHap(int num){

int sum = 0;

for(int i=2; i<=num; i+=2){

sum += i;

}

numberOutput("짝수의 합", sum, 10);

}

void allHap(){

//hap();

//oddHap();

//evenHap();

}

//출력하는 메소드

void numberOutput(String msg, int result, int num){

System.out.println("1 ~ "+num+"까지의 "+msg+" = "+result);

}

public static void main(String[] args) 

{

/*ClassEx1 ce = new ClassEx1();

ce.numberInput();//입력

ce.hap();

ce.oddHap();

ce.evenHap();

ce.allHap();*/

ClassEx1 ce2 = new ClassEx1();

int a = ce2.numberInput();

ce2.hap(a);

ce2.oddHap(a);

}

}

/*

실행

임의의 정수입력 = ___

1~___까지의 합 = ___

1~___까지의 홀수의 합 = ___

1~___까지의 짝수의 합 = ___

*/

Posted by Hyun CHO
,