How can I run a class method inside of a public void from a different class? - class

So I have a method like so (Method1):
public class Levels extends JFrame{
public void levelClass() {
if(menu.playerClass.equals("Warrior")) {
// I NEED COMMAND HERE
}
}
}
and I want to know how to run this class method (that is in a different class):
public class Classes {
public void listClasses() {
class Warrior { // THIS ONE
int health=100;
int evasionChance=20; // Percentage
int maxAttackDamage=30;
int minAttackDamage=25;
int numHealthPotions=2;
}
}
}
from the first code aka Method1.
Edit
DON'T
Change all:
public class Classes {
public void listClasses() {
class Warrior {
int health=100;
int evasionChance=20; // Percentage
int maxAttackDamage=30;
int minAttackDamage=25;
int numHealthPotions=2;
}
}
}
To:
public class Classes {
public void Warrior {
int health=100;
int evasionChance=20; // Percentage
int maxAttackDamage=30;
int minAttackDamage=25;
int numHealthPotions=2;
}
}

To call a method on a class, you need to instantiate the class.
public class Levels extends JFrame{
public void levelClass() {
if(menu.playerClass.equals("Warrior")) {
// instantiate the Classes class
Classes classes = new Classes();
// call the warrior method
classes.warrior();
}
}
}

Related

i faced problem in baseclass and derived class

I faced some problem in topic inheritence in this i make a Baseclass a "Animal" and its subClass
"Dog" but after makin object of Dog i print no of legs ,eyes ,sound but after the code run there is written eyes=0;legs=0;& sound=null.
Below is my code..
class Animal{
int legs;
int eyes;
public void setEyes(int i){
i=eyes;
return;
}
public int getEyes(){
return eyes;
}
public void setLegs(int i){
i=legs;
return;
}
public int getLegs(){
return legs;
}
}
class Dog extends Animal{
String sound;
public void setSound(String i){
i=sound;
return;
}
public String getSound(){
return sound;
}
}
public class practice_oops{
public static void main(String[] args) {
Dog D=new Dog();
D.setEyes(2);
D.setLegs(4);
D.setSound("bark");
System.out.println(D.getEyes());
System.out.println(D.getLegs());
System.out.println(D.getSound());
}
}
You should set
eyes=i,
legs=i,
sound=i
in the
setEyes(),
setLegs(),
setSound()
functions respectively.

Haxe access Class<T> static fields

I have three classes and I would like to be able to call static functions from the returned Class<Access>. I would like to select class type based on conditions.
class Access {
public static function get(item: Int): Int { return -1; }
public static function getAccessType(): Class<Access> {
if(Client.hasConnection())
return Remote;
else return Local;
}
}
class Remote extends Access {
override public static function get(item: Int): Int { return Server.getItem(item); }
}
class Local extends Access {
override public static function get(item: Int): Int { return Client.getItem(item); }
}
You can't override a static function in Haxe.
But you can probably achieve what you're trying to do by simply removing the override in Remote and Local
Can be done with singletons.
However, still the question might relevant whether such feature in Haxe even exists.
Depending on target, you may be able to cast a class to an interface/typedef to pull out values in a type-safe-ish way. "override" does not work for static methods
class Test {
static function pick(z:Bool):HasGetItem {
return z ? cast A : cast B;
}
static function main() {
trace("Haxe is great!");
trace(pick(false).getItem(1));
trace(pick(true).getItem(2));
}
}
#:keep class A {
public static function getItem(i:Int):Int return 10;
}
#:keep class B {
public static function getItem(i:Int):Int return 5;
}
typedef HasGetItem = {
getItem:Int->Int
}
https://try.haxe.org/#b2b87

Interface in java

interface A {
public void eg1();
}
interface B {
public void eg1();
}
public class SomeOtherClassName implements A, B {
#Override
public void eg1() {
System.out.println("test.eg1()");
}
}
What is the output and what occurs if method is overriden in interface?
First of all it's of no use to implement both class A and B as both
of them has same method signature i.e both has same method name and
return type.
Secondly you'll need a main method to run the program.
Also in interface you can only declare the methods, the implementation
has to be done in the class which implements it.
interface A {
public void eg1();
}
interface B {
public void eg1();
}
public class Test implements A{
#Override
public void eg1() {
System.out.println("test.eg1()");
}
public static void main (String args[]) {
A a = new test();
a.eg1();
}
}
Output : test.eg1()

Generic build adding fields

Consider this code:
http://try.haxe.org/#5AD6e
class Test
{
public static function main()
{
var foo = new FluentFoo(null, { bar: 1 }).hello();
}
}
class Foo
{
public function new(options:{ bar:Int }) {}
public function hello()
{
trace("Hi there");
}
}
class Fluent
{
private var parent:Null<Fluent>;
public function new(parent:Null<Fluent>)
{
this.parent = parent;
}
public function end()
{
if(parent != null) {
return parent;
}
throw 'Top level already reached';
}
}
class FluentFoo extends Fluent
{
public var base:Foo;
public function new(parent:Null<Fluent>, options:{ bar:Int })
{
super(parent);
base = new Foo(options);
}
public function hello()
{
base.hello();
return this;
}
}
I want to generate classes such as FluentFoo automatically.
In pseudohaxe code:
import haxe.Constraints.Constructible;
class Test
{
public static function main()
{
var foo = new Fluent<Foo>(null, { bar: 1 }).hello();
}
}
class Foo
{
public function new(options:{ bar:Int }) {}
public function hello()
{
trace("Hi there");
}
}
#:generic
#:genericBuild(FluentMacro.build())
class Fluent<T:Constructible<Dynamic -> Void>>
{
private var parent:Null<Fluent>;
private var base:T;
public function new(parent:Null<Fluent>, options:Dynamic)
{
this.parent = parent;
this.base = new T(options);
}
public function end()
{
if(parent != null) {
return parent;
}
throw 'Top level already reached';
}
}
class FluentMacro
{
public static function build()
{
//Get "T" public methods
//Add them to class calling this genericBuild method (in this example to Fluent_Foo)
//Modify them so they return "this"
}
}
I know that I can't use #:build as all I'd get from Context.getLocalType would be TInst(Fluent,[TInst(Fluent.T,[])]).
However, I'm not completely understanding haxe manual on generic builds - they are under the same section "Type building macros" as normal #:build, yet the build method is expected to return ComplexType, and not an array of fields. Is it possible at all to add fields in #:genericBuild?
Thank you
That was a bit more complex than I anticipated, but I did it. For other people to use: https://github.com/Misiur/Fluent

interfaces in java - MyClass is not abstract and does not override abstract method eq(Object) in MyClass error

I know there is a Comparable interface, trying to figure out how to write my own.
Here's the interface
public interface MyComparable {
public boolean lt(Object other);
}
and a class that implements it and packages an int (yes, I know there is an Integer class)
public class MyInteger implements MyComparable {
private int value;
public MyInteger(int v)
{ value = v; }
public void set(int v)
{ value = v; }
public int get()
{ return value; }
public boolean lt(MyInteger other)
{ return get() < other.get(); }
}
I get " MyInteger is not abstract and does not override abstract method eq(Object) in MyInteger error". MyComparable doesn't declare an eq method. So it's comping from the superclass but I don't understand.