java(자바) 공부중인데.. 도움 부탁드립니다.!

eoeo708의 이미지

class foo{
	public int a = 3;
	public void addFive() {
		a+=5;
		System.out.println("f");
 
	}
}
class Bar extends foo{
	public int a = 8;
 
public void addFive(){
		this.a +=5;
		System.out.print("b");
	}
}
public class jsut {
 
	public static void main(String[] args) {
		foo f =new Bar();
		f.addFive();
		System.out.println(f.a);
	}
 
}

결과값
b 3

Q1. 여기에서 왜 3이 나오는 건지 잘 모르겠습니다. ㅜㅠ
f에 들어있는건 Bar아닌가요? f.addfive 햇을때에는 bar에잇는게나오고 f.a하면 3이나오는이유가....
어떻게이해해야되는건가요.. 참조변수.

익명 사용자의 이미지

> f에 들어있는건 Bar아닌가요?

f에 들어있는 것은 Bar인데 Bar는 foo를 상속했으니까 Bar는 foo를 포함하고 있습니다.
즉, f에는 foo도 들어있는 거지요. foo의 멤버 변수인 a와 Bar의 멤버 변수인 a가 모두 존재합니다.
각각을 foo.a 와 Bar.a 라고 부릅시다.

멤버 함수는 override됩니다. 하지만 멤버 변수는 override되지 않습니다.
풀어서 말하자면...
올리신 예제에서처럼 foo의 인스턴스 변수를 통해서 Bar객체를 다룰 때에
멤버 함수 호출은 Bar의 멤버 함수를 호출하게 되지만,
멤버 변수는 이와 달리 foo의 멤버 변수를 보게 됩니다.
규칙이 그렇습니다.

좀 더 자세히...
f.addFive()는 Bar의 addFive를 호출하고, Bar의 addFive는 Bar의 멤버 변수 Bar.a = 8 에 5를 더해서 Bar.a = 13을 만듭니다.
Bar에 포함되어 있는 foo의 foo.a 는 바뀌지 않고 3 그대로이지요.
그리고 f.a 는 Bar.a가 아니라 foo.a이기 때문에 System.out.println(f.a)는 3을 출력합니다.

신재민 킬러의 이미지

eoeo님 한번 다시 잘 체크해보세요 !!

익명 사용자의 이미지

신재민 킬러님 글 쓴 날짜 한번 다시 잘 체크해보세요 !!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.