標題:

Java class problem, Any one help ?

發問:

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;... 顯示更多 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 ?

最佳解答:

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.

免費註冊體驗

 

此文章來自奇摩知識+如有不便請留言告知

其他解答:
arrow
arrow

    bgdwze3 發表在 痞客邦 留言(0) 人氣()