Leo
Leo

Reputation: 189

Equivalent of the C# method in java

I am new to programming and trying to translate some c# code to java. I am trying to find out what exactly the below method does. How can I write the same in java.

This is part of an abstract class in C#

 //
// Summary:
//     Gets a page object of the desired type that wraps this document.
//
// Type parameters:
//   TPage:
//     The WatiN.Core.Document.Page<T0>() subclass
//
// Returns:
//     The page object
public virtual TPage Page<TPage>() where TPage : Page, new();

Upvotes: 1

Views: 202

Answers (3)

Stefan Nuxoll
Stefan Nuxoll

Reputation: 867

Based on the documentation you provided this would be the best solution since you are likely wanting the type information to lookup the correct page.

public <T extends Page> T getPage(Class<T> pageClass)

You would then use it like this:

MyPage page = myClass.Page(MyPage.class);

This design is due to Java's implementation of generics being based around type erasure, so you'll need to pass in the desired type as a parameter to get around it.

Upvotes: 4

Dave Doknjas
Dave Doknjas

Reputation: 6542

public abstract <TPage extends Page> TPage Page();

But there is no Java equivalent to the "new()" constraint in C#.

Upvotes: 0

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

TPage Page<TPage extends Page>();

All methods are virtual by default in java so that part is fine.

Upvotes: 0

Related Questions