package classEx;
public class Animal {
String name;
public void setName(String name) {
this.name = name;
}
}
package classEx;
public class Sample {
public static void main(String[] args) {
Animal cat = new Animal();
Animal dog = new Animal();
Animal bird = new Animal();
// cat.name = "meow";
cat.setName("dali"); // 값을 대입
System.out.println(cat.name);
dog.setName("puppy");
System.out.println(dog.name);// 객체 변수는 공유되지 않음
}
}
메소드의 구조
1) 입력/출력 있음
2) 입력/출력 없음
3) 입력 있음 / 출력 없음
4) 입력 없음 / 출력 있음
package methodEx;
interface Cal{
public abstract int hap(int d, int e); // 람다?
}
public class Sample {
int sum(int a, int b){
return a+b;
}
public static void main(String[] args) {
int a=3;
int b=4;
Sample sample = new Sample();
int c = sample.sum(a,b);
System.out.println(c);
// lambda?
Cal cal = (d, e) -> d+e;
System.out.println(cal.hap(10,40));
}
}
package methodEx;
public class Sample {
String say(){
return "Hey";
}
void sum(int a, int b){
System.out.println(a+"와 "+b+"의 합은 "+(a+b)+"이다.");
}
void called(){
System.out.println("bye");
}
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.say()); // 입력 없음/출력 있음
sample.sum(3,5); // 입력있음 / 출력 없음
sample.called(); // 입/출력 없음
}
}
출력(
return
)이 있는 경우System.out.print-
로 출력해야 함.
public class Sample {
void sayNick(String nick){
if ("fool".equals(nick)){
return;
}
System.out.printf("나의 별명은 %s이다.\n",nick);
}
public static void main(String[] args) {
Sample sample = new Sample();
sample.sayNick("fool"); // 출력 안 됨
sample.sayNick("gogo");
}
}
package methodEx;
public class Sample {
int varTest(int a){
a++; // 지역변수
return a;
}
public static void main(String[] args) {
int a=1;
Sample sample = new Sample();
a=sample.varTest(a);
System.out.println(a);
}
}
package callByValue;
class Updater{
// 변경전
// void update(int count){
// count++; // 변경전
// }
//변경후
void update (Counter counter){
counter.count++;
}
}
class Counter{
int count = 0; //객체변수
}
public class Sample {
public static void main(String[] args) {
Counter myCounter = new Counter();
System.out.println("before update : "+myCounter.count);
Updater updater = new Updater();
updater.update(myCounter);
System.out.println("After update : "+myCounter.count);
}
}
package Inheritance;
class Dog extends Animal{
int age;
Kind kind; // 포함(2) - 다른 클래스 사용
public Dog(int age, Kind kind) {
this.age = age;
this.kind = kind;
}
void sleep(){ // 확장 - 기능 추가
System.out.println(this.name+" is sleeping zzZ");
System.out.println(this.name+" is "+kind.type+" and "+kind.color);
}
}
class Kind{ // 포함(1)
String type;
String color;
public Kind(String type, String color) {
this.type = type;
this.color = color;
}
}
public class Sample {
public static void main(String[] args) {
Kind kind = new Kind("Jindo","white");
Dog dog = new Dog(3, kind);
dog.setName("bob");
System.out.println(dog.name);
dog.sleep();
}
}
package Inheritance;
class Animals{
String name;
public void setName(String name) {
this.name = name;
}
}
class Cat extends Animal{
void sleep(){
System.out.println(this.name + " zzZ");
}
}
class HouseCat extends Cat{
void sleep(){
System.out.println(this.name+" is zzZ");
System.out.println(this.name+" awake for eat~");
}
}
public class Overriding {
public static void main(String[] args) {
HouseCat houseCat = new HouseCat();
houseCat.setName("bobby");
houseCat.sleep();
}
}
package Inheritance;
class Bird extends Animal{
void sleep(){
System.out.println(this.name + " zzZ");
}
}
class HouseBird extends Bird{
void sleep() {
System.out.println(this.name+" is zzZ");
}
void sleep(int hour){ // 오버로딩 (재작성)
System.out.println(this.name + "is zzZ for "+hour+" hours.");
}
}
public class OverLoading {
public static void main(String[] args) {
HouseBird houseBird = new HouseBird();
houseBird.setName("Jack");
houseBird.sleep();
houseBird.sleep(3);
}
}
class Animal{
String name;
public void setName(String name) {
this.name = name;
}
}
class Dog extends Animal{
void sleep(){
System.out.println(this.name+" zzZ");
}
}
class HouseDog extends Dog{
public HouseDog(String name) { // 셍상지
this.setName(name);
}
void sleep(){
System.out.println(this.name+" is zzZ");
System.out.println(this.name+" is awake for hunt.");
}
void sleep(int hour){
System.out.println(this.name+" is zzZ for "+hour+" hours.");
}
}
public class Sample {
public static void main(String[] args) {
HouseDog houseDog = new HouseDog("olha");
System.out.println(houseDog.name);
}
}
package interfaceEx;
public interface Predator {
String getFood(); // 인터페이스 규칙 - 몸통이 없다.
}
package interfaceEx;
class Animal{
String name;
public void setName(String name) {
this.name = name;
}
}
class Tiger extends Animal implements Predator{
public String getFood(){
return "apple";
}
}
class Lion extends Animal implements Predator{
public String getFood(){
return "banana";
}
}
class ZooKeeper{
void feed(Predator predator){ // predator 인터페이스로 대체
System.out.println("feed " + predator.getFood());
}
}
public class Sample {
public static void main(String[] args) {
ZooKeeper zooKeeper = new ZooKeeper();
Tiger tiger = new Tiger();
Lion lion = new Lion();
zooKeeper.feed(tiger);
zooKeeper.feed(lion);
}
}
Tiger tiger = new Tiger(); // Tiger is a Tiger
Animal animal = new Tiger(); // Tiger is a Animal
Predator predator = new Tiger(); // Tiger is a Predator
Barkable barkable = new Tiger(); // Tiger is a Barkable
public interface Predator{
String getFood();
}
public interface Barkable{
void bark();
}
package Polymorphism;
class Animal{
String name;
public void setName(String name) {
this.name = name;
}
}
class Tiger extends Animal implements Predator, Barkable{
public String getFood(){
return "apple";
}
public void bark(){
System.out.println("어흥");
}
}
class Lion extends Animal implements Predator,Barkable{
public String getFood(){
return "banana";
}
public void bark() {
System.out.println("으르렁");
}
}
class ZooKeeper{
void feed(Predator predator){ // predator 인테페이스로 대체
System.out.println("feed " + predator.getFood());
}
}
// 추가-경비원 -> 인터페이스 작성(Barkable)
class Bouncer{
// void barkAnimal(Animal animal){
// if(animal instanceof Tiger){
// System.out.println("어흥");
// } else if (animal instanceof Lion) {
// System.out.println("으르렁");
// }
// }
void barkAnimal(Barkable animal){ //Animal 대신 Barkable 사용
animal.bark();
}
}
public class Sample {
public static void main(String[] args) {
ZooKeeper zooKeeper = new ZooKeeper();
Tiger tiger = new Tiger();
Lion lion = new Lion();
zooKeeper.feed(tiger);
zooKeeper.feed(lion);
System.out.println();
// 추가
Bouncer bouncer = new Bouncer();
bouncer.barkAnimal(tiger);
bouncer.barkAnimal(lion);
}
}
package abstractEx;
abstract public class Predator extends {
abstract String getFood();
void printFood(){
System.out.printf("My food is %s",getFood());
}
static int LEG_COUNT = 4; // 추상 클래스의 상수는 static 선언이 필요하다.
static int speed(){
return LEG_COUNT * 30;
}
}
package abstractEx;
class Animal{
String name;
public void setName(String name) {
this.name = name;
}
}
class Tiger extends Predator implements Barkable {
public String getFood(){
return "apple";
}
public void bark(){
System.out.println("어흥");
}
}
class Lion extends Predator implements Barkable {
public String getFood(){
return "banana";
}
public void bark() {
System.out.println("으르렁");
}
}
class ZooKeeper{
void feed(Predator predator){ // predator 인테페이스로 대체
System.out.println("feed " + predator.getFood());
}
}
// 추가-경비원 -> 인터페이스 작성(Barkable)
class Bouncer{
void barkAnimal(Barkable animal){ //Animal 대신 Barkable 사용
animal.bark();
}
}
public class Sample {
public static void main(String[] args) {
Tiger tiger = new Tiger();
Lion lion = new Lion();
System.out.println();
// 추가
Bouncer bouncer = new Bouncer();
bouncer.barkAnimal(tiger);
bouncer.barkAnimal(lion);
}
}