자바(Java) 기초 강의 #10 생성자 & Getter Setter - 쉽게 자바 코딩하는 방법 (현직 개발자 설명) , Java / android / Java tutorial
출처:youtube hongdroid홍드로이드
Alt + insert Constructor
public class Main { // public Main () {} // default 생성자 public static void main(String[] args) { } }
인스턴스 적용
public class User { public static void main(String[] args) { Main main = new Main("홍승엽", 27,"프로그래밍"); } }
Alt + insert Getter and Setter
public String getName() { return name; } // Getter public void setName(String name) { this.name = name; } // Setter
public class Main { String name; // 이름 public Main() {} // default 생성자 } public static void main(String[] args) { Main main = new Main(); // default 생성자 필요 main.setName("홍승엽"); System.out.println(main.getName()); System.out.println(main.name()); }
접근제어자가 private인 경우 Getter와 Setter를 활용하여 접근
&, |: 비트연산자의 경우 뒤쪽까지 모두 실행
abstract class Bird { // 추상클래스 private int x, y, z; void fly(int x, int y, int z) { printlocation(); System.out.println("이동합니다."); this.x = x; this.y = y; if(flyable(z)) { this.z = z; } else { System.out.println("그 높이로는 날 수 없습니다."); } printlocation(); } abstract boolean flyable(int z); public void printlocation() { System.out.println("현재위치 (" + x + ", " + y + ", " + z + ")"); } } class Pigeon extends Bird { // 각 클래스에서 flyable z 포함 @Override boolean flyable(int z) { return z < 10000; } } class Peacock extends Bird { @Override boolean flyable(int z) { return false; } } public class Main { public static void main(String[] args) { Bird pigeon = new Pigeon(); Bird Peacock = new Peacock(); System.out.println("---비둘기---"); pigeon.fly(1,2,3); System.out.println("---공작새---"); Peacock.fly(1,2,3); System.out.println("---비둘기---"); pigeon.fly(2,2,10002); } }
인터페이스
접근제어자, 리턴타입, 메소드 이름만을 정의
함수의 내용은 없음
인터페이스를 구성하는 class는 함수의 내용({} 중괄호 안의 내용)을 반드시 구현해야한다.
interface Flyable { void fly(int x, int y, int z); } class Pigeon implements Flyable { // 내용 구현 private int x, y, z; @Override public void fly(int x, int y, int z) { printLocation(); System.out.println("날아갑니다."); this.x = x; this.y = y; this.z = z; printLocation(); } public void printLocation() { System.out.println("현재 위치 (" + x + ", " + ", " + z + ")"); } } public class Main { public static void main(String[] args) { Flyable pigeon = new Pigeon(); pigeon.fly(1,2,3,); } }
class Family {
String name;
int age;
int speed;
int x;
int y;
String way;
public Family(String name, int age) { this.name = name; this.age = age; this.x = x; this.y = y; if(name=="Grand") { this.speed=1; } else if(name=="Parents") { this.speed=3; } else { this.speed=5; } System.out.println("Name: " + name + ", Age: " + age + ", Speed: " + speed + ", Initial Location: " + x + ", " + y); if(name=="Grand") { this.x = 1; this.y = 1; way = "walking"; } else if(name=="Parents") { this.x = 2; this.y = 2; this.speed+=2; way = "running"; } else { this.x = 3; this.y = -1; this.speed+=1; way = "swimming"; } } public void printLocation() { System.out.println("Speed: " + speed + ", Location: " + x + ", " + y + ", by " + way); } } public class Main { public static void main(String[] args) { Family grand = new Family("Grand",70); grand.printLocation(); Family parents = new Family("Parents",35); parents.printLocation(); Family child = new Family("Childre",10); child.printLocation(); } }
Human.java
public class Human { String name; int age; int speed; int x,y; public Human(String name, int age, int speed, int x, int y) { this.name = name; this.age = age; this.speed = speed; this.x = x; this.y = y; } public Human(String name, int age, int speed) { this(name, age, speed, 0,0); } public String getLocation() { return "(" + x + ", " + y + ")"; } protected void printWhoAmI() { System.out.println("My name is " + name + ". " + age + " aged."); } }
Walkable.java
public interface Walkable { void walk(int x, int y); }
Runnable.java
public interface Runnable { void run(int x, int y); }
Swimmable.java
public interface Swimmable { void swim(int x, int y); }
GrandParent.java
public class GrandParent extends Human implements Walkable { public GrandParent(String name, int age) { super(name, age,1); } @Override public void walk(int x, int y) { printWhoAmI(); System.out.println("walk speed: " + speed); this.x = x; this.y = y; System.out.println("Walked to " + getLocation()); } }
Parent.java
public class Parent extends Human implements Walkable, Runnable{ public Parent(String name, int age) { super(name, age, 3); } @Override public void run(int x, int y) { printWhoAmI(); System.out.println("run speed: " + (speed + 2)); this.x = x; this.y = y; System.out.println("Ran to " + getLocation()); } @Override public void walk(int x, int y) { printWhoAmI(); System.out.println("walk speed: " + speed); this.x = x; this.y = y; System.out.println("Walked to " + getLocation()); } }
Child.java
public class Child extends Human implements Walkable, Runnable, Swimmable{ public Child(String name, int age) { super(name, age,5); } @Override public void run(int x, int y) { printWhoAmI(); System.out.println("run speed: " + (speed + 2)); this.x = x; this.y = y; System.out.println("Ran to " + getLocation()); } @Override public void walk(int x, int y) { printWhoAmI(); System.out.println("walk speed: " + speed); this.x = x; this.y = y; System.out.println("Walked to " + getLocation()); } @Override public void swim(int x, int y) { printWhoAmI(); System.out.println("swim speed: " + (speed + 1)); this.x = x; this.y = y; System.out.println("Swum to " + getLocation()); } }
Main.java
public class Main { public static void main(String[] args) { Human grandParent = new GrandParent("할아버지",70); Human parent = new Parent("어머니",45); Human child = new Child("아들", 15); Human[] humans = {grandParent, parent, child}; for(Human human : humans) { System.out.println(human.name + ", 나이: " + human.age + ", 속도: " + human.speed + "현재위치" + human.getLocation()); } System.out.println("<활동시작>"); for(Human human : humans) { if(human instanceof Walkable) { ((Walkable) human).walk(1,1); System.out.println("- - - - - -"); } if(human instanceof Runnable) { ((Runnable) human).run(2,2); System.out.println("- - - - - -"); } if(human instanceof Swimmable) { ((Swimmable) human).swim(3,-1); System.out.println("- - - - - -"); } } } }
어렵다....