추상클래스는 객체 생성이 불가능하다
=> 미구현 내용이 존재하여 완전하지 않다.
=> 오버라이딩을 통해 재정의(구현내용 존재) 하면 객체 생성이 가능하다.
// 만들어진 기능을 다른데서 사용할 수 있게 제공하되, 가공이 가능한 형태로 주고 싶을 때 사용
public abstract class Test16A { //추상 클래스
public void test() {
System.out.println("테스트 중입니다.");
}
public abstract void a(); // 추상메소드
}
추상메소드 재정의
//추상클래스는 상속을 통해서만 사용가능
public class Test16 extends Test16A {
@Override
public void a() {
// TODO Auto-generated method stub
}
@Override
public void b() {
// TODO Auto-generated method stub
}
}
넓다
좁다
package com.gd.test.service;
public class TestService1 {
private int a = 10;
protected int b = 7;
}
package com.gd.test.controller;
import com.gd.test.service.TestService1;
public class TS extends TestService1 {
public void t() {
// System.out.println(a); //private 사용 불가
System.out.println(b); //protected 상속관계라서 사용 가능
}
}
package com.gd.test.bean;
public class TestBean {
private int no;
private String name;
private int age;
public int getNo() { //getter는값을 가져올 때
return no;
}
public void setNo(int no) {//setter는 값을 넣을 때
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
실행파일
TestBean tb = new TestBean();
//값을 넣올때 setter
tb.setNo(1);
tb.setName("권재현");
tb.setAge(300);
//값을 가져올 때 getter
System.out.println(tb.getNo()+ " "+ tb.getName()+" "+ tb.getAge());
}
사용예시
float f = 3.541529f;
System.out.println(Math.ceil(f)); // ceil(값) : 올림
System.out.println(Math.round(f)); // round(값) : 반올림
System.out.println(Math.floor(f)); // floor(값) : 버림
//출력 4.0
// 4
// 3.0
int a = -3;
System.out.println(Math.abs(a)); // abs(값) : 절대값
//출력 3
System.out.println(Math.pow(2, 10));
//pow(숫자 1, 숫자2) : 숫자1의 숫자 2승
//출력 1024.0
System.out.println(Math.sqrt(4)); //sqrt(숫자) : 숫자의 루트
//출력 2.0
System.out.println(Math.random());
// random() 0.0이상 1.0미만의 난수 생성
// 15 ~ 25 : 큰수 - 작은수 + 1 => 11
System.out.println(Math.floor(Math.random()*11)+15);
}
//문제 1. 주사위 6면체(1~6), 12면체(1~12), 20(1~20)면체의 굴린 값을 구하시오.
//이따가 bean 으로 처리
int six=(int) Math.floor(Math.random()*6)+1; //주사위 6면체
int twelve=(int) Math.floor(Math.random()*12)+1; //주사위 12면체
int twenty=(int) Math.floor(Math.random()*20)+1; //주사위 20면체
//문제 2. 로또번호 5번 생성. 1번당 6개의 숫자 중복없이..중복체크..
int[] lotto=new int [6]; //로또 번호 담은 배열변수
//비교를 위한
for(int times= 0; times<lotto.length-1; times++) {
lotto[0]=(int) Math.floor(Math.random()*45)+1; // 1번 째 번호
for(int i =1; i<lotto.length; i++) { //2번부터 6번까지 번호 뽑기 횟수
lotto[i]=(int) Math.floor(Math.random()*45)+1; // //2번부터 6번까지 번호 뽑기
for(int j=0; j<i; j++) {
if(lotto[i]==lotto[j]) {
//비교하다 해당차례에서 중복 수가 나오면
//그 차례를 다시 뽑으면 되기 때문에 i--해주면 해결된다.
i--;
}
}
}
System.out.println("로또 "+(times+1)+" 회차 :"+lotto[0]+","+lotto[1]+","
+lotto[2]+","+lotto[3]+","+lotto[4]+","+lotto[5]);
}
드디어 9일만에 문제을 상위권 풀었다. 교육기간 내내 꼴찌 였는데, 다행히도 그간의 노력이 헛되이 안된것 같아 너무 기쁘다.
아직 갈길이 멀었다. 꾸준함을 모든 것을 이긴다.
어제는 절망을 오늘은 희망을 봤다.