23.01.11(Java)

MH S·2023년 1월 11일

Java

목록 보기
4/16

상속

package ch07;

class Car1 /*2대 클래스*/{
	int velocity;

	void speedUp() {
		velocity++;
	}
	void speedDown() {
		velocity--;
		if(velocity<0)
			velocity=0;
	}

	void stop() {
		velocity=0;
	}
}

class Taxi1 extends Car1{
	@Override //Annotation(Java 5.0 추가)
	void speedUp() {
		velocity+=5;
	}
	
}

class Bus1 extends Car1{
	@Override
	void speedUp() {
		super.speedUp();//상위 메소드 호출
		if(velocity>100)
			velocity=100;
	}
}

public class InheritanceEx1 {
	public static void main(String[] args) {
		Taxi1 t = new Taxi1();
		t.speedUp();
		System.out.println(t.velocity);
		
		Bus1 b = new Bus1();
		b.velocity=110;
		b.speedUp();
		System.out.println(b.velocity);
	}
}
//출력 5
//    100
package ch07;

import java.awt.Color;
import java.awt.Frame;

class MFrame2 extends Frame{
	public MFrame2() {
	}
	public MFrame2(Color c, int w, int h, boolean flag) {
		setBackground(c);
		setSize(w,h);
		setVisible(true);
	}
}
	
public class InheritanceEx2 {
	public static void main(String[] args) {
		MFrame2 m = new MFrame2();
		m.setBackground(Color.orange);
		m.setSize(300,300);
		m.setVisible(true);
		MFrame2 m2 = new MFrame2(Color.red, 200, 150, true);	
	}
}
// 프레임 출력

package ch07;

class Car3{
	public Car3() {
		super();
	}
	void speedUp() {
		
	}
}

class Taxi3 extends Car3{
	void stop() {}
}

public class InheritanceEx3 {
	public static void main(String[] args) {
		Object obj = new Car3();
		//obj.speedUp();
		Car3 c = new Taxi3();
		//Taxi3 t = new Car3();
		
	}
}

상위클래스가 하위클래스를 참조할수는 있어도 하위클래스의 메소드를 호출할수는 없다.

package ch07;

class A4{}
class B4{}
class C4 extends A4/*, B4 */{
	
}

//인터페이스
interface D4{}
interface E4{}
interface F4 extends D4, E4{}

public class InheritanceEx4 {
	public static void main(String[] args) {
		
	}
}



캐스팅

package ch07;

public class CastingEx1 {
	public static void main(String[] args) {
		int a= 1234567;
		short s= (short)a;
		System.out.println(a);
		System.out.println(s);
		long l=a;
		System.out.println(l);
	}	
}
package ch07;

import java.util.Vector;

class Person2{
	String name;
	public Person2(String name) {
		this.name=name;
	}
}

class Student2 extends Person2{
	String grade;
	public Student2(String name) {
		super(name);
	}
}
public class CastingEx2 {
	public static void main(String[] args) {
		Person2 p;
		Student2 s = new Student2("홍길동");
		p=s;//업캐스팅(UpCasting) 발생
		System.out.println(p.name);
		//System.out.println(p.grade);
		Student2 s2;
		Person2 p2 = new Person2("홍길자");
		//s2 = (Student2)p2;//다운캐스팅(DownCasting)
		//s2.grade = "A";
		//System.out.println(s2.grade);
		Vector v = new Vector();
		String str = new String();
		Student2 s3 = new Student2("장동건");
		v.add(str);
		v.add(s3);
		String str1 = (String)v.get(0);
		Student2 s4 = (Student2)v.get(1);
	}
}
package ch07;

import java.util.Iterator;

class Animal{
	void move() {
		System.out.println("동물아 움직여라~");
	}
}

class Bird extends Animal{
	String name = "새";
	@Override
	void move(){
		System.out.println(name + "날아라~");
	}
}

class Fish extends Animal{
	String name = "물고기";
	@Override
	void move(){
		System.out.println(name + "헤엄쳐라~");
	}
}

class Cheetah extends Animal{
	String name = "치타";
	@Override
	void move(){
		System.out.println(name + "달려라~");
	}
}

public class CatingEx3 {
	public static void main(String[] args) {
		//Animal 타입의 객체를 저장 할 수 있는 칸을 3개 만듬
		Animal ani[] = new Animal[3];
		ani[0] = new Bird();
		ani[1] = new Fish();
		ani[2] = new Cheetah();
		for(int i=0; i<ani.length;i++) {
			ani[i].move();//동적 바인딩: 하위 클래스의 메소드를 호출한다.
		}
	}
}
//출력 새날아라~
//	  물고기헤엄쳐라~
//	  치타달려라~



추상클래스

  • Type 존재, 객체 생성 x(객체 선언은 가능)
  • 상속 유지
  • 추상메소드(추상 클래스에 추상 메소드 선언시 하위 클래스에 추상 메소드 선언 해야됨.)
package ch07;

import java.awt.Component;

//추상 클래스
abstract class Abstract1{
	//추상 메소드
	abstract void prn();
}

class Normal1 extends Abstract1{
	@Override
	void prn() {
	}
}

class MComponent extends Component{
	
}

public class AbstractEx1 {
	public static void main(String[] args) {
		Abstract1 a ;
		//a = new Abstract1 ();
		a= new Normal1();
		
	}
}
  • 추상 클래스끼리는 추상 메소드 선언 할 필요가 없음.
package ch07;

abstract class Abstract2{
	abstract void prn();
}

abstract class Abstract2_1 extends Abstract2{
	abstract void prn2();
}

class Normal2 extends Abstract2_1{

	@Override
	void prn2() {
	}

	@Override
	void prn() {
	}
	
}
public class AbstractEx2 {
	public static void main(String[] args) {
		
	}
}
package ch07;

abstract class Abstract3{
	//일반필드
	int a;
	//일반메소드
	void prn() {}
	//abstract prn1() {}
}

public class AbstractEx3 {
	public static void main(String[] args) {
		
	}
}
package ch07;

//추상클래스: 도형
abstract class Shape{
	int x,y;
	void move(int x, int y) {
		this.x = x;
		this.y= y;
	}
	abstract void draw();
}

//삼각형
class Triangle extends Shape{
	@Override
		void draw() {
			System.out.println("삼각형 그리기");
		}
}

//사각형
class Rectangle extends Shape{
	@Override
		void draw() {
			System.out.println("사각형 그리기");
		}
}

//원
class Circle extends Shape{
	@Override
		void draw() {
			System.out.println("원 그리기");
		}
}
		

public class AbstractEx4 {
	public static void main(String[] args) {
		
	}
}

인터페이스

  • 인터페이스 선언 후 메소드 선언시 자동 추상메소드로 전환됨
package ch08;

interface Calc{
	void plus(int a, int b);//자동 추상메소드
}

class Function{
	
}
//
//class Function extends Graphics{
//	
//}

class Graphics implements Calc{
	@Override
	public void plus(int a, int b) {
	}
}


public class InterfaceEx1 {

	
}
  • 인터페이스 안의 필드는 무조건 상수
  • 일반 메소드는 선언 불가
package ch08;

interface Interface2{
	int a=10;//필드는 무조건 상수 static final
	//void prn() {}//일반 메소드는 선언 불가
	void prn();
}

//인터페이스들끼리 상속 가능 
interface Interface2_1 extends Interface2 {
	void prn1();
}

class MyClass3 implements Interface2_1{
	@Override
	public void prn() {
	}
	
	@Override
	public void prn1() {
	}
}

public class interfaceEx2 {
	
}
package ch08;

interface InterfaceA3{
	void prn1();
}
interface InterfaceB3{
	void prn2();
}

class MyClass implements InterfaceA3,InterfaceB3{
	@Override
	public void prn2() {
	}

	@Override
	public void prn1() {
	}
	
}
public class InterfaceEx3 {

}
package ch08;

abstract class Tv{
	String color;
	boolean power;
	int ch;
	void powerOnOff() {
		power=!power;
	}
	
	void upCh() {
		ch++;
	}
	
	void downCh() {
		ch--;
	}
	
	abstract void setModel();
}

class SMTv extends Tv implements RemoteControl{
	@Override
	void setModel() {
	}
	
	@Override
	public void turnOff() {
	}
	
	@Override
	public void turnOn() {
	}
}
class LGTv extends Tv implements RemoteControl{
	@Override
	void setModel() {
	}
	
	@Override
	public void turnOff() {
	}
	
	@Override
	public void turnOn() {
	}
}

interface RemoteControl{
	void turnOn(); 
	void turnOff();
}

public class InterfaceEx4 {

}




0개의 댓글