-
Notifications
You must be signed in to change notification settings - Fork 2
Chapter 06. Objects and Data Structures Part.1
2021.08.21 (SAT) 10:20-12:00 (100mins)
π Lead by. 'Jeongyeol'
μ λ³μλ λΉκ³΅κ°(
private)λ‘ μ μν κΉ?
-> λ³μμ μμ‘΄νμ§ μκ² λ§λ€κ³ μΆμ΄μ
μ μ‘°ν(
get) ν¨μμ μ€μ (set) ν¨μλ₯Ό
λΉμ°νκ² κ³΅κ°(public)λ‘ λΉκ³΅κ° λ³μ(private variable)λ₯Ό μΈλΆμ λ ΈμΆν κΉ?
β¬οΈ Block 6-1: ꡬ체μ μΈ User ν΄λμ€
// in Java
public class User {
public long id;
public String name;
}// in Typescript
class User {
id: number;
name: string;
}
export default User;μ ν΄λμ€λ μ¬μ©μ μ 보λ₯Ό λͺ νν νννκ³ μλ€.
β¬οΈ Block 6-2: μΆμμ μΈ User ν΄λμ€
// in Java
public interface User {
long getId();
String getName();
void setUserInfo(long id, String name);
String getTenuer();
String getJoinDate();
void setJoinDate(int yyyy, int mm, int dd);
}// in Typescript
interface User {
getId(): number;
getName(): string;
setUserInfo(id: number, y: string): void;
getTenuer(): string;
getJoinDate(): string;
setJoinDate(yyyy: number, mm: number, dd: number): void;
}
export default User;μΈν°νμ΄μ€λ μλ£ κ΅¬μ‘°λ₯Ό νννλ κ² μ΄μμΌλ‘ μλ£ κ΅¬μ‘°λ₯Ό μ κ·Όνλ μ μ± κΉμ§ κ°μ νλ€.
-
μκ°μ μ€λ₯ μ§κ³ λμ΄κ°κΈ°:
μ’νμ΄ λ§μ‘±λλ€κ³μ°νμ λ§μ‘±νμ§ μλλ€.-
ν΄λμ€μ λ³μ μ¬μ΄μ ν¨μλΌλ κ³μΈ΅μ λ£λλ€/=ꡬνμ΄ κ°μΆ°μ§λ€ -
Getter/Setterλ‘ λ³μλ₯Ό λ€λ£¬λ€/=μλ£κ΅¬μ‘°λ₯Ό νννλ ν΄λμ€// User ν΄λμ€λ‘ μμ±λ μΈμ€ν΄μ€μ public id λ³μλ₯Ό λ°λ‘ μ κ·Ό const idFromUserAsDataStructrue = user.id; // User ν΄λμ€λ‘ μμ±λ μΈμ€ν΄μ€μ public getId() ν¨μλ₯Ό ν΅ν΄ λ³μλ₯Ό μ κ·Ό const idFromUserAsObject = user.getId(); // return id; => λ΄λΆ μλ£λ₯Ό κ·Έλλ‘ λ ΈμΆνλ κ²½μ°, ꡬνμ΄ κ°μΆ°μ Έ μλ€κ³ 보기 μ΄λ ΅λ€.
// in Typescript class Execution { private a: number; private b: number; constructor(a: number, b: number) { this.a = a; this.b = b; } public plus() { return this.a + this.b; } } const exec = new Execution(1, 2); // exec.a; // error TS2341: Property 'a' is private const result = exec.plus(); console.log(result); // 3
-
-
μ§μ ν μλ―Έμ ν΄λμ€(Class)
- μΆμ μΈν°νμ΄μ€λ₯Ό μ 곡
- μ¬μ©μκ° μ€μ ꡬν λ΄μ©μ λͺ°λΌλ, μλ£μ ν΅μ¬μ μ‘°μν μ μλ€
β¬οΈ Block 6-3: ꡬ체μ μΈ Salary ν΄λμ€
// in Java
public interface Salary {
long getTotalPayments();
double getInsentives();
}// in Typescript
interface Salary {
getTotalPayments(): number;
getInsentives(): number;
}
export default Salary;λ ν¨μκ° λ³μκ°μ μ½μ΄ λ°νν λΏμ΄λΌλ μ¬μ€μ΄ κ±°μ νμ€νλ€
getTotalPayments(): μ 체 μ§κΈμ‘μ΄λΌλ ꡬ체μ μΈ μ«μλ₯Ό μ 곡νλλ‘ λͺ μgetInsentives(): μΈμΌν°λΈ λΉμ¨μ΄λΌλ ꡬ체μ μΈ μ«μ μ 곡νλλ‘ λͺ μ
β¬οΈ Block 6-4: μΆμμ μΈ Salary ν΄λμ€
// in Java
public interface Salary {
long getRatioOfInsentives();
}// in Typescript
interface Salary {
getRatioOfInsentives(): number;
}
export default Salary;ν΄λΉ μ λ³΄κ° μ΄λμ μ€λμ§ μ ν λλ¬λμ§ μλλ‘ μΆμν λμ΄μλ€.
-
κ°μ²΄κ° ν¬ν¨νλ μλ£λ₯Ό ννν κ°μ₯ μ’μ λ°©λ²μ π₯μ¬κ°νκ² κ³ λ―Όν΄μΌ νλ€.π₯
-
μ무 μκ° μμ΄ Getter/Setterλ₯Ό μΆκ°νλ λ°©λ²μ΄ κ°μ₯ λμλ€.
κ°μ²΄λ μΆμν λ€λ‘ μλ£λ₯Ό μ¨κΈ΄ μ±, μλ£λ₯Ό λ€λ£¨λ ν¨μλ§ κ³΅κ°νλ€.
μλ£κ΅¬μ‘°λ μλ£ κ·Έλλ‘λ₯Ό 곡κ°νλ©°, λ³λ€λ₯Έ ν¨μλ₯Ό μ 곡νμ§ μλλ€.μ 2κ°μ§ μλλ λ³Έμ§μ μΌλ‘ μλ°λλ€. (μ¬μ€ μ λ°λμ μλ―Έλ₯Ό κ°μ§λ€.)
β¬οΈ Block 6-5: μ μ°¨μ μΈ λν ν΄λμ€
(μ°Έκ³ ) Javascriptλ λΆλμμμ μ ν¬ν¨νλ μ«μ κ³μ°μ΄ μ·¨μ½νλ―λ‘ μμ λ‘ κ΅¬μ±νμ§ μμ
// in Java
public class Point {
public double x;
public double y;
}
public class Square {
public Point topLeft;
public double side;
}
public class Rectangle {
public Point topLeft;
public double height;
public double width;
}
public class Circle {
public Point center;
public double radius;
}
public class Geometry {
public final double PI = 3.141592653589793;
public double area(Object shape) throws NoSuchShapeException
{
if (shape instanceof Square) {
Square s = (Square)shape;
return s.side * s.side;
}
else if (shape instanceof Square) {
Rectangle r = (Rectangle)shape;
return r.height * r.width;
}
else if (shape instanceof Circle) {
Circle c = (Circle) shape;
return PI * c.radius * c.radius;
}
throw new NoSuchShapeException();
}
}- ν΄λμ€λ₯Ό ν΅ν΄ μ 보λ₯Ό λ΄μ μΈμ€ν΄μ€λ₯Ό
area(Object shape)ν¨μμ μ§μ΄λ£μ΄μ νΈμΆνλ©΄ λ©΄μ μ΄ κ΅¬ν΄μ§λ€.
κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ¨Έκ° μ μ½λλ₯Ό λ³Έλ€λ©΄ μ½μμ μΉ μ§λ λͺ¨λ₯΄μ§λ§, μ μ°¨μ μ΄λΌμ λΉννλ€λ©΄ λ§λ λ§μ΄λ€. - λνμ λλ λ₯Ό ꡬνλ
perimeter(Object shape)ν¨μλ₯Ό μΆκ°λ‘ ꡬννλ€λ©΄?
λλκ²λGeometryν΄λμ€μ μμ‘΄νλ λ€λ₯Έ λν ν΄λμ€λ€μ λ³ν μμ΄ μΆκ°ν μ μλ€. - λ°λλ‘ μλ‘μ΄ λνμ μΆκ°νλ€λ©΄? μλ₯Ό λ€μ΄
Pentagon(μ μ€κ°ν)μ μΆκ°νλ€λ©΄?
λΉμ°νκ²λGeometryν΄λμ€μ μν λͺ¨λ ν¨μλ₯Ό κ³ μ³μΌνλ€.
β¬οΈ Block 6-6: λ€νμ μΈ λν ν΄λμ€
(μ°Έκ³ ) Javascriptλ λΆλμμμ μ ν¬ν¨νλ μ«μ κ³μ°μ΄ μ·¨μ½νλ―λ‘ μμ λ‘ κ΅¬μ±νμ§ μμ
// in Java
public interface Shape {
double area();
}
public class Square implements Shape {
private Point topLeft;
private double side;
public double area() {
return side * side;
}
}
public class Rectangle implements Shape {
private Point topLeft;
private double height;
private double width;
public double area() {
return height * width;
}
}
public class Circle implements Shape {
private final double PI = 3.141592653589793;
private Point center;
private double radius;
public double area() {
return PI * radius * radius;
}
}μ΄μ―€μμ λ€μ νλ² μ½μ΄λ³΄μ.
κ°μ²΄λ μΆμν λ€λ‘ μλ£λ₯Ό μ¨κΈ΄ μ±, μλ£λ₯Ό λ€λ£¨λ ν¨μλ§ κ³΅κ°νλ€.
μλ£κ΅¬μ‘°λ μλ£ κ·Έλλ‘λ₯Ό 곡κ°νλ©°, λ€λ₯Έ ν¨μλ₯Ό μ 곡νμ§ μλλ€.
μ΄λ νκ°? λ 쑰건μ μμ ν λ°λλλ€λ μ¬μ€μ μ΄ν΄ν μ μκ² λκ°?
-
Block 6-5μ μ μ°¨μ μΈ λ°©λ²κ³Ό
Block 6-6μ λ€νμ μΈ λ°©λ²μ
μνΈλ³΄μμ μΈ νΉμ§μ΄ μλ€. (μ¬μ€ λ°λλ€!!) -
κ°μ²΄μ μλ£κ΅¬μ‘°λ κ·Όλ³Έμ μΌλ‘ μλΆλλ€. (μλ λͺ μ λ μ°Έμ΄λ€)
-
μλ£ κ΅¬μ‘°λ₯Ό μ¬μ©νλ μ μ°¨μ μΈ μ½λ
- κΈ°μ‘΄ μλ£ κ΅¬μ‘°λ₯Ό λ³κ²½νμ§ μμΌλ©΄μ μ ν¨μλ₯Ό μΆκ°νκΈ° μ½λ€.
β‘οΈ κΈ°λ₯ ꡬνμ μν νμ₯ - μλ‘μ΄ μλ£ κ΅¬μ‘°λ₯Ό μΆκ°νκΈ° μ΄λ ΅λ€.
β‘οΈ μλ‘μ΄ μλ£κ΅¬μ‘°μ λν μΆκ° κΈ°λ₯ ꡬνμ΄ νμ
- κΈ°μ‘΄ μλ£ κ΅¬μ‘°λ₯Ό λ³κ²½νμ§ μμΌλ©΄μ μ ν¨μλ₯Ό μΆκ°νκΈ° μ½λ€.
-
κ°μ²΄ μ§ν₯ μ½λ
- κΈ°μ‘΄ ν¨μλ₯Ό λ³κ²½νμ§ μμΌλ©΄μ μ ν΄λμ€λ₯Ό μΆκ°νκΈ° μ½λ€.
β‘οΈ λ€νμ±μ ν΅ν νμ₯ - μλ‘μ΄ ν¨μλ₯Ό μΆκ°νκΈ° μ΄λ ΅λ€.
β‘οΈ λ€νμ±μ λν κ°μ νλ‘ λͺ¨λ ν΄λμ€μ λν μμ μ΄ νμ
- κΈ°μ‘΄ ν¨μλ₯Ό λ³κ²½νμ§ μμΌλ©΄μ μ ν΄λμ€λ₯Ό μΆκ°νκΈ° μ½λ€.
-
λ Έλ ¨ν νλ‘κ·Έλλ¨Έλ€μ
κ°μ²΄ μ§ν₯ νλ‘κ·Έλ¨μμ λ°λμ "λͺ¨λ κ²μ΄ κ°μ²΄λ€"λΌλ λ§μλ―Έμμ΄λΌλ κ²μ μ μλ€.
π Prerequisites
π How to use
π Clean Code
π Meaningful Names + Extra
π Functions
π Comments
π Formatting
π Objects and Data Structures
ongoing! π
π Error Handling
π Boundaries
π Unit Tests
π Classes
π Systems
π Emergence
up-to-here! π
π§· Concurrency
π§· JUnit Internals