聚會時間公告: 因應COSCUP 2011, Kalug 8月份休會一次

七月 6, 2011
» Java Swing basics

In Java Swing system, below are a few basic concepts you need to understand:
  1. Everything appears on screen is basically a component. JComponent is the Swing superclass of all things that draw on screen. For example, JLabel is a built-in JComponent that displays little text string. What you usually do is to create components objects and install it onto screen or their container.
  2. Frames (JFrame) are the outermost components. A JFrame represents a single window. Each JFrame component has a "content pane" JComponent that contains all components in the frame. You can use frame.getContentPane() to get the content pane. Content pane uses a Layout Manager to size and position its components. Frame has a convenience add() and setLayout() that go to its content pane.
  3. Components are placed inside other components which form a nesting hierarchy from outer to inner components. And each container is behind the components it contains. Layout Manager sizes and places those components according to some 'policies' or 'intents' of the layout. Don't call setSize() yourself, the layout manager controls that! Instead, do call setMinimumSize(), setMaximumSize() or setPreferredSize(), to register a preference before the layout manager lays everything out (before calling pack() / setVisible()).
  4. Three basic layout manager: FlowLayout, BoxLayout, BorderLayout. Flow layout arranges components left-right, top-down like text. Box layout aligns components in a line, either vertically or horizontally. Border layout puts main content in the center and decorate with 4 things around the outsize -- north, south, east, west.
  5. Swing thread, or Event-dispatch thread, Composite pattern and Observer/Observable patterns basically form the underlying architecture of Swing GUI system.
  6. A common technique in Java GUI code is to use an anonymous inner class to create a 'listener' class object and register it using addXXXListener() method to a Observable, e.g a button, on the fly.

三月 18, 2011
» [Techonote] Java container anomaly

You can assign a pointer of type sub to a pointer of type super (this is simply a is-a relationship). However, you cannot assign a pointer type container(sub) to a pointer of type container(super) (e.g. List<String> IS NOT a List<Object>).

二月 23, 2011
» [Technotes] Java Inner classes

First thing first, there is some terminology that needs to be explained.

In Java, you can define a class inside another class; such a class is called nested class. Nested classes can be divided into two categories: static or non-static. Static nested classes are simply called static nested classes. Non-static nested classes are called inner classes.

As for nested classes' privileges, inner classes have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class, instead, it can only access to static methods and members of the enclosing (parent) class.

To make the story even more complex, there are 2 other flavors of 'inner classes': local classes and anonymous classes (also called anonymous inner classes). You get to see lots of these two special kinds of inner classes in GUI codes. So you must understand and master them to be able to do GUI programming in Java. To see some concrete examples of these two classes, check out here and there.

In addition, you'd better read the following three web pages carefully before using inner or nested classes:

http://bit.ly/fPoJad - Nested classes introduction
http://bit.ly/dQzVvD - Inner class code example
http://bit.ly/e8h9uR - Summary

Finally, to instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

一月 31, 2011
» Virtual function

In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of OOP.

The purpose of having a virtual function is as follows:

In OOP when a derived class inherits a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class, the method call behaviour is ambiguous.

The distinction between virtual and non-virtual resolves this ambiguity. If the function in question is designated "virtual" in the base class then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

Virtual functions overcome the problems with the type-field solution by allowing the programmer to declare functions in a base class that can be redefined in each derived class.

Programming language supports for virtual function:

C++: virtual methods are declared by using the virtual keyword followed by the function name and the parenthesis ().

Java: In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual. To override a method, use an annotation '@Override' (could be omitted) followed by function definition.

Python: In Python all class functions (methods) are virtual. And no special keywords needed to make overriding or virtualization happen. It just works automatically!

Reference: wiki

support:

biggo.com.tw

biggo.sg

A Django site.