Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

첫번째 세미나 기본과제 예제코드 작성 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions firstSeminar/firstSeminar_basic/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package firstSeminar_basic;
//오버로딩에 대해서 공부하기 위해 두개의 매개변수의 개수가 다른 생성자를 작성했습니다.
public class Car {
String color;
String gearType;
int door;
int price;
int doorPrice;

Car(String a,String b, int door){
this.color = a;
this.gearType=b;
this.door=door;
}
Car(int a, int b){
this("안녕안녕","모닝스터디",4);
this.price = a;
this.doorPrice=b;
}
}
10 changes: 10 additions & 0 deletions firstSeminar/firstSeminar_basic/CarMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package firstSeminar_basic;
//오버로딩에 대해서 정리하는 예제입니다.
public class CarMain {
public static void main(String [] args){
Car martiz = new Car("d","k",3);
Car blueCar = new Car(300,500);
System.out.println(martiz.color+martiz.gearType+martiz.door);
System.out.println(blueCar.color+blueCar.gearType+blueCar.door);
}
}
36 changes: 36 additions & 0 deletions firstSeminar/firstSeminar_basic/OCP_Practice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package firstSeminar_basic;
/*
ocp가 어떤 개념인지 학습하는 예제입니다.
*/
class HelloAnimal{
void hello(Animal animal){
animal.speak();
}
}
abstract class Animal{
abstract void speak();
}

class Cat extends Animal{
void speak(){
System.out.println("냥냥");
}
}
class Dog extends Animal{
void speak(){
System.out.println("아침 일어나기 성공!");
}
}

public class OCP_Practice {
public static void main(String[] args){
HelloAnimal hello = new HelloAnimal();

Animal cat = new Cat();
Animal dog = new Dog();

hello.hello(cat); //냥냥
hello.hello(dog); //아침 일어나기 성공!

}
}
34 changes: 34 additions & 0 deletions firstSeminar/firstSeminar_basic/PointTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package firstSeminar_basic;

/*
super()를 다루는 예제입니다.
*/
public class PointTest {
public static void main(String[] args){
Point3D p3 = new Point3D(1,3,2);
}

}
class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
String getLocation(){
return "x :"+x+", y :"+y;
}

}
class Point3D extends Point{
int z;
Point3D(int x, int y, int z){
super(x,y); //여기 위치에 super(x,y)를 넣지 않으면 에러발생.
this.x=x;
this.y=y;
this.z=z;
}
String getLocation(){
return "X :"+ x+ ", y:"+y+ ", z :"+z;
}
}
9 changes: 9 additions & 0 deletions firstSeminar/firstSeminar_basic/liscope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package firstSeminar_basic;
//liscope 원칙이 무엇인지 알아보는 예제입니다. 현재 클래스는 드라이버 클래스입니다.

public class liscope {
public static void main(String[] args){
square sq = new square(5);
System.out.println(sq.getArea());
}
}
27 changes: 27 additions & 0 deletions firstSeminar/firstSeminar_basic/rect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package firstSeminar_basic;
//리스코프 치환원칙을 정리하기 위한 예제입니다. 둘의 공통점을 shape라는 인터페이스로 빼서 둘의 관계를 정리하여 의존성을 떨어뜨리는 것을 볼 수 있습니다.
//그 중 이 rect.java는 직사각형을 의미하는 클래스입니다.
import firstSeminar_basic.shape;

public class rect implements shape {
private int h;
private int w;

rect(int h, int w) {
this.h = h;
this.w = w;
}

@Override
public int getArea() {
return h * w;
}

public void setH(int h) {
this.h = h;
}

public void setW(int w) {
this.w = w;
}
}
5 changes: 5 additions & 0 deletions firstSeminar/firstSeminar_basic/shape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package firstSeminar_basic;
//공통점을 shape라는 인터페이스로 뽑아내서 리스코프 치환원칙을 지키도록 만들어주었습니다.
public interface shape {
public int getArea();
}
19 changes: 19 additions & 0 deletions firstSeminar/firstSeminar_basic/square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package firstSeminar_basic;
//정사각형을 담당하는 square.java클래스입니다. 공통점인 shape인터페이스를 받고있습니다.
public class square implements shape {
private int h;
private int w;

square(int h){
this.h = h;
this.w = h;
}
public void setWH(int h){
this.h=h;
this.w=h;
}
@Override
public int getArea(){
return h*w;
}
}