Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Aldacron

Member Since 16 Oct 1999
Offline Last Active Yesterday, 06:54 PM

Posts I've Made

In Topic: Best Java Book For Complete Beginners

04 January 2015 - 05:13 AM

I've heard very good things about Herbert Schildt's "Java, A Beginner's Guide." The 5th edition covers Java 7, which is a good place to start. Your friend can move on up to Java 8's new functional features after getting comfortable with 7. Besides, it's going to be a while before 8 becomes a major part of the Java ecosystem. And you can tell your friend that Java is perfectly fine for games :-)


In Topic: Problem with a getter function

01 January 2015 - 10:30 PM

The foo in your main function is *not* the same instance as the foo in your Moo instance. They are two different objects. Change your main method to this to see what I mean:
 

#include <iostream>


#include "Moo.h"


using std::cout;
using std::endl;



int main()

{

    bool running = true;

    Foo foo;

    while(running)

    {

        foo.Update();

        foo.Show();


    }


    system("Pause");


    return 0;

}

 

You will need to call moo.foo.Update() to do what you want, either by adding an update method to Moo that calls it internally or some other means.


In Topic: Cannot grasp the concept of delegation

30 December 2014 - 05:38 PM


I think I am sort of getting it. When you said "member" is that the same as saying Java objects?

I'm referring to  class member variables.


class MyClass {

    int thisIsAMember;
    String thisIsAlsoAMember;


   void thisIsAMethod() {}

}



For delegation, you obviously can't delegate to a value type (int, float and so on), so the member must be an object instance.


In Topic: Cannot grasp the concept of delegation

30 December 2014 - 12:05 AM

I think the bit about multiple-inheritance is distracting you. The main point was given in the Cat example at the bottom.

If you look at that sample code, the cat class has a member 'sound' which implements the ISoundBehavior interface. That interface has a 'makeSound' method. Cat also has a 'makeSound' method, but it doesn't actually make a sound itself. Instead, it "delegates" to the sound member, by calling sound.makeSound.

It's just like when a manager in an office gets assigned a task from a superior. The manager isn't likely going to do all of the work related to the task, instead delegating it to the appropriate team members. This is the same thing -- Cat doesn't actually make a sound, but delegates to a member. Notice that the Cat class also has a setSoundBehavior method. This means you can change the sound behavior of a Cat instance from MeowSound to RoarSound. If Cat had a concrete implementation of makeSound, meaning Cat itself made the meow sound and didn't delegate to a member, this wouldn't be possible.

Now, let's look at that line "Delegation is like inheritance done manually through composition." All that means is that delegation simulates inheritance.

Let's say that class A has a method "makeFoo" and class B has a method "makeBar." Now you want class C to have the same methods and to be treated as both an A and a B -- meaning any method that takes an A and any method that takes a B can both take a C. In C++, you could use multiple inheritance. That simply means that C inherits from both A and B, so it has both makeFoo and makeBar methods -- C is an A and C is a B.

In Java, multiple inheritance is not an option -- you only have single inheritance. However, we do have multiple interface inheritance in Java. You can inherit from exactly one class, but as many interfaces as you want. The Single Inheritance with Delegation example they give is basically saying that C inherits from A, so has all of A's methods; and C has a member of type B, plus the same interface as B. All calls to the methods on C that have B's interface are delegated to the B member.

I don't think that's a good example, though. So take a look at this one.

public interface A {
    void makeFoo();

}


public interface B {

    void makeBar();

}


class AImpl implements A {

    public void makeFoo() {

        System.out.println( "I made a Foo." );

    }

}


class BImpl implements B {

    public void makeBar() {

        System.out.println( "I made a Bar" );

    }

}


class C implements A, B {

    private A a = new AImpl();

    private B b = new BImpl();


    public void makeFoo() {

        // Delegate to a

        a.makeFoo();

    }


    public void makeBar() {

        // Delegate to b

        b.makeBar();

    }
}

 

So now C can be passed around as an A or as a B and can behave as an AImpl and a BImpl. In C++, you would be able to inherit directly from AImpl and BImpl, but in Java you can only simulate it using delegates. Even better, if you want to use a different implementation of A or B, such as AImpl2 or BImpl33, you can add setA and setB methods to change behavior at runtime.


In Topic: Minimalist Programming Environment

14 December 2014 - 10:21 PM

I'm one of those people SeanMiddleDitch doesn't understand :) I have the VS 2013 Community Edition installed but I don't use it. I find it really just slows me down since I'm not used to it. On the other hand, I don't really want to get used to it. I see no benefit from it over what I already do. I don't use vim or emacs either. I use Sublime Text, a language-appropriate build tool (premake5 for C or C++) and a command prompt (more often than not, my command prompt is via MSYS) for just about everything I code, regardless of language (the exception being Java).


PARTNERS