Java class problem, Any one help ?

2006-11-29 2:45 am
Can anyone help for question ? many Thanks.
class A{private int member;
public ClassA(int indata) {
member = indata;
int getMember() {return member;} }}

interface B { public int getB(); public void setB();}

class C extends A implements B { int memberC;
public ClassB(int inputC) {
memberC=inputC; }}

Is my definition correct ? How I know /test InterfaceB is belong to other class's object or not ?

回答 (1)

2006-11-29 8:07 am
✔ 最佳答案
I am not sure what you are trying to do but your code is really messy.
here is your code...

class A{private int member;
public ClassA(int indata) {
member = indata;
int getMember() {return member;} } //You cannot do this. take this function out of your constructor.
}

interface B {
public int getB();
public void setB();
}

class C extends A implements B {
int memberC;
public ClassB(int inputC)
{
memberC=inputC;
}
}

in class C, your constructor needs to call the constructor of the Super Class, AKA class A. use super()
Your class A constructor takes in a int parameter. So you will need to add that to the super() call. So if you are trying to put inputC in to class A, then you will call super(inputC) inside the class C constructor.

If you are doing what I think you are doing...
there is no need for memberC=inputC, in the C constructor, as the value are put in to A anyway, UNLESS you want to do it the original way.

here is the final code.
http://home.cfl.rr.com/ricebox/blah.txt


[How I know /test InterfaceB is belong to other class's object or not ?]
well, what you do is, declare an object of class C, which will implement the abstract methods of interface B.

C newObject = new C(100); //declare a C object
int FromB = C.getB() //this will return zero.

This doesnt actually tell you that getB() belongs to B, because right now, your interitance doesnt really do anything fancy. Unless you want to declare another super class,derive a new class from it, also implement interface B.


收錄日期: 2021-04-13 21:27:33
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061128000051KK02974

檢視 Wayback Machine 備份