Content deleted Content added
Notifying of internal link to section Abstract and concrete classes from PHP |
→Examples: made consistant -- four spaces for indents (removed tab characters), used <pre></pre> tags |
||
Line 184:
==== Example 1 ====
<pre>
#include <string>
class Hello
{
std::string what;
public:
Hello(const char* s) {
}
</pre>
This example shows how to define a [[C++]] class named "Hello". It has a private string attribute named "what", and a public method named "say".
Line 215 ⟶ 217:
==== Example 2 ====
<pre>
class MyAbstractClass
{
public:
virtual void MyVirtualMethod() = 0;
};
class MyConcreteClass : public MyAbstractClass
{
public:
void
{
//do something </pre>
An object of class MyAbstractClass cannot be created because the function MyVirtualMethod has not been defined (the =0 is C++ syntax for a pure virtual function, a function that must be part of any derived concrete class but is not defined in the abstract base class. The MyConcreteClass class is a concrete class because its functions (in this case, only one function) have been declared and implemented.
Line 234 ⟶ 238:
==== Example 3 ====
#include <string> string m_subject, m_to, m_from;
InetMessage (const string& subject,
const string& to,
const string& from) : m_subject(subject), m_to(to), m_from(from) { }
string subject () const { return m_subject; }
string to () const { return m_to; }
string from () const { return m_from; }
virtual void printTo(std::ostream &out)
{
out << "Subject: " << m_subject << std::endl;
out << "From: " << m_from << std::endl;
out << "To: " << m_to << std::endl;
}
</pre>
=== Java ===
:''For more information on the programming language, see [[Java (programming language)]]. For more information on Java .class files, see [[Class (file format)]].''
==== Example 1 ====
<pre>
public class Example1
{
// This is a Java class, it automatically extends the class Object
public static void main (String args[]) {
System.out.println("Hello world!");
}
}
</pre>
This example shows the simplest [[Java (programming language)|Java]] class possible.
Line 276 ⟶ 282:
==== Example 2 ====
<pre>
public class Example2 extends Example1
{
// This is a class that extends the class created in Example 1.
protected int data;
public Example2()
{
// This is a constructor for the class. It does not have a return type.
data = 1;
}
public int getData()
{
return data; }
public void setData(int d)
{
data = d;
}
</pre>
This example shows a class that has a defined constructor, one member data, an accessor method (getData) and a Modifier method (setData) 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.
Line 302 ⟶ 310:
=== REALbasic ===
==== Example 1====
<pre>
Class Hello
Private Dim what as String
Sub Constructor( s as String )
what = s
End Sub
Sub Say()
MsgBox "Hello " + what + EndOfLine
End Sub
End Class
Sub App.Open()
dim h as new Hello( "Foobar" )
h.Say
</pre>
This example is a port of the C++ example above. It demonstrates how to make a class named Hello with a private property named what. It also demonstrates the proper use of a constructor, and has a public method named Say. It also demonstrates how to instantiate the class and call the Say method.
Line 326 ⟶ 337:
<?php
class A {
public function foo() {
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}
?>
Line 340 ⟶ 351:
====Example 2====
<pre>
<?php
class date {
private $date;
public function __construct() {
$this->date = date('c');
}
private function getDate() {
return $this->date;
}
public function printDate() {
echo 'Todays Date is ' . $this->getDate() . '\n';
}
}
?>
Line 363 ⟶ 375:
===C#===
====Example 1====
<pre>
using System;
Line 413 ⟶ 426:
class Cart {
}
}
}
}
}
}
}
</pre>
|