archive
7장 상속 연습 문제 및 LAB 문제 -1 본문
1. 동물 예제
package 동물예제;
public class Animal {
private double weight;
private String picture;
void eat(){
System.out.println("eat()가 호출되었음");
}
void sleep(){
System.out.println("sleep()가 호출되었음");
}
}
package 동물예제;
public class Lion extends Animal{
private int legs=4;
void roar() {
System.out.println("roar()가 호출되었음");
}
}
package 동물예제;
public class Eagle extends Animal{
private int wings=2;
void fly() {
System.out.println("fly()가 호출되었음");
}
}
package 동물예제;
public class AnimalTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Lion mylion=new Lion();
Eagle myeagle=new Eagle();
mylion.eat();
mylion.sleep();
mylion.roar();
myeagle.eat();
myeagle.sleep();
myeagle.fly();
}
}
2. 도형 예제
package 도형예제;
public class Shape {
public int x,y;
void print() {
System.out.println("x좌표 : "+x+" y좌표: "+y);
}
}
package 도형예제;
public class Rectangle extends Shape{
private int width, height;
double area() {
return (double)width*height;
}
void draw() {
System.out.printf("(%d,%d)위치에 가로: %d 세로: %d\n", x, y, width, height);
}
public static void main(String[] args) {
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.x=5;
r1.y=3;
r1.width=10;
r1.height=20;
r2.x=8;
r2.y=9;
r2.width=10;
r2.height=20;
r1.print();
r1.draw();
r2.print();
r2.draw();
}
}
3. 직원과 매니저 클래스 실습예제
package 직원과매니저클래스작성;
public class Employee {
public String name;
public String address;
protected int salary;
private int rrn;
public Employee(String name,String address,int salary,int rrn) {
this.name=name;
this.address=address;
this.salary=salary;
this.rrn=rrn;
}
@Override
public String toString() {
return "Employee [name="+name+", address="+address+", salary="+salary+", rrn="+rrn+"]";
} //rrn을 출력해야하기 때문에 이 클래스에 위치해야함, 아니면 이 클래스에 접근자를 추가하던가 아마...?
}
package 직원과매니저클래스작성;
public class Manager extends Employee{
private int bonus;
public Manager(String name,String address,int salary,int rrn, int bonus) {
super(name, address, salary, rrn);
this.bonus=bonus;
}
public void test() {
System.out.println("name = "+name);
System.out.println("address = "+address);
System.out.println("salary = "+salary);
}
}
package 직원과매니저클래스작성;
public class ManagerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee employee=new Employee("Tom", "Seoul", 1000000, 123456);
Manager manager=new Manager("Tom", "Seoul", 1000000, 123456, 50000);
String s=employee.toString();
System.out.println(s);
manager.test();
}
}
4. 은행 클래스 작성하기
package 은행다양한이자율;
public class Bank {
double getInterestRate() {
return 0.0;
}
}
class BadBank extends Bank{
double getInterestRate() {
return 10.0;
}
}
class NormalBank extends Bank{
double getInterestRate() {
return 5.0;
}
}
class GoodBank extends Bank{
double getInterestRate() {
return 3.0;
}
}
class BankTest{
public static void main(String[] args) {
BadBank bb=new BadBank();
NormalBank nb=new NormalBank();
GoodBank gb=new GoodBank();
System.out.println("BadBank의 이자율: "+ bb.getInterestRate());
System.out.println("NormalBank의 이자율: "+ nb.getInterestRate());
System.out.println("GoodBank의 이자율: "+ gb.getInterestRate());
}
}
5. 복잡한 상속 구조
package 복잡한상속;
public class Shape {
private int x;
private int y;
public Shape(int x, int y) {
this.x=x;
this.y=y;
System.out.println("Shape()");
}
}
package 복잡한상속;
public class Rectangle extends Shape {
private int width;
private int height;
public Rectangle(int x, int y, int width, int height) {
super(x,y);
System.out.println("Rectangle()");
this.width=width;
this.height=height;
}
double area() {
return width*height;
}
}
package 복잡한상속;
public class Colored extends Rectangle{
String color;
public Colored(int x, int y, int width, int height, String color) {
super(x,y,width, height);
System.out.println("Colored()");
this.color=color;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Colored thing= new Colored(10,10,20,20,"red");
}
}
6. 동적 메소드 호출
package 동적메소드;
public class Animal {
void sound() {
System.out.println("Animal 클래스의 sound()");
}
}
package 동적메소드;
public class Cat extends Animal {
void sound() {
System.out.println("냐옹");
}
}
package 동적메소드;
public class Dog extends Animal {
void sound() {
System.out.println("멈머");
}
}
package 동적메소드;
public class SoundTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal animal=new Animal();
Dog dog=new Dog();
Cat cat=new Cat();
Animal s;
s=animal;
s.sound();
s=dog;
s.sound();
s=cat;
s.sound();
}
}
7. TV 클래스 상속 예제
package TV클래스;
class TV{
private int size;
public TV(int size) { this.size = size; }
protected int getSize() { return size; }
}
package TV클래스;
class ColorTV extends TV {
private int color;
ColorTV(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.print(getSize()+"인치 "+color+"컬러");
}
}
package TV클래스;
class IPTV extends ColorTV {
String IP;
IPTV(String IP, int size, int color) {
super(size, color);
this.IP = IP;
}
public void printProperty() {
System.out.print("나의 IPTV는 "+IP+" 주소의 ");
super.printProperty();
}
}
package TV클래스;
public class TVTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
IPTV iptv = new IPTV("192.1.1.2", 32, 2048);
iptv.printProperty();
}
}
8. point 이동 클래스 상속 예제
package Point;
public class Point {
private int x,y;
public Point(int x, int y) {
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
this.x=x;
this.y=y;
}
}
package Point;
public class Point {
private int x,y;
public Point(int x, int y) {
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
this.x=x;
this.y=y;
}
}
package Point;
public class PositivePoint extends Point {
public PositivePoint() {
super(0, 0);
}
public PositivePoint(int x, int y) {
super(x,y);
if(x<0 || y<0) {
super.move(0,0);
}
}
protected void move(int x, int y) {
if(x>0 && y>0) {
super.move(x, y);
}
}
public String toString() {
String s = "("+getX()+","+getY()+")의 점";
return s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PositivePoint p = new PositivePoint();
p.move(10, 10);
System.out.println(p.toString()+"입니다.");
p.move(-5,5); // 객체 p는 음수 공간으로 이동되지 않음
System.out.println(p.toString()+"입니다.");
PositivePoint p2 = new PositivePoint(-10, -10);
System.out.println(p2.toString()+"입니다.");
}
8. ColorPoint 상속 예제
package Point;
public class ColorPoint extends Point{
private String color;
public ColorPoint(int x, int y, String color) {
super(x,y);
this.color=color;
}
public void setXY(int x, int y) {
move(x,y);
}
public void setColor(String color) {
this.color=color;
}
public String toString(){
String s= this.color+"색의 ("+getX()+","+getY()+")의 점";
return s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ColorPoint cp=new ColorPoint(5,5,"Yellow");
cp.setXY(10,20);
cp.setColor("RED");
String str=cp.toString();
System.out.println(str+"입니다.");
}
}
'Java > 스터디 예제 풀이' 카테고리의 다른 글
9장 인터페이스, 람다식, 패키지 연습 문제 및 LAB 문제 (0) | 2023.03.03 |
---|---|
7장 상속 연습 문제 및 LAB 문제 -2 (0) | 2023.03.03 |
6장 클래스와 메소드 심층 탐구 연습 문제 및 LAB 문제 (0) | 2023.02.05 |
5장 클래스, 객체, 메소드 연습 문제 및 LAB 문제 (0) | 2023.02.05 |
4장 배열 연습 문제 및 LAB 문제 (0) | 2023.02.05 |