- 클래스 선언과 객체 생성
public class Cls {
static double radius;
public Cls(){
this.radius = radius;
};
public Cls(double r){
this.radius = r;
}
double getVolume(){
return 4.0 / 3.0 * Math.PI * Math.pow(radius, 3);
}
double getArea(){
return 4 * Math.PI * radius * radius;
}
public static void main(String[] args) {
Cls ball = new Cls(2.0);
Cls ball2 = new Cls();
System.out.println("부피: " + ball2.getVolume());
System.out.println("표면적: " + ball2.getArea());
}
}
public이 붙은 클래스는 소스 파일 이름과 동일해야 한다.(대소문자까지!)
- 기초, 참조 타입의 차이
import com.sun.security.jgss.GSSUtil;
class Ball {
double radius;
public Ball(double r){
radius = r;
}
public void setRadius(double r){
radius = r;
}
}
public class Prim {
public static void main(String[] args) {
int a = 10;
int b = a; // 기본 값 복사
b = 20; // b값 변경
System.out.println("a의 값: " + a); // 10
System.out.println("b의 값: " + b); // 20
Ball myBall = new Ball(4.0);
Ball yourBall = myBall; // 참조 타입 값(주소) 복사
Ball otherBall = yourBall; // 참조 타입 값(주소) 복사2
yourBall.setRadius(5.0); // yourBall로 객체값 변경
otherBall.setRadius(6.0); // otherBall로 객체값 변경
System.out.println("myBall의 반지름: " + myBall.radius); // 6.0
System.out.println("yourBall의 반지름: " + yourBall.radius); // 6.0
System.out.println("otherBall의 반지름: " + otherBall.radius); // 6.0
}
}
- 필드와 지역변수 차이
public class Demo {
int field;
public void showDiff(){
int field = 7;
this.field = field; // Copy
System.out.println("클래스 필드: " + this.field);
System.out.println("메서드 지역변수: " + field);
}
public void changeField(int field){
this.field = field;
}
public static void main(String[] args) {
Demo demoInstance = new Demo();
demoInstance.showDiff(); // 7, 7
demoInstance.changeField(50);
System.out.println("변경된 클래스 필드: " + demoInstance.field); // 50
}
}
- 접근자, 설정자
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Person person = new Person("rabat");
System.out.println("이름: " + person.getName()); // rabat
person.setName("rabbit");
System.out.println("변경된 이름: " + person.getName()); // rabbit
}
}
- 생성자 오버로딩 예제
class Rect{
private double w;
private double h;
public Rect(){
this(1.0,1.0);
}
public Rect(double w){
this(w,1.0);
}
public Rect(double width, double height){
this.w = width;
this.h = height;
}
public void printSize(){
this.printSize("이름없음");
}
public void printSize(String name){
System.out.println(name + "님이 요청한 결과입니다.");
System.out.println("너비: " + w + ", 높이: " + h);
}
}
public class RectTest {
public static void main(String[] args) {
Rect r1 = new Rect();
Rect r2 = new Rect(2.0, 3.0);
r1.printSize("홍길동");
r2.printSize();
}
}
- 연속 호출
public class Person1 {
private String name;
private int age;
public Person1(){}
public static Person1 getInstance(){
return new Person1();
}
public Person1 setName(String name){
this.name = name;
return this;
}
public Integer setAge(int age){
this.age = age;
return this.age;
}
public void Hello(){
System.out.println("안녕, 나는 " + name + "이고 " + age + "살이야. ");
}
public static void main(String[] args) {
Person1 person = getInstance();
System.out.println(person.setAge(21)); // 21
person.setName("민규").Hello(); // 안녕, 나는 민규이고 21살이야.
person.Hello(); // 안녕, 나는 민규이고 21살이야.
}
}
- 인스턴스(객체) 멤버와 static 멤버
static은 일반객체에 접근할 수 없으나 일반객체는 static에 접근할 수 있음! (static 맴버가 먼저 생성됌)
객체 = instance
class Calc {
private double res;
static final double PI = 3.14159;
public double area(double rad){
res = PI * rad * rad;
return res;
}
public static double circ(double rad){ // res에 접근 못함
return 2 * PI * rad;
}
}
public class CalcTest {
public static void main(String[] args){
Calc calc1 = new Calc();
double a = calc1.circ(10);
double c = Calc.circ(10);
System.out.println("넓이: " + a);
System.out.println("둘레: " + c);
}
}
'KT AIVLE School > JAVA' 카테고리의 다른 글
예외, 컬렉션 (1) | 2024.11.29 |
---|---|
상속, 추상화, 제네릭 (0) | 2024.11.29 |
자바 시작 ( 자바 변수 및 제어문 ) (0) | 2024.11.26 |