본문 바로가기
computing

20250515java10

by greentworkshop 2025. 5. 15.

 

이론

메소드 오버로딩

 오버로딩 (Overloading) :

- 한 클래스 내에서 두 개 이상의 이름이 같은 메소드 작성

- 메소드 이름이 동일하여야 함

- 매개 변수의 개수가 서로 다르거나, 타입이 서로 달라야 함

- 리턴 타입은 오버로딩과 관련 없음 • 매개변수 타입에 따라 또는 매개변수 개수에 따라 자동적으로 구분

 오버라이딩(Overriding)

- 있던 메소드를 새로 고침


객체 소멸 , 가비지 켈렉션

 


this(super) 레퍼런스

 


클래스 메소드

 


final, abstract, synchronized 메소드

 



예제

Circle

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());
	}
 }

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;
	}

 */

Box7

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; //객체의속성에매개변수값배정
}
 }

Box7Test1

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);
 }
 }

Box10

class Box10 {
	private int width; // private 불필요한접근방지용
	private int height;
	private int depth;
	private int vol;
	private long idNum; //일반객체변수
	private static long boxID = 0; //클래스변수
	
	public Box10(int width, int height, int depth) {
	this.width = width;
	this.height = height;
	this.depth = depth;
	idNum = ++boxID; // 생성자에서idNum에고유번호부여
	volume(); }
	
	private void volume() { // 일반메소드, 일반객체변수(++boxID로idNum에고유번호부여) -고유번호와부피반환
		vol = width * height * depth;  }
	public String getvolume() { 
		return idNum +"번 박스의 부피 : "+ vol; 
	}
	public static long getCurrentID() {   
//현재박스번호를반환하는클래스메소드
// return idNum; // 클래스메소드내일반객체변수사용불가
return boxID; // 클래스변수boxID반환
	}
 }

 Box10Test1

 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); //오류 ==> 수정 하기?
 }
 }

Sample01

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); 
	}
}

Sample02

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);
	}
 }
 
 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 
 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])));

	}
 }

 

배열 데이터값 입력창 띄우는 방법

IniTest

 class
 IniTest
 {
 static int nStatic = 0;
 int nValue = 1;
 {
 nValue = 2;
 }
 static {
 nStatic = 3;
 }
 public IniTest() {
 nValue = 3;
 }
 public static void main(String[] args) {
 int nValue
 =10;
 IniTest
 t
 =
 new
 IniTest
 ();
 System.out.println("nValue="+t.nValue );
 }
 }
 
 //nValue == 3

'computing' 카테고리의 다른 글

20250522java11  (1) 2025.05.22
20250519_DB8 트랜잭션, 동시성 제어  (0) 2025.05.19
20250508_java10  (0) 2025.05.08
나는 언제쯤 행복해질까?  (0) 2025.05.08
20250417_java7  (0) 2025.04.17