java 기초 질문입니다. ㅠ.ㅠ

hyunuck의 이미지

실제로 이렇게 쓸일은 거의 없을것 같은데요,
책보다보니 궁굼해서 질문드립니다.

3개의 클래스 a,b,c 가 있다고 가정했을때
a
b extends a
c extends b

했을때 b,c 클래스에서 a 에 있는 notice()를 오버라이딩 했다고 가정합니다.

그럼 c 에서
super.notice() 하면 b에 있는 notice() 가 호출되는데,
c 에서 a 에 있는 notice()로 바로 접근하는 방법은 없는지 궁굼합니다.
기분같아서는 super.super.notice()하면 될것같았는데 에러나더군요. ㅋ

혹시 자세히 아시는분은 답변 부탁드립니다.

익명 사용자의 이미지

설계를 바꾸세요.
c 와 b 의 공통 부분을 뽑아 d 를 만들고

a
d extends a
b extends d
c extends d

이렇게 하면 될듯합니다.
c, d 는 notice()를 오버라이드하지 않고 b 만 오버라이드하면
님이 원한대로 될 겁니다.

dwfree74의 이미지

이런 식의 구체적으로 찾아가는 것도 괜찮지 않나요?

//TestSuper.java
class Employee
{
	void notice(int num)
	{
		switch (num)
		{
		case 1: 
			System.out.println("할아버지");		
		    break;
		}

	}
};

class  b extends Employee
{
	void notice(int num)
	{
		super.notice(num);

		switch (num)
		{
		case 2:
			System.out.println("아버지");		
		    break;

		}
	}


};

class  c extends b
{

	void notice(int num)
	{
		super.notice(num);
		switch (num)
		{
		case 3:
			System.out.println("아들");
		    break;
		}
	}


};

class  TestSuper
{

	public static void main(String[] args) 
	{
		c t_c = new c();

		t_c.notice(1);
		t_c.notice(2);
		t_c.notice(3);

	}
}

kldp.net 에 많은 프로그래머들이 동참하기를 바라며...^^

theuhm의 이미지

문득 생각이 난 코드인데...

class c extends b
{
    public void notice()
    {
        a A = new a( this );
        A.notice();
        assign( A );
    }
    public void assign( a A )
    {
         // change internal states... possibly, nothing to change..
    }
};

이런 식으론 곤란할까요?
a.notice()메소드가 내부 상태를 바꾸지 않는다면야 별 문제 없이 적용이 가능하지 않을까요. 그렇다면 assign() 메소드는 필요가 없어지죠..

..

nohmad의 이미지

질문하신 분의 상황을 고려컨대, 지나치게 상속 구조에 얽매여 있는 것 같습니다. 다른손님 말씀처럼 상속구조가 너무 깊어지지 않도록 공통 부분을 분리하는 것도 좋은 방법이고, theuhm님 말씀처럼 그냥 위임을 해버려도 됩니다. 위임(delegation)이란 C에 notice 메쏘드 호출을 요청했는데, 정작 C.notice는 A의 notice 메쏘드에게 일을 떠넘기는 것을 말합니다. 코드상의 추함이나 퍼포먼스, 다형성 문제를 제외하면, 위임만으로도 자바의 extends 키워드가 하는 일을 대신할 수 있습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.