2012-11-21 6 views
0

나는 스크린에 드래그 된 이미지를 다루기 위해 하위 클래스를 가지고있다. 변수의 각 이미지의 이름과 x, y 위치를 저장합니다. 그런 다음이를 확장하고 다른 작업을 수행하는 방법이 있습니다.부모 클래스에서 만든 변수를 자식 클래스에서 사용하는 방법이 있습니까?

저는이 두 클래스를 갖고 싶지만 이미지 이름과 x, y 위치는 다를 것입니다.

먼저이 클래스가 하위 클래스 인 클래스를 만들려고했습니다. 이 부모 클래스에서는 자식 클래스에서 만든 변수 대신에 동일한 변수를 사용하기를 희망하면서 동일한 변수를 만들려고했습니다.

하위 클래스는 여전히 부모가 아닌 그 안에 만들어진 변수를 사용합니다.

이 작업을 수행 할 수있는 방법이 있습니까, 아니면 잘못된 트리를 오르고 있습니까? 내가 질문을 이해한다면

public class cShapeParent extends cShapes { 

    // would like the base class to use this data 
    String[] mNames= 
    { 
     "ball_green.png", 
     "ball_green.png", 
     "ball_green.png"  
    }; 

    cShapeParent(Context context,int screenWidth, int screenHeight) 
    { 
     super(context, screenWidth, screenHeight); 
    } 
} 

하위 클래스

import java.io.IOException; 
import java.io.InputStream; 

import android.content.Context; 
import android.content.res.AssetManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Rect; 

public class cShapes { 
    public 

    String[] mNames= 
    { 
     "ball_green.png", 
     "ball_red.png", 
     "ball_blue.png" 
    }; 

    int[] mX={200,300,400}; 
    int[] mY={200,200,200}; 
    int[] mW={0,0,0}; 
    int[] mH={0,0,0}; 

    int Amount=3; 
    int MyScreenWidth=512; 

    int[] BitMapId={ 
     R.drawable.ball_green, R.drawable.ball_red, 
     R.drawable.ball_blue, R.drawable.ball_green 
    }; 

    Bitmap MyImages[]; 
    Rect[] mBitmapSize; 

    cShapes(Context context,int screenWidth, int screenHeight) 
    { 
     // Load in bitmaps 
     MyImages=new Bitmap[4]; 

     AssetManager assetManager= context.getAssets(); 
     InputStream inputStream; 
     mBitmapSize = new Rect[Amount]; 
     try { 
      // set scaling ratio 
      float r=(float)MyScreenWidth/(float)screenWidth; 
      for(int i=0;i<Amount;i++) 
      { 
       inputStream=assetManager.open(mNames[i]); 
       MyImages[i]=BitmapFactory.decodeStream(inputStream); 

       int w=(int)((float)MyImages[i].getWidth()*r); 
       int h=(int)((float)MyImages[i].getWidth()*r); 
       mBitmapSize[i]=new Rect(0,0,w,h); 
       mW[i]=w; mH[i]=h; 

       mX[i]=(int) ((float)mX[i]*r); 
       mY[i]=(int) ((float)mY[i]*r); 

       inputStream.close(); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    boolean Intersect(int id, int x,int y) 
    { 
     if (x >= mX[id] && x <= mX[id]+128 && y >= mY[id] && y <= mY[id]+128) 
      return true; 

     return false; 
    } 
} // end class 
+7

나는 제목을 편집 할 수 없습니다. –

답변

1

올바르게, 당신은 당신의 하위 클래스에서 상위 클래스에 정의 된 변수에 액세스하려고합니다. 그렇다면 당신의 질문은 그렇습니다. 가능한 슈퍼 키워드를 사용하십시오. 당신도 슈퍼 키워드 않고 직접 i에 액세스 할 수

class Super{ 
public int i; 
} 
class Sub extends Super { 
    public void method() { 
    System.out.println(super.i); // you can access i as it is overriden by sub 
    } 
} 

참고, 난 그냥 그것이 더 표현하기 위해 포함되어 있습니다. 그러나이 예제에서는 으로 선언되지 않은 경우에만 i에 액세스 할 수 있습니다.

+0

이 예제에서는'super'를 사용할 필요가 없습니다. – Madbreaks

+0

@Madbreaks notr 정말,하지만 그 비트 표현 :) – PermGenError

+0

동의하지만, 그것은 (당신이 봤어, 좋은) 어떤 설명을 보증한다고 생각. +1 – Madbreaks

0

잠깐, 하위 (cShapeParent) 클래스가 부모 (cShapes) 클래스에서 참조 할 수있는 변수를 만들고 유지하려고합니까? 어떤 경우에 나는이 종류의 패턴을 따를 것입니다 :

public class Child extends Parent { 

    private String[] mNames= { 
    "ball_green.png", 
    "ball_red.png", 
    "ball_blue.png" 
    }; 

    protected String[] getNames() { 
     return mNames; 
    } 

} 

public abstract class Parent { 

    protected abstract ListString[] getNames(); 

    public void doX() { 
     ... 
     String[] names = getNames(); 
     ... 
    } 
} 
+0

안녕하세요, 도움을 주셔서 감사합니다. 저는 제 앱을 프로그래밍 할 때 Java를 배우는 사람입니다. –

+0

@Tedpottel 기꺼이 도와 드릴 수 있습니다! 이미지의 모든 세부 사항에 대해 병렬 배열을 갖는 대신, 단일 이미지의 세부 사항을 담을 수있는 간단한 래퍼를 만들 수 있습니다. 그렇게하면 주위에있는 사람들의 목록을 전달할 수 있습니다 :) (컬렉션 API는 많은 경우 어쨌든 배열을 처리하는 데 더 좋을 수 있습니다) – Charlie

관련 문제