변수초기화
package javajungsuck;
public class C8변수초기화 {
int a;//멤버변수 초기화 해주지 않아도 자동 초기화 됨
static int b; //클래스변수
/* 초기화 방법 1.명시적 초기화 =사용
* int a = 0;
* 2.참조변수의 초기화
* Engin e = new Engin(); 객체를 생성해서 넣어줘야됨 null값 넣는 것보다.
* 3. 복잡초기화 static{} 생성자 블록이용 거의 안씀
*/
public static void main(String[] args) {
//지역변수는 꼭 초기화 해야지 에러가 안난다.
//멤버변수(인스턴스변수) 클래스변수는 자동초기화됨
}
}
class p {
void a() {
//int x;//지역변수 초기화 해줘야됨
}
}
오버라이딩
package javajungsuck;
//오버라이딩이란 : 상속받은 조상의 메서드를 자신에 맞게 변경하는 것 (내용 변경)
/* 오버라이딩의 조건
* 1. 선언부가 조상클래스의 메서드와 일치해야한다.
* 선언부란 반환타입, 메서드이름, 매개변수목록
* 2. 접근제어자를 조상클래스보다 좁은 범위로 설정할수 없다.
* 3. 예외는 조상클래스의 메서드보다 많이 선언할수없다.
*/
//1. 오버라이딩
class Point1{
int x;
int y;
String getLocation() {
return "x :"+x+", y :"+y;
}
}
//1. 오버라이딩
class myPoint3d extends Point1{
int z;
//조상의 String getLocation을 오버라이딩
String getLocation() {
return "x :"+x+", y :"+y+", z :"+z;
}
}
//2. 오버라이딩 오브젝트의 to String();
class Do1 extends Object {
int x, y;
public String toString() {
return "x : "+x+", y : "+ y;
}
}
public class C11오버라이딩 {
public static void main(String[] args) {
//1. 오버라이딩
myPoint3d p1 = new myPoint3d();
//2.오버라이딩 toString
Do1 d = new Do1();
p1.x = 1;
p1.z = 2;
p1.y = 3; //지역변수라 초기화 필수
System.out.println(p1.getLocation()); //오버라이딩 된게 쓰인다 x :1, y :3, z :2
System.out.println(d.toString()); //x : 0, y : 0
System.out.println(d);// toString 과 찹조변수 주소값이 같은 결과라 오버라이딩 결과가 같음 x : 0, y : 0
}
}
static import문
package javajungsuck;
//static import문
//static 멤버를 사용할때 클래스 이름을 생략 할 수 있게 해준다.
//사용예시 import static java.lang.math.random;
//왠만하면 꼭 필요할때만 사용하는게 좋다.
import static java.lang.Math.random;
import static java.lang.System.out;
public class C13스태틱임포트문 {
public static void main(String[] args) {
out.println(random());//0.7962488430217309
out.println("스택틱 임포트문 실행중입니다.");//스택틱 임포트문 실행중입니다.
}
}
기본생성자 super this등 복습 -> 잘이해가 안가서 다시 복습해본다
package pracitce;
public class A {
public static void main(String[] args) {
Bo b = new Bo();
Bo b1 = new Bo(3,4,6);
}
}
class Vo {
int x, y;
//기본생성자 정의 super();가 꼭포함 되어야한다. 객체 초기화를 위해
Vo(){//기본생성자
}
Vo(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
// VO를 상속받는 하위클래스 Bo의 생성자 x,y를 초기화하기 위해서
//super(x,y);로 상위클래스를 호출해 객체를 초기화 한다.
class Bo extends Vo{
int z;
Bo(){} //기본생성자 꼭필요
Bo(int x,int y,int z) {
super(x,y);
this.z= z;
}
Bo(int z){
this.z = z;
}
}
'개발공부 2023 ~03~13' 카테고리의 다른 글
접근제어자2(public,private,protected,defalut,) 다형성 ,형변환 (0) | 2023.03.30 |
---|---|
접근제어자(static,abstract,final) (0) | 2023.03.28 |
is-a,has-a(상속,포함관계) ,단일상속, super,super() (0) | 2023.03.27 |
(중복메소드(오버로딩) , 기본생성자 및 this, this()) (0) | 2023.03.26 |
변수와 메서드(인스턴트, 클래스, 참조형 매개변수, 메서드) (0) | 2023.03.26 |