java 수업 DAY 15 // 객체, package

java 수업 DAY 15 // 객체, package

public class Person { private FullName fullName; private int age; public Person(FullName fullName, int age) { this.fullName = fullName; this.age = age; } public FullName getFullName() { return fullName; } public void setFullName(FullName fullName) { this.fullName = fullName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "Person [fullName=" + fullName + ", age=" + age + "]"; } }

public class FullName { private String firstName; private String lastName; public FullName(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String toString() { return "FullName [firstName=" + firstName + ", lastName=" + lastName + "]"; } }

public class TestPerson { public static void main(String[] args) { FullName tom = new FullName("Tom", "Cruise"); Person p = new Person(tom, 15); FullName name = p.getFullName(); System.out.println(name.toString()); System.out.println(p.toString()); } }

출력 :

FullName [firstName=Tom, lastName=Cruise]

Person [fullName=FullName [firstName=Tom, lastName=Cruise], age=15]

public class Book { private String title; private Person author; private int price; public Book(String title, Person author, int price) { this.title = title; this.author = author; this.price = price; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Person getAuthor() { return author; } public void setAuthor(Person author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String toString() { return "Book [title=" + title + ", author=" + author + ", price=" + price + "]"; } }

public class TestBook { public static void main(String[] args) { Book b = new Book("파워자바", new Person(new FullName("인국", "전"), 30), 200000); System.out.println(b.toString()); } }

출력

Book [title=파워자바, author=Person [fullName=FullName [firstName=인국, lastName=전], age=30], price=200000]

2차원 평면의 좌표를 (x,y) 나타낼 수 있는 point클래스를 작성해보세요.

public class Point { private int x; // 필드 private int y; public Point(int x, int y) { // 생성자 this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public String toString() { return "( " + x + ", " + y + ")"; } }

// 2차원 평면상의 중심점 x,y 좌표를 가지는 // 원클래스

public class Circle { private Point center; private int radius; public final double PI = 3.14; public Circle(Point center, int radius) { this.center = center; this.radius = radius; } public Point getCenter() { return center; } public void setCenter(Point center) { this.center = center; } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; } public double getPI() { return PI; } public String toString() { return "Circle [center=" + center + ", radius=" + radius + "]"; } }

public class Test { public static void main(String[] args) { Point p1 = new Point(10, 25); // 중심점 (10,25) 반지름 5인 원 Circle c = new Circle(p1, 5); System.out.println(c.toString()); // 중심점 (-3, 9)인 반지름 7인 원 Circle c2 = new Circle(new Point(-3, 9), 7); System.out.println(c2.toString()); } }

출력

Circle [center=( 10, 25), radius=5]

Circle [center=( -3, 9), radius=7]

// 학생 클래스

// 점수

//------

// 시험치기

// 학생 클래스 // 점수 //------ // 시험치기 import java.util.Random; // ctrl shift o public class Student { private int score; private Random random; public Student() { random = new Random(); // 쓰지 않으면 null로 초기화로 됨 // test에서 랜덤을 가져올때 Student에 아무것도 없으면 // null을 가져오게 되면서 오류가 난다 } public void test() { score = random.nextInt(101); // 랜덤으로 숫자를 만든다 // 위에 random 이 있기때문에 가지고 오면됨 // 이 안에 radom = new Random 이면 한번 쓰고 버리는 것 } public int getScore() { return score; } }

public class TestStudent { public static void main(String[] args) { Student s = new Student(); System.out.println(s.getScore()); // 0으로 초기화 s.test(); // 시험을 치라고 호출 System.out.println(s.getScore()); } }

출력

0

랜덤 값

// 학급

// 학생이 3명

public class ClassRoom { private Student student1; private Student student2; private Student student3; public ClassRoom(Student student1, Student student2, Student student3) { this.student1 = student1; this.student2 = student2; this.student3 = student3; } // 3명의 학생들에게 시험을 치라고 할 수 있음 private void sayTestAll() { student1.test(); student2.test(); student3.test(); } private void printAll() { // 여기서만 쓸 수있음 int score1 = student1.getScore(); int score2 = student2.getScore(); int score3 = student3.getScore(); int sum = score1 + score2 + score3; int avg = sum / 3; System.out.println("1번 학생: " + score1); System.out.println("2번 학생: " + score2); System.out.println("3번 학생: " + score3); System.out.println("펑균: " + avg); } public void testAndPrint() { // 버튼을 만드는 것 sayTestAll(); printAll(); } }

public class TestStudent { public static void main(String[] args) { ClassRoom c = new ClassRoom( new Student(), new Student(), new Student()); // c.sayTestAll(); // 순서가 중요함 / classRoom에서 private를 붙이면 여기서 사용 불가 - // c.printAll(); // 이걸 합쳐서 classRoom에 적어서 한번에 가능 c.testAndPrint(); // 이걸 버튼 누른다고 생각하면 됨 } }

파일 정리할 때 영상끼리 모으고 그림은 그림끼리 정리하는 해놓은 파일을 package 라고 분린다

즉 package는 분류 폴더를 만드는 것

package 이름은 그 안에 들어 있는것을 설명할 수 있는 것으로 한다

파일로 만들어진다

package main; import first.FirstClass; // 알려주는 것 first 패키지에 FirstClass를 가져올것이다 public class MyMain { public static void main(String[] args) { FirstClass f = new FirstClass(); } }

패키지 안에 패키지는 ' . ' 을 써서 구분한다

접근제한자

package first; public class FirstClass { private int a; // 필드 int b; // 이것도 접근 제한이 됨 다른 package에서 보이지 않음 같은 package는 보임 //default 접근 제한자 / package 접근 제한자 public int c; // 다른 package에서 보인다 }

다른 package는 public만 볼수있음

같은 package에서는 public과 기본

private은 class 내에서만

package이름은 많이 쓰는 것을 하면 안됨 ex.java.util이런거 안됨

이름은 고유하게 써야함 세계에서 쓰는 유명한거 ㄴㄴ 회사에선 자기 회사로 쓴다

다만 역순으로 ex. com.google.www 처럼 쓴다

지금은 회사가 없으니 편하게

class 앞에 붙는 것도 마찬가지

접근제한자 - class앞 메소드앞 필드앞

from http://hiapprendre.tistory.com/28 by ccl(A) rewrite - 2021-12-18 21:28:00