문제

I have 3 classes:

class Superclass...

public class Generic{
     public Generic(Superclass s){...}
}

And a subclass extending Superclass

class SubClass extends Superclass{
      public void methodM(){
           Generic g = new Generic(???);
           ...
      }
}

In methodM I use the constructor Generic and I want to pass a reference to the Superclass instance extended by Subclass. So I don't want to do something like new Generic(new Superclass), rather something like: new Generic(super.IdontKnowWhat)

thank you

도움이 되었습니까?

해결책

The reference to the current object is implicitly a reference to a superclass as well (as it extends it).

So you should be able to do

 Generic g = new Generic(this);

다른 팁

Just call it as super.parameter as super is an inbuilt keyword used to reference the super class fields. You can read about super in the java docs tutorial for super.

U can pass the reference of the subclass as it extends the superclass.. But it depends on what methods u want to acssess..If u pass reference of subclass then u should acssess the methods of subclass through the object received in constructor of Generic class.. So u should construct the new object of superclass in case u want to acssess methods of superclass..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top