12
Inner and Anonymous Classes • Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and write procedural-like code. • Often times, no one but the enclosing class cares about an object. In this case, you may consider using inner or anonymous classes.

Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Embed Size (px)

Citation preview

Page 1: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Inner and Anonymous Classes

• Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and write procedural-like code.

• Often times, no one but the enclosing class cares about an object. In this case, you may consider using inner or anonymous classes.

Page 2: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and
Page 3: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Intro to Data Structures in Java

• Single Lined List• Stack• Binary Tree• others...

Page 4: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

WindowsBuilderPro by Google

• http://download.eclipse.org/windowbuilder/WB/release/R201109201200/3.7/

• b,y,c,n,s,l,d,f,str; get rid of these in Window || Preferences || Java || Code Style || Fields. Otherwise, they will hose you in WB.

Page 5: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and
Page 6: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Constructors• Constructors are optional. If your class is just a static

driver; i.e. it just has a static main method, then no need to instantiate it; and no need for constructor.

• If your class is a "java bean" -- a class that models a real world object (noun) in your system, then it should have a constructor. House, Car, Person, etc.

• If you don't provide a constructor, a no-args constructor is provided automatically.

• You can, and often want, more than one constructor with different signatures.

• You call a constrcutor with the 'new' keyword.

Page 7: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Model a system• Object-oriented programming is about

modelling a system. • Write the problem out as requirements -- this

will essentially be a math world problem (your favorite kind to solve!)

• Your computer objects map directly to real-world objects (nouns).

• Your methods map directly to real-world actions (verbs).

Page 8: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Write a very simple application for a contact manager. The the sake of simplicity, each contact will have a name and a phone number only. The user should be able to create new contacts and diplay all contacts.

Build a GUI using WindowBuilder and Swing.

Page 9: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Write a very simple application for computer dictionary. The the sake of simplicity, each entry has a word and a definition. Store the pair in an ArrayList<DictEntry>. Each DictEntry has two strings. Build a GUI using WindowBuilder and Swing.

Page 10: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Describe the system:

The game of BlackJack; a single player plays against the house for money. His bet is consistently $100.00 and he starts with 1,000.00. There is a shoe of six 52-card decks which is reshuffled when the shoe is half used. If the player wins the hand, his gets his bet back plus the amount of the bet. If he loses, he loses the money, and if he gets blackjack, he get's his bet * 1.5.

The player plays against a dealer who must follow the following strict rules; aces are worth 11points only, and the dealer must hit on 16 or below. The player however, is allowed to hit or hold on any hand-value. Furthermore, aces are worth either 1 or 11, whichever is more advantageous.

An initial two hands are dealt on seperate sides of a table consisting of two cards apiece. The dealer's hand displays only one card up. The player has the option to hit, hold, split, double-down, buy insurance, etc. For this initial version of the game, we'll consider only hit, hold, and deal.

blue are nouns (objects or fields)red are verbs (methods)

Page 11: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and

Interfaces

• A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods.

• Interface names often end with "able" to imply that they add to the capabilty of the class.

• An interface is a contract; it defines the methods

Page 12: Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and