Author Topic: Let's learn OOP!  (Read 13971 times)

0 Members and 1 Guest are viewing this topic.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Let's learn OOP!
« Reply #30 on: March 15, 2011, 08:40:19 pm »
Something I forgot to mention about generics: You can have more than one generic type. An example would be Dictionary<K, V>.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline qazz42

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1130
  • Rating: +30/-12
  • hiiiiiiiii
    • View Profile
Re: Let's learn OOP!
« Reply #31 on: March 15, 2011, 09:09:19 pm »
Hmm, for C# my tips are keep your classes (when instantiating as I have a habit of doing, ALOT) clean and make sure vars and methods and such do not get mixed up (Method overloading (something I myself dislike doing, but I am just lazy) and making important vars private vars shall help you with this `-`)

Random other things I can think of are

1. Use Constructors/destructors to your advantage
2. Deirving from System.Windows.Forms is something you will do alot for GUI programs `-`

Eh, some of these are probably wrong, but they are what I do while programming in C#


EDIT: as I have never even touched Java, I probably just mentioned stuff you Java people know, beats me  :hyper:
« Last Edit: March 15, 2011, 09:10:02 pm by qazz42 »

Offline Compynerd255

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +53/-4
  • Betafreak Games
    • View Profile
    • Betafreak Games
Re: Let's learn OOP!
« Reply #32 on: March 16, 2011, 10:41:12 am »
The Diamond operator (<>) is in C# as well, and it is used the same way. Also, C# does have a lot of Java's mechanics, but they are written differently (e.g. public class Square extends Quadrilateral is written as public class Square : Quadrilateral but it has the same function).

Here's another topic I want to write about: properties! Properties are a more elementary item in OOP, and their purpose is simply to set conditions or hook functions into the getting and setting of variables.

A generic Property is a block with special get and send methods, as shown below (in C#):
Code: [Select]
int foo;
public int Foo {
 get { return foo; }
 set { foo = value; }
}
Then, when objects reference the class, they reference the property Foo, which gets and sets foo.

Now, let's set some conditions on access to Foo (these are all within the definition of Foo):
Code: [Select]
get { return foo; }
private set { foo = value; }
This makes it so that outside forces referencing the object cannot set the value of Foo, only read it.

Code: [Select]
get { return foo; }
set { if (value < 100) foo = value; }
This restricts the value of Foo to values less than 100.

Code: [Select]
get { return foo; foo = 0; }
set {
 foo = value;
 RickRoll();
}
This code sets foo to zero after its value is read, and also calls the method RickRoll when foo is set.

Properties don't even have to be inimately attached to specific variables. For example:
Code: [Select]
public int Area {
get { return width*height; }
}
When the property Area is read, it will always return width * height;

Some advice on using Properties:
1. If you are just going to do a public Get and Set method with no restrictions, just declare a lone, public variable. It's faster.
2. Never make get less accessible than set. You won't be able to set it relative to itself.
3. Capitalize the names of public members, and leave private members as lowercase. This will help you seperate the two.
4. Use properties. Realize their sheer power!
The Slime: On Hold, preparing to add dynamic tiles

Axe Eitrix: DONE

Betafreak Games: Fun filled games for XBox and PC. Check it out at http://www.betafreak.com



Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Let's learn OOP!
« Reply #33 on: March 16, 2011, 10:35:03 pm »
Java has no built-in syntax for properties, so things work slightly differently. You have to declare two methods, one for getting (typically called "get{variable}()" or "is{variable}()" for booleans), and one for setting (Typically "set{variable}()").
An example:
Code: [Select]
class Properties {
    private int property_;
    public int getProperty() {
        return property_;
    }
    private void setProperty(int property) {
        property_ = property;
    }
}
Of course, you would probably check values or other things, like compynerd said. But that's the basic outline. (Note that setProperty(int) is private. You can do anything with these that you can with other methods, because they are methods. ;))
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Let's learn OOP!
« Reply #34 on: March 16, 2011, 10:55:38 pm »
Code: [Select]
get { return foo; foo = 0; }
set {
 foo = value;
 RickRoll();
}
This code sets foo to zero after its value is read, and also calls the method RickRoll when foo is set.
I'm not sure if foo = 0; gets executed after a return...

3. Capitalize the names of public members, and leave private members as lowercase. This will help you seperate the two.
I got taught to prefix m_ in front of private members instead of the lowercase thing, it's more readable.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Compynerd255

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +53/-4
  • Betafreak Games
    • View Profile
    • Betafreak Games
Re: Let's learn OOP!
« Reply #35 on: March 17, 2011, 10:33:32 am »
Code: [Select]
get { return foo; foo = 0; }
set {
 foo = value;
 RickRoll();
}
This code sets foo to zero after its value is read, and also calls the method RickRoll when foo is set.
I'm not sure if foo = 0; gets executed after a return...
Oops, you're right. I overlooked that. However, the set method will still RickRoll you. Oh, and here's a better example of something you could do in the set method:
Code: [Select]
set {
 foo = value;
 if (foo > 9000)
 Console.Writeline("It's over 9000!");
}
This writes "It's over 9000!" to the console (DOS) window whenever foo is set to a value over 9000.
The Slime: On Hold, preparing to add dynamic tiles

Axe Eitrix: DONE

Betafreak Games: Fun filled games for XBox and PC. Check it out at http://www.betafreak.com