pootisspencer 1 Share Posted October 26, 2018 Hello, I'm still kinda a beginner to Java. I wanted to know how exactly I could make a .jar API file with real class, method, field names, but the actual contents (body) of the code not visible when importing it into the project. To clarify what I'm talking about :- The same thing when importing the bot's client.jar file to your script project and programming using that API, - how do you do it? Link to comment Share on other sites More sharing options...
Notorious 342 Share Posted October 26, 2018 So creating an API is actually pretty easy, I'll do a quick example below to hopefully show you the process. There are a few ways to can go about this, but personally, I like using interfaces for structure, then implementing them for different use-cases. So, for example, we have a library project, where we are making our API. Inside we want to create an interface. package com.project.api; public interface IExample { // This is the method we will be implementing. int getValue(); } After creating our interface, let's make an impl package in the same location as the interface, and inside the new impl package, create an implementation of the Example class like so. package com.project.api.impl; import com.project.api.IExample; public class ExampleImpl implements IExample { @Override public int getValue() { return 1337; } } After you have created your implementation, you can now package your project as a JAR file, and add it as a dependency in another project. To call this API from a new project, ensure you have added it as a dependency, then use it like so. package com.different.project; import com.project.api.IExample; import com.project.api.impl.ExampleImpl; public class TestAPI { //Here we reference our interface, as our implementation is of the same type. private IExample example; public TestAPI() { //Here we set our reference to our Example implementation this.example = new ExampleImpl(); } @Override pubic String toString() { return String.format("Example implementation value: %d", this.example.getValue()); } } Now once we have referenced our new API, you can now access functions within the interface, and they will behave based on the implementation provided. So for example, we printed our toString in the TestAPI class above, we should have: "Example implementation value: 1337" print out, as our ExampleImpl class returns that value for the overridden function. I hope this helps answer a few questions you were having. Also, this code is untested and was written in the browser, so there could be syntax/logic errors, though the idea remains the same either way. Link to comment Share on other sites More sharing options...
pootisspencer 1 Author Share Posted October 27, 2018 Thanks man ! I'll have to try it out Link to comment Share on other sites More sharing options...
Articron 738 Share Posted October 28, 2018 On 10/27/2018 at 1:11 AM, Notorious said: So creating an API is actually pretty easy, I'll do a quick example below to hopefully show you the process. There are a few ways to can go about this, but personally, I like using interfaces for structure, then implementing them for different use-cases. So, for example, we have a library project, where we are making our API. Inside we want to create an interface. package com.project.api; public interface IExample { // This is the method we will be implementing. int getValue(); } After creating our interface, let's make an impl package in the same location as the interface, and inside the new impl package, create an implementation of the Example class like so. package com.project.api.impl; import com.project.api.IExample; public class ExampleImpl implements IExample { @Override public int getValue() { return 1337; } } After you have created your implementation, you can now package your project as a JAR file, and add it as a dependency in another project. To call this API from a new project, ensure you have added it as a dependency, then use it like so. package com.different.project; import com.project.api.IExample; import com.project.api.impl.ExampleImpl; public class TestAPI { //Here we reference our interface, as our implementation is of the same type. private IExample example; public TestAPI() { //Here we set our reference to our Example implementation this.example = new ExampleImpl(); } @Override pubic String toString() { return String.format("Example implementation value: %d", this.example.getValue()); } } Now once we have referenced our new API, you can now access functions within the interface, and they will behave based on the implementation provided. So for example, we printed our toString in the TestAPI class above, we should have: "Example implementation value: 1337" print out, as our ExampleImpl class returns that value for the overridden function. I hope this helps answer a few questions you were having. Also, this code is untested and was written in the browser, so there could be syntax/logic errors, though the idea remains the same either way. Whoa man who said you could have that S++ rank?! You need to pass the test first! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.