//BOOK.java
public class Book {
String title;
String author;
public Book(String t) {
title = t;
author = "작자 미상";
}
public Book(String t,String a) {
title = t;
author = a;
}
public static void main(String[] args) {
Book littlePrince = new Book("어린왕자", "생텍쥐베리");
Book loveStory = new Book("춘향전");
System.out.println(littlePrince.title +" "+ littlePrince.author);
System.out.println(loveStory.title +" "+ loveStory.author);
}
}
/*
public class Book {
String title;
String author;
void publicBook(String title) {
//this(title,"작자미상");
this.title=title;
this.author="작자미상";
}
void publicBook(String title, String author) {
this.title=title;
this.author= author;
}
*/
//Box10.java
public class Book {
String title;
String author;
public Book(String t) {
title = t;
author = "작자 미상";
}
public Book(String t,String a) {
title = t;
author = a;
}
public static void main(String[] args) {
Book littlePrince = new Book("어린왕자", "생텍쥐베리");
Book loveStory = new Book("춘향전");
System.out.println(littlePrince.title +" "+ littlePrince.author);
System.out.println(loveStory.title +" "+ loveStory.author);
}
}
/*
public class Book {
String title;
String author;
void publicBook(String title) {
//this(title,"작자미상");
this.title=title;
this.author="작자미상";
}
void publicBook(String title, String author) {
this.title=title;
this.author= author;
}
*/
//Box10Test.java
public class Box10Test1 {
public static void main(String[] args) {
Box10 mybox1;
for(int i=1 ; i <= 5 ; i++) {
mybox1 = new Box10(i,i+1,i+2);
System.out.println(mybox1.getvolume());
}
System.out.println("마지막 생성된 박스 번호는 "+ Box10.getCurrentID() + "번입니다");
// System.out.println(Box10.boxID); //오류 ==> 수정 하기?
}
}
//Box7.java
class Box7{
int width;
int height;
int depth;
public Box7(){
this(1,1,1); //<--this를이용해3개매개변수생성자(다른생성자)를호출한다
System.out.println("매개변수없는생성자수행"); }
public Box7(int width){
this(width,1,1);
System.out.println("매개변수(1개) 생성자수행");}
public Box7(int width, int height){
this(width,height,1);
System.out.println("매개변수(2개) 생성자수행");}
public Box7(int width, int height, int depth){
System.out.println("매개변수(3개) 생성자수행");
this.width = width; //객체의속성에매개변수값배정
this.height = height; //객체의속성에매개변수값배정
this.depth = depth; //객체의속성에매개변수값배정
}
}
public class Box7Test1{
public static void main(String[] args) {
Box7 mybox1 = new Box7();
int vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("박스의부피(매개변수없음) : " + vol);
System.out.println("======================");
mybox1 = new Box7(10);
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("박스의부피(매개변수1개) : " + vol);
System.out.println("======================");
mybox1 = new Box7(10,20);
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("박스의부피(매개변수2개) : " + vol);
System.out.println("======================");
mybox1 = new Box7(10,20,30);
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("박스의부피(매개변수3개) : " + vol);
}
}
class Circle {
static double PI=3.14;
int r;
public Circle(int r) {
this.r= r;
}
public double getArea() {
return this.r* this.r* PI;//PI = 3.14
}
public static void main(String[] args) {
Circle c=new Circle(3);
System.out.println(c.getArea());
}
}
/*
abstract class Rabbit {
abstract void sleep();
}
class HouseRabbit extends Rabbit {
void sleep() {
System.out.println("집토끼가 우리에서 잠자고 있습니다.");
}
}
class MountainRabbit extends Rabbit {
void sleep() {
System.out.println("산토끼가 굴속에서 잠자고 있습니다.");
}
}
public class Code08_08 {
public static void main(String[] args) {
HouseRabbit hRabbit= new HouseRabbit();
MountainRabbit mRabbit= new MountainRabbit();
hRabbit.sleep();
mRabbit.sleep();
}
}
*/
interface Rabbit {
abstract void sleep();
}
class HouseRabbit implements Rabbit {
public void sleep() {
System.out.println("집토끼가 우리에서 잠자고 있습니다.");
}
}
class MountainRabbit implements Rabbit {
public void sleep() {
System.out.println("산토끼가 굴속에서 잠자고 있습니다.");
}
}
public class Code08_09 {
public static void main(String[] args) {
HouseRabbit hRabbit= new HouseRabbit();
MountainRabbit mRabbit= new MountainRabbit();
hRabbit.sleep();
mRabbit.sleep();
}
}
//빈칸 채우기 문제
abstract class DD {abstract public int add(int a, int b);}
class C extends DD{
public int add(int x, int y) {return x+y;}
public void show() {
System.out.println("");
}
}
public class Ex1 {
public static void main(String[] args) {
C c=new C();
c.show();
System.out.println(c.add(5,10));
}
}
interface Greeting{
void sayHello();
}
public class GreetingEx {
public static void main(String[] args) {
//Hello_Greetig greeting=new Hello_Greeting() {
Greeting greeting = new Greeting(){
public void sayHello() {
System.out.println("Hello Everybody");
}
};
greeting.sayHello();
}
}
/*람다식으로 변환
interface Greeting{
void sayHello();
}
public class GreetingEx {
public static void main(String[] args){
Greeting greeting = ();
Systme.out.println("Hello Everybody");
greeting.sayHello();
}
}
*/
/*----------------------------------------------------------------------------------------
interface Greeting{
void sayHello();
}
class Hello_Greeting implements Greeting{
@Override
public void sayHello() {
System.out.println("Hello Everybody");
}
}
public class GreetingEx {
public static void main(String[] args) {
//Hello_Greetig greeting=new Hello_Greeting();
Hello_Greeting greeting = new Hello_Greeting();
greeting.sayHello();
}
}
*/
interface PhoneInterface {
final int TIMEOUT = 10000;
void sendCall();
void receiveCall();
default void printLogo() { // default 메소드
System.out.println("** Phone **");
}
}
interface MobilePhoneInterface extends PhoneInterface {
void sendSMS();
void receiveSMS();
}
interface MP3Interface { // 인터페이스 선언
public void play();
public void stop();
}
class PDA { // 클래스 작성
public int calculate(int x, int y) {
return x + y;
}
}
class SmartPhone extends PDA implements MobilePhoneInterface, MP3Interface {
// MobilePhoneInterface의 추상메소드 구현
@Override
public void sendCall() {
System.out.println("따르릉따르릉~~");
}
@Override
public void receiveCall() {
System.out.println("전화왔어요.");
}
@Override
public void sendSMS() {
System.out.println("문자갑니다.");
}
@Override
public void receiveSMS() {
System.out.println("문자왔어요.");
}
// MP3Interface의 추상메소드 구현
@Override
public void play() {
System.out.println("음악연주합니다.");
}
@Override
public void stop() {
System.out.println("음악중단합니다.");
}
// 추가로 작성한 메소드
public void schedule() {
System.out.println("일정관리합니다.");
}
}
public class InterfaceEx {
public static void main(String[] args) {
SmartPhone phone = new SmartPhone(); // 클래스명, 변수명 오타 수정
phone.printLogo(); // 메소드 이름 오타 수정
phone.sendCall();
phone.play();
System.out.println("3과 5를 더하면 " + phone.calculate(3, 5)); // calculate로 변경
phone.schedule(); // 메소드 이름 오타 수정
}
}
class CSuper {
public double x;
}
class CSub extends CSuper{
public double x;
public CSub (double new_x) {
this.x=new_x; //10
super.x=new_x*10; //100
}
public double getSuper() {
return super.x;
}
public double getSub() {
return this.x;
}
}
public class Main03 {
public static void main(String[] args) {
CSub sub= new CSub(10.0);
System.out.println(sub.x);
System.out.println(sub.getSuper());
System.out.println(sub.getSub());
}
}
/*interface MyInterface{
public void method();
}
class Inf implements MyInterface {
@Override
public void method() {
System.out.println("aaa");
}
} ;
public class Main04 {
public static void main(String[] args) {
MyInterface sub=()-> {
System.out.println("sub1");
sub.method();
}
}
*/
abstract class Animal {
public abstract void sound();
public void sleep() {
System.out.println("Sleeping...");
}
}
// 정식 클래스 상속 예시 (사용하진 않지만 오류는 수정)
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Woof Woof");
}
}
public class Main05 {
public static void main(String[] args) {
// 익명 내부 클래스 형태로 Animal 구현
Animal dog = new Animal() {
@Override
public void sound() {
System.out.println("Woof Woof");
}
};
dog.sound(); // 대소문자 수정
dog.sleep(); // 대소문자 수정
}
}
class MyThread04 extends Thread{
@Override
public void run() {
System.out.println(getName());
}
}
public class Main10 {
public static void main(String[] args) {
MyThread04 my_thread= new MyThread04();
my_thread.start();
}
}
class AA {
public void func() { System.out.println("aa");
}
}
class BB extends AA {
public void func() { System.out.println("bb");
}
}
class CC extends BB {
public void func() { System.out.println("cc");
}
}
public class MainAA {
public static void main(String[] args) {
AA aa =new BB();
//BBaa=new BB();
aa.func();
aa=new CC();
aa.func();
}
}
/*MainRabbit클래스*/
/*
public class MainRabbit {
public static void main(String[] args) {
Rabbit rabbit1=new Rabbit();
Rabbit rabbit2=new Rabbit();
Rabbit rabbit3=new Rabbit();
//속성값대입: 객체명.속성명
rabbit1.shape="원";
rabbit1.shape="삼각형";
rabbit1.shape="토끼";
}
}
*/
public class MethodOverloading {
MethodOverloading a=new MethodOverloading();
public int getsum(int i,int j) {
return i+j;
}
public int getsum(int i,int j,int k) {
return i+j+k;
}
public double getsum(double i,double j) {
return i+j;
}
public static void main(String[] args) {
MethodOverloading a=new MethodOverloading();
}
int i=a.getsum(1, 2);
int j=a.getsum(1, 2, 3);
double k=a.getsum(1.1, 2.2);
}
class Shape {
public double getArea(double h,double w) {
return h*w;
}
}
class Triangle extends Shape {
public double getArea(double h,double w) {
return h*w*0.5;
}
}//얘가 shape를 오버라이딩함
public class OverridingTest {
public static void main(String[] args) {
//Triangle t= new Triangle();
Shape t= new Triangle(); // 업캐스팅
//Triangle t= new Shape(); //오류
System.out.println(t.getArea(3.0, 4.0)); // 3.0*4.0*0.5 =
//6.0 객체의 유형에 맞게 메소드 실행 됨
}
}
/*
public class Rabbit{
String shape="";
int xpos;
int ypos;
void setPosition(int x, int y){
int xpos;
int ypos;
}
void printInfo(){
System.out.println("모양:"+shape+", 위치:("+xpos+","+ypos+")");
}
}
*/
public class Sample01 {
int count=10;
static int num=20;
public int sum(int x,int y) {
return x+y;
}
public int mul(int x,int y) {
return x*y;
}
public static void main(String[] args) {
Sample01 s1=new Sample01();
int same=s1.count;
System.out.println(same);
same=s1.num;
System.out.println(same);
same=s1.sum(5,5);
System.out.println(same);
same=s1.mul(5,5);
System.out.println(same);
}
}
public class Sample02 {
public static void main(String[] args) {
String s1=args[0];
String s2=args[1];
System.out.println("첫 번째 매개변수 값="+s1);
System.out.println("두 번째 매개변수 값="+s2);
System.out.println("두 매개변수 합="+(Integer.parseInt(args[0])+Integer.parseInt(args[1])));
}
}
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("안녕하세요, 제 이름은 " + name + "이고 나이는 " + age + "살입니다.");
}
}
class D1 {
int x = 1000;
void display() {
System.out.println("상위 클래스 D1의 display() 메소드입니다"); }
}
class D2 extends D1 {
int x = 2000;
void display() {
System.out.println("하위 클래스 D2의 display() 메소드입니다"); }
void write() { display();
super.display();
System.out.println("D2 클래스 객체의 x 값은 : " + x);
System.out.println("D1 클래스 객체의 x 값은 : " + super.x); }
}
public class SuperTest2 {
public static void main(String[] args) {
D2 d = new D2();
d.write(); }
}
class SD1 {
public int i1;
public double d1;
public SD1(int i1) {
System.out.println("SD1(int i1) 생성자수행");
this.i1 = i1 * i1 ;
System.out.println(i1 +"의2제곱은: "+this.i1);
}
public SD1(double d1) {
System.out.println("SD1(double d1) 생성자수행");
this.d1 = d1 * d1 ;
System.out.println(d1 +"의2제곱은: "+this.d1);
}
}
class Sub1 extends SD1 {
public Sub1(int i1) {
super(i1);
System.out.println("Sub1(int i1) 생성자수행");
this.i1 = this.i1 * i1 ;
System.out.println(i1 +"의3제곱은: "+this.i1); }
public Sub1(double d1) {
super(d1);
System.out.println("Sub1(double d1) 생성자수행");
this.d1 = this.d1 * d1 ;
System.out.println(d1 +"의3제곱은: "+this.d1); }
}
public class SuperTest3 {
public static void main(String[] args) {
Sub1 sub1= new Sub1(10);
Sub1 sub2 = new Sub1(10.5);
}
}
abstract class Calculator {
public abstract int add(int a, int b);
public abstract int subtract(int a, int b);
public abstract double average(int[] a);
}
class GoodCalc extends Calculator {
public int add(int a, int b) {return a + b;}
public int subtract(int a, int b) {return a * b; }
public double average(int[] a) {
int sum = 0;
for (int i : a) {
sum += i;
}
return (double) sum / a.length;
}
}
public class Task {
public static void main(String[] args) {
GoodCalc c = new GoodCalc();
System.out.println("add: " + c.add(2, 2));
System.out.println("subtract: " + c.subtract(2, 2));
System.out.println("average: " + c.average(new int[]{5, 4}));
}
}
20250612 일단 이거 위주로 공부
자바 기본 제공 패키지 - 아래 내용만 시험
뭐 찾고 그러는거는 시험에 안나옴
➢java.lang – 기본 클래스 (String, Math, System 등)
➢java.util – 유틸리티 클래스 (ArrayList, HashMap 등)
➢java.io – 입출력 관련 (File, InputStream 등)
➢java.net – 네트워크 관련 클래스
➢javax.servlet – 서블릿 관련 클래스 (웹 프로그래밍)
------
기본 제공 패키지
java.lang
thread 는 임포트 하지 않아도 사용가능?
스캐너를 사용하기 위해 임포트를 어떻게 해야하나
패키지 먼저
ㄴ그 다음 임포트문 작성
1. 사용자 패키지
2. 기본 패키지
java.lang – 기본 클래스 (String, Math, System 등) 기본 임포트
기본생성자
생성자 선언 매개변수
객체 초기화
오버로딩
ㄴ메소드 오버로딩의 조건
ㄴ메소드 오버로딩(같은 이름, 다른 매개변수)
ㄴ반환형에 상관 없이, 매개변수가 동일하면 메서오버로딩 X
생성자 오버로딩
this()코드
final, abstact, synchronized, static 메소드
예제문제 다음 클래스의 저의로부터 객체가 생성될 때 이 객체가 가지는 필드 nValue의 초기값은?
개념어
인스턴스멤버
this
정적 멤버
final
상수
부모 클래스 Super와 자식 클래스 Sub가 존재하고 각각의 메소드 f()가 정의되어 있다. 다음에서 어떤f()가 호출되는가?
Super x = new Sub();
x.f();
ㄴ답 : Sub() 클래스
자바의 인터페이스
추상클래스의 추상메소드 재정의 (@Override 사용)
ㄴ원래는 추상클래스 가지고 메소드를 선언할 수 없어서 업캐스팅해줌
추상 클래스의 상속과 구현 : 다음 결과를 유추하면?
인터페이스의 구현 InterfaceEx1 - 유사
dlraud zmffotmfh
익명 클래스로 객체 생성하기
ㄴ익명 서브 클래스 정의하기
<슈퍼클래스> <익명객체변수>=new <슈퍼 클래스>()
예제) 인터페이스 구현하기
Greeting
익명 클래스를 람다식으로**
제너릭을 뺐다?
스레드 동기화(간섭과 동기화)
+키티
+익명클래스
'computing' 카테고리의 다른 글
20250904_안드로이드_1 (0) | 2025.09.04 |
---|---|
20250901 - iOS_1 (0) | 2025.09.01 |
20250522java11 (1) | 2025.05.22 |
20250519_DB8 트랜잭션, 동시성 제어 (0) | 2025.05.19 |
20250515java10 (0) | 2025.05.15 |