Content deleted Content added
m iw +zh |
remove stray text and use wiki instead of html pre |
||
Line 46:
From another point of view, subclasses are not required to be disjoint. Then there is no concept of a most-derived class, and all types in the inheritance hierarchy that are types of the instance are considered to be equally types of the instance. This view is based on a dynamic classification of objects, such that an object may change its class at runtime. Then object's class is considered to be its ''current'' structure, but changes to it are allowed. The basis for allowing object's class to change is performance. It's more efficient to allow changes to object's type, since references to the existing instances do not need to be replaced with references to new instances when the class of the object changes. However, this ability is not readily available in all programming languages.
==Reasons for implementing classes==
Line 54 ⟶ 53:
Another reason for using classes is to simplify the relationships of interrelated data. Rather than writing code to repeatedly draw a GUI window on the terminal screen, it is simpler to represent the window as an object and tell it to draw itself as necessary. With classes, GUI items that are similar to windows (such as dialog boxes) can simply inherit most of their functionality and data structures from the window class. The programmer then need only add code to the dialog class that is unique to its operation. Indeed, GUIs are a very common and useful application of classes, and GUI programming is generally much easier with a good class framework.
==
An '''abstract class''', or ''abstract base class'' (ABC), is one that is designed ''only'' as a [[class (object-oriented programming)|parent class]] and from which [[class (object-oriented programming)|child classes]] may be derived, and which is not itself suitable for [[instance|instantiation]]. Abstract classes are often used to represent [[abstract]] concepts or entities. The incomplete features of the abstract class are then shared by a group of sibling sub-classes which add different variations of the missing pieces. In C++, an abstract class is
Line 66 ⟶ 65:
==Object-based programming==
Some languages have objects, but no classes;
==C++==
Line 85 ⟶ 84:
=== Examples in C++ ===
==== Example 1 ====
class example {
// this is a class
};
This example shows how to define a [[C Plus Plus|C++]] class. It has no data, and performs no functions; it only contains the comment, "this is a class".
Line 112 ⟶ 111:
==== Example 3 ====
#include <string>▼
using std::string;▼
▲ #include <string>
class InetMessage▼
▲ using std::string;
{▼
▲ class InetMessage
▲ {
public:▼
▲ public:
string subject () const;
};▼
string to () const;
string from () const;
▲ };
</pre>
=== Examples in Java ===
==== Example 1 ====
public class Example1
{
// This is a Java class, it automatically extends the class Object
}
This example shows the simplest [[Java programming language|Java]] class possible.
==== Example 2 ====
protected int data
}▼
{▼
// This is a constructor for the class. It does not have a return type.
▲ }
{▼
return data;▼
}▼
public void setData(int d)
data = d;
}
}▼
▲ public int getData()
▲ {
▲ return data;
▲ }
▲ public void setData(int d)
▲ {
▲ data = d;
▲ }
▲}
This example shows a class that has a defined constructor, one member data, and two accessor methods for that member data. It extends the previous example's class. Note that in Java all classes automatically extend the class Object. This allows you to write generic code to deal with objects of any type.
|