Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:27:48+00:00 2026-06-10T05:27:48+00:00

I have an Extension class for handling general functions that’s designed to be used

  • 0

I have an “Extension” class for handling general functions that’s designed to be used dynamically – that is, I want to be able to use it with many different classes and variables (kind of like how you would use something like the Math class). But I’ve encountered a little problem that I can’t seem to figure out (I’ve looked at quite a few forums and websites, but no luck).

Here’s the function from my Extension class:

public void setPlayerFriction(Object objectType, double hspeed, double vspeed){
    objectType obj = new objectType();

    if(obj.hspeed >= GameWindow.friction){
          obj.hspeed -= GameWindow.friction;
    }else if(obj.hspeed <= -GameWindow.friction){
          obj.hspeed += GameWindow.friction;
    }else{
          obj.hspeed = 0;
    }
    if(obj.vspeed >= GameWindow.friction){
          obj.vspeed -= GameWindow.friction;
    }else if(obj.vspeed <= -GameWindow.friction){
          obj.vspeed += GameWindow.friction;
    }else{
          obj.vspeed = 0;
    }
}

And the function’s use in my Player class:

public void runPhysicsEngine(){
    Extensions ext = new Extensions();
    ext.setPlayerFriction(this,hspeed,vspeed);
}

Obviously doing something like obj.var -= GameWindow.friction;, where obj and var are arguments of a method, isn’t the right way to do what I want to do – it just returns the unresolved type error. So what is the right way? How do I access a variable from a dynamic class name?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:27:50+00:00Added an answer on June 10, 2026 at 5:27 am

    I think having an abstract class for your objectType with appropriate getter/setter methods might help. For example:

    public abstract class ParentObject {
    
        private double hSpeed;
        private double vSpeed;
    
        /** Getter/setter methods */
    
        public double gethSpeed() {
            return hSpeed;
        }
        public void sethSpeed(double hSpeed) {
            this.hSpeed = hSpeed;
        }
        public double getvSpeed() {
            return vSpeed;
        }
        public void setvSpeed(double vSpeed) {
            this.vSpeed = vSpeed;
        }
    }
    

    You can then have concrete implementation of your ObjectType classes that extend this ParentObject class. And you would change your setPlayerFriction() method as follows:

    public void setPlayerFriction(ParentObject objectType, double hspeed, double vspeed){
    
        double hSpeed = objectType.gethSpeed();
        if(hSpeed >= GameWindow.friction){
              objectType.sethSpeed(hSpeed - GameWindow.friction);
        }else if(hSpeed <= -GameWindow.friction){
              objectType.sethSpeed(hSpeed + GameWindow.friction);
        }else{
              objectType.sethSpeed(0);
        }
    
        double vSpeed = getvSpeed();
        if(vSpeed >= GameWindow.friction){
              objectType.setvSpeed(vSpeed - GameWindow.friction);
        }else if(obj.vspeed <= -GameWindow.friction){
              objectType.setvSpeed(vSpeed + GameWindow.friction);
        }else{
              objectType.setvSpeed(0);
        }
    }
    

    And to this method, you would basically be sending references of classes that have actually extended your ParentObject class. For example:

    public class ObjImpl extends ParentObject{
        //Concrete implementation
    }
    
    public class ObjImpl2 extends ParentObject{
        //Concrete implementation
    }
    
    public class TestClass{
        public void setPlayerFriction(ParentObject objectType, double hspeed, double vspeed){   
            double hSpeed = objectType.gethSpeed();
            if(hSpeed >= GameWindow.friction){
                  objectType.sethSpeed(hSpeed - GameWindow.friction);
            }else if(hSpeed <= -GameWindow.friction){
                  objectType.sethSpeed(hSpeed + GameWindow.friction);
            }else{
                  objectType.sethSpeed(0);
            }
    
            double vSpeed = getvSpeed();
            if(vSpeed >= GameWindow.friction){
                  objectType.setvSpeed(vSpeed - GameWindow.friction);
            }else if(obj.vspeed <= -GameWindow.friction){
                  objectType.setvSpeed(vSpeed + GameWindow.friction);
            }else{
                  objectType.setvSpeed(0);
            }
        }
    
        public static void main(String[] args) {
            ParentObject object1    = new ObjImpl();
            ParentObject object2    = new ObjImpl2();
    
            setPlayerFriction(object1, 1.0d, 2.0d);
            setPlayerFriction(object2, 2.25d, 2.0d);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to ignore executable files that do not have an extension For example:
I have an extension class that is converting an Observable collection to list and
I have an Objective-c class MyClass. In MyClass.m I have a class extension that
Just curious, I've noticed that some php files have extension 'class.php' or 'html.php'. What
I have an extension method that I'm trying to use where you can use
I have made extension for UIButton class. Now if I use it everything works
I have build a Ruby extension in Objective-C. Now I want to use @throw/@catch
I have a large class, which I have divided into several different class extension
I have a class that extends Application. The class is fairly extensive. When the
This is what I have: class Calendar extends CI_Controller { public $extension; function __construct()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.