[ Pobierz całość w formacie PDF ]
.Where do you get an instance of a class to assign to a variable in the first place? The answer isthrough the use of the new operator.We'll examine object creation a little later in the chapter.2.1.5 InheritanceJava classes are arranged in a parent-child hierarchy, in which the parent and child are known asthe superclass and subclass, respectively.We'll explore these concepts fully in Chapter 6.InJava, every class has exactly one superclass (a single parent), but possibly many subclasses.The only exception to this rule is the Object class, which sits atop the entire class hierarchy; ithas no superclass.The declaration of our class in the previous example uses the keyword extends to specify thatHelloJava1 is a subclass of the JComponent class:public class HelloJava1 extends javax.swing.JComponent {.}A subclass may be allowed to inherit some or all of the variables and methods of its superclass.Through inheritance, the subclass can use those variables and methods as if it has declaredthem itself.A subclass can add variables and methods of its own, and it can also override themeaning of inherited variables and methods.When we use a subclass, overridden variables andmethods are hidden (replaced) by the subclass's own versions of them.In this way, inheritanceprovides a powerful mechanism whereby a subclass can refine or extend its superclass.For example, the hypothetical spreadsheet class might be subclassed to produce a new scientificspreadsheet class with extra mathematical functions and special built-in constants.In this case,the source code for the scientific spreadsheet might declare methods for the added mathematicalfunctions and variables for the special constants, but the new class automatically has all thevariables and methods that constitute the normal functionality of a spreadsheet; they are inheritedfrom the parent spreadsheet class.This means the scientific spreadsheet maintains its identity asa spreadsheet, and we can use it anywhere the simpler spreadsheet is used.Our HelloJava1 class is a subclass of the JComponent class and inherits many variables andmethods not explicitly declared in our source code.These members operate in the same way asthe ones we add or override.2.1.6 The JComponent ClassThe JComponent class provides the framework for building user interface components (calledcontrols or widgets in other windowing systems).Particular components, such as buttons, labels,and list boxes, are implemented as subclasses of JComponent.We override methods in such a subclass to implement the behavior of our particular component.This may sound restrictive, as if we are limited to some predefined set of routines, but that is notthe case at all.Keep in mind that the methods we are talking about are means of interacting withthe windowing system.A realistic application might involve hundreds or even thousands ofclasses, with legions of methods and variables and multiple threads of execution.The vastmajority of these are related to the particulars of our job.The inherited methods of theJComponent class, and of other predefined classes, serve as a framework on which to hangcode that handles certain types of events and performs special tasks.The paintComponent( ) method is an important method of the JComponent class; weoverride it to implement the way our particular component displays itself on the screen.Thedefault behavior of paintComponent( ) doesn't do any drawing at all; here, we're overridingpaintComponent( ) to do something interesting.We don't override any of the other inheritedmembers of JComponent because they provide basic functionality and reasonable defaults forthis (trivial) example.As HelloJava grows, we'll delve deeper into the inherited members and useadditional methods.We will also add some application-specific methods and variables for theneeds of HelloJava.JComponent is really the tip of another iceberg called SwingSwing.Swing is Java's userinterface toolkit; we'll discuss it in some detail in Chapter 13 through Chapter 18.2.1.7 Relationships and Finger PointingWe can correctly refer to HelloJava1 as a JComponent because subclassing can be thought ofas creating an "is a" relationship, in which the subclass is a kind of its superclass.HelloJava1 istherefore a kind of JComponent.When we refer to a kind of object, we mean any instance of thatobject's class or any of its subclasses.Later, we will look more closely at the Java class hierarchyand see that JComponent is itself a subclass of the Container class, which is further derivedfrom a class called Component , and so on, as shown in Figure 2.2.Figure 2.2.Part of the Java class hierarchyIn this sense, a HelloJava1 object is a kind of JComponent, which is a kind of Container,and each of these can ultimately be considered to be a kind of Component.It's from theseclasses that HelloJava1 inherits its basic graphical user interface functionality and the ability tohave other graphical components embedded within it.Component is a subclass of the top-level Object class, so all of these classes define kinds ofObjects.Every other class in the Java API inherits behavior from Object, which defines a fewbasic methods, as you'll see in Chapter 7.We'll continue to use the word object (lowercase o) ina generic way to refer to an instance of any class; we'll use Object to refer specifically to thatclass.2.1.8 PackagesIn our previous example, the JComponent class is referenced by its fully qualified namejavax.swing.JComponent:public class HelloJava1 extends javax.swing.JComponent {.}The prefix on the class name identifies it as belonging to the javax.swing package
[ Pobierz całość w formacie PDF ]
-
Menu
- Index
- Wylie Jonathan Słudzy Arki Tom 3 Magiczne Dziecko
- Jonathan Kellerman Bomba zegarowa (Alex Delaware 05)
- Jonathan Nasaw Dziewczyny, których pożšdał
- Carroll Jonathan Kosci ksiezyca
- Jonathan Swift Podróże Gulliwera
- Carroll Jonathan Dziecko na niebie
- Jonathan Carroll Kraina Chichów
- Aldiss Brian W Wiosna Helikonii (SCAN dal 918)
- Bishop Anne Efemera 02 Belladonna
- Onfray Michel Trattato di ateologia
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- legator.pev.pl