본문 바로가기

Programming/Java27

Troubleshoot #1 No enclosing instance of type ... is accessible 생성하는 자바 파일의 public class 이름을 Foo라고 합시다. 여러 클래스를 혼용하여 사용할 시 발생하는 에러가 있습니다. No enclosing instance of type Foo is accessible. Must qualify the allocation with an enclosing instance of type Foo (e.g. x.new A() where x is an instance of Foo). 발생하는 예시입니다. import java.util.*; public class Foo { public static void main(String[] args) { tester t = new tester(); //위의 에러 발생 } class tester { //빈 클래스 } } 발생 .. 2020. 6. 11.
5장. 상속 #2 업/다운캐스팅 업다운캐스팅 업캐스팅(upcasting) : 서브클래스 객체를 슈퍼클래스 타입으로 변환 ex) class Person {} class Student extends Person {} Person p = new Student(); 업캐스팅 cf) Student s = new Student(); Person p = s; 레퍼런스만 치환해주면 됨. 업캐스팅된 s는 슈퍼클래스의 멤버만 접근 가능하다. 2. 다운캐스팅(downcasting) : 슈퍼클래스 객체를 서브클래스 타입으로 변환 ex) class Person{} class Student extends Person{} Person p = new Person(); Student s = (Student)p; //캐스팅 필요 3. instanceof 연산자 - i.. 2020. 6. 11.
5장. 상속 #1 슈퍼/서브클래스 상속 1. 상속 -> 슈퍼클래스의 멤버들을 서브클래스가 가져오는 것! -형식 public class Student extends Person {}; -방향 : 서브클래스가 슈퍼클래스를 접근 -슈퍼클래스는 여러 새끼 까는 것 가능하지만 서브클래스는 여러 부모를 두는 것이 불가능하다. cf) 상속의 최상위 슈퍼클래스는 java.lang.Object클래스. 모든 클래스가 상속받는다. Object 클래스 : java.lang 패키지에 속한 클래스이며 모든 클래스는 강제로 Object를 상속받는다. 아무 클래스도 상속받지 않는 최상위 클래스이다. Object 클래스의 주요 메소드 getClass() : 현 객체의 런타임 클래스 리턴 boolean equals(Object obj) : obj가 가리키는 객체와 같으.. 2020. 6. 11.
4장. 클래스와 객체 #5 접근지정자 #6 static과 non-static 접근지정자 1. 패키지 : 관련 있는 클래스 파일을 저장하는 디렉토리(.class) 2. 접근 지정자 : 클래스나 일부 멤버를 보호 private 같은 클래스 default 같은 패키지 protected 같은 패키지에서 상속관계까지 public 전부 3. 클래스의 접근지정자 -다른 클래스에서의 사용 여부를 결정 4. 멤버의 접근지정자 -private 클래스 안에 public멤버가 있는 경우 같은 클래스 내에서만 접근이 가능하다. inner클래스의 경우만 사용하는 방식이다. https://stackoverflow.com/questions/7777256/private-class-with-public-method https://hashcode.co.kr/questions/6441/private%EC%9C%BC.. 2020. 6. 10.
4장. 클래스와 객체 #3 객체 배열 #4 메소드 객체 배열 객체 배열 : 배열의 각 원소가 배열을 가리키는 레퍼런스이다. ex) class Circle { int radius; Circle(int radius) { this.radius = radius; } public double area() {return radius*radius*3.14;} } public class CircleArray{ public static void main(String[] args) { Circle[] c = new Circle[5] // 다섯 개의 null pointer를 원소로 가진 배열 for (int i=0; i 2020. 6. 10.
4장. 클래스와 객체 #2 this와 this() 1. this 레퍼런스 : 객체 자신에 대한 레퍼런스 -사용법 : this.멤버 (메소드 안에서 사용된다) -사용시기 : *객체의 필드와 메소드의 매개변수가 이름이 같은 경우 *다른 메소드 호출 시 '객체 자신의 레퍼런스'를 전달할 때 *메소드가 '객체 자신의 레퍼런스'를 반환할 때 2. this() -반드시 생성자 코드의 제일 처음에 수행 -생성자 내에서만 사용 가능 -클래스 내의 다른 생성자 호출 3. 객체의 "="은 다중 레퍼런스가 된다. ex) package Classstudy; public class Book { String title; String author; void show() {System.out.println(title+" "+author);} public Book() { this(".. 2020. 6. 10.