본문 바로가기

전체 글384

0929_클래스 구성(연습) public class StudentTest {//---------10 public static void main(String[] args) { //국어: 100, 영어:60, 수학:76 //실행결과 //이름: 홍길동 //총점 : 236 //평균 : 78.7 소수점 2번째 자리에서 반올림 Student stu = new Student("김유림",3,17,100,60,76); int total = stu.getTotal(); double avg = stu.getAverage(); System.out.println("이름 : "+ stu.name); System.out.println("총점 : "+ total); System.out.println("평균 : "+ String.format("%.1f", avg.. 2022. 9. 29.
0929_초기화블럭 사용 목적 : 클래스의 멤버 변수 초기화 인스턴스 변수 - 형식 : {} - 초기화 진행 순서 : 기본값 → 명시적 초기화 → 인스턴스 초기화 블록 → 생성자 1) 객체 생성 public class Main{ public static void main(String[] args) { Member m = new Member(88,99); } } public class Member { int x; int y; ---------- 1. 자동 초기화 static int cv = 100; static int count; { x = 10;---------- 2. 인스턴스 초기화 블럭 y = 20; System.out.println("인스턴스 초기화 블럭"+ x +","+ y); } Member(int x, int y.. 2022. 9. 29.
0929_this. / this() public class Car1 {//0929-4 생성자 //속성 String color; String gearType; int door; //Car1(){ //기본생성자 //} //생성자 오버로딩 //Car1(String c){ //매개변수 c로 들어온 값을 color를 초기화 //color = c; //} //this ----------5 멤버변수와 매개변수의 변수명이 같을 때 Car1(String color){ this.color = color; //this.멤버변수 = 매개변수로 받아온 값의 변수 이름 } Car1(String color,String gearType){ this.color = color; this.gearType = gearType; } Car1(String color,String.. 2022. 9. 29.
0929_생성자 public class Car1Test {//0929-4 생성자 public static void main(String[] args) { //Car1 c1 = new Car1(); // Car1()객체 생성과 동시에 생성자가 호출됨. // ////인스턴스 초기화 방법1 //c1.color = "red"; //c1.gearType = "Auto"; //c1.door = 4; // //System.out.println(c1.color); //System.out.println(c1.gearType); //System.out.println(c1.door); // // //Car1 c2 = new Car1(); //System.out.println(c2.color); //System.out.println(c2.g.. 2022. 9. 29.
[java] 오버로딩 오버로딩이란? 한 클래스 내에 같은 이름의 메서드를 여러개 정의하는 것. 오버로딩 특징? - 자바는 원래 한 클래스 내에 같은 이름의 메소드를 둘 이상 가질 수 없다. 하지만 매개변수의 개수나 타입을 다르게 하면, 하나의 이름으로 메소드를 작성할 수 있다. - 메소드 오버로딩은 서로 다른 시그니처를 갖는 여러 메소드를 같은 이름으로 정의하는 것. - 다형성을 구현하는 방법 중 하나. - 리턴타입과는 무관하다. 오버로딩을 사용하는 이유? 1) 같은 기능을 하는 메서드를 하나의 이름으로 사용할 수 있다. 2) 메서드 이름 절약 가능 * 오버로딩 대표적 함수 : print함수. - println() 메서드는 오버로딩 되어있기 때문에 int, string, boolean 인자 등 모두 받아서 동작한다. - 오버.. 2022. 9. 29.
0929_static method public class FuncStatic { //0929-1 static method (블록내 변수,메소드 : 멤버) int iv; static int cv; //멤버 메소드: 1) 인스턴스 메소드 //- 인스턴스 변수, 클래스 변수 모두 사용 가능 //- 인스턴스 메소드, 클래스 메소드 모두 사용 가능 //2) 클래스(static) 메소드 :객체 생성 없이 사용가능(static 변수랑 동일) //- 클래스 변수, 클래스 메소드만 사용가능 void instanceMethod() { //메소드 메모리 할당 시점보다 static 변수인 cv 생성이 더 빠름 //인스턴스 변수는 생성해야 메모리 할당이 됨. System.out.println("instanceMethod() call"); iv = 10; //생.. 2022. 9. 29.