Java Tutorial/JSP/Class in JSP Page
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Calling Superclass Constructors
- 2 Creating a Java Interface
- 3 Overloading Methods
- 4 Overriding Methods in derived class
- 5 Runtime Polymorphism
- 6 Using Abstract Classes
- 7 Using Inheritance
- 8 Using Parameterized Constructors
- 9 Using Restricted Access
- 10 Using Superclass Variables With Subclassed Objects
- 11 Working With Constructors and Inheritance
Calling Superclass Constructors
<HTML>
<HEAD>
<TITLE>Calling Superclass Constructors</TITLE>
</HEAD>
<BODY>
<H1>Calling Superclass Constructors</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class a
{
a() throws java.io.IOException
{
localOut.println("In a\"s constructor...<BR>");
}
}
class b extends a
{
b() throws java.io.IOException
{
localOut.println("In b\"s constructor...<BR>");
}
}
%>
<%
localOut = out;
b obj = new b();
%>
</BODY>
</HTML>
Creating a Java Interface
<HTML>
<HEAD>
<TITLE>Creating a Java Interface</TITLE>
</HEAD>
<BODY>
<H1>Creating a Java Interface</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
interface Printem
{
void printText() throws java.io.IOException;
}
class a implements Printem
{
public void printText() throws java.io.IOException
{
localOut.println("Hello from JSP!");
}
}
%>
<%
localOut = out;
a printer = new a();
printer.printText();
%>
</BODY>
</HTML>
Overloading Methods
<HTML>
<HEAD>
<TITLE>Overloading Methods</TITLE>
</HEAD>
<BODY>
<H1>Overloading Methods</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
void printText() throws java.io.IOException
{
localOut.println("Hello!<BR>");
}
void printText(String s) throws java.io.IOException
{
localOut.println(s + "<BR>");
}
%>
<%
localOut = out;
printText();
printText("Hello from JSP!");
%>
</BODY>
</HTML>
Overriding Methods in derived class
<HTML>
<HEAD>
<TITLE>Overriding Methods in derived class</TITLE>
</HEAD>
<BODY>
<H1>Overriding Methods</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class BaseClass
{
public void print() throws java.io.IOException
{
localOut.println("print in base...<BR>");
}
}
class DerivedClass extends BaseClass
{
public void print() throws java.io.IOException
{
localOut.println("print in Derived class...<BR>");
}
}
%>
<%
localOut = out;
out.println("Creating an animal object...<BR>");
BaseClass a = new BaseClass();
a.print();
out.println();
out.println("Creating a trout object...<BR>");
DerivedClass t = new DerivedClass();
t.print();
%>
</BODY>
</HTML>
Runtime Polymorphism
<HTML>
<HEAD>
<TITLE>Runtime Polymorphism</TITLE>
</HEAD>
<BODY>
<H1>Runtime Polymorphism</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class BaseClass
{
public void print() throws java.io.IOException
{
localOut.println("Hello from BaseClass...<BR>");
}
}
class DerivedClass1 extends BaseClass
{
public void print() throws java.io.IOException
{
localOut.println("Hello from DerivedClass1...<BR>");
}
}
class DerivedClass2 extends BaseClass
{
public void print() throws java.io.IOException
{
localOut.println("Hello from DerivedClass2...<BR>");
}
}
class DerivedClass3 extends BaseClass
{
public void print() throws java.io.IOException
{
localOut.println("Hello from DerivedClass3...<BR>");
}
}
%>
<%
localOut = out;
BaseClass a1 = new BaseClass();
DerivedClass1 b1 = new DerivedClass1();
DerivedClass2 c1 = new DerivedClass2();
DerivedClass3 d1 = new DerivedClass3();
BaseClass baseClassVariable;
baseClassVariable = a1;
baseClassVariable.print();
baseClassVariable = b1;
baseClassVariable.print();
baseClassVariable = c1;
baseClassVariable.print();
baseClassVariable = d1;
baseClassVariable.print();
%>
</BODY>
</HTML>
Using Abstract Classes
<HTML>
<HEAD>
<TITLE>Using Abstract Classes</TITLE>
</HEAD>
<BODY>
<H1>Using Abstract Classes</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
abstract class a
{
abstract String getText() throws java.io.IOException;
public void printem() throws java.io.IOException
{
localOut.println(getText());
}
}
class b extends a
{
String getText() throws java.io.IOException
{
return "Hello from JSP!";
}
}
%>
<%
localOut = out;
b bObject = new b();
bObject.printem();
%>
</BODY>
</HTML>
Using Inheritance
<HTML>
<HEAD>
<TITLE>Using Inheritance</TITLE>
</HEAD>
<BODY>
<H1>Using Inheritance</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class BaseClass
{
public void start() throws java.io.IOException
{
localOut.println("Starting...<BR>");
}
}
class DerivedClass extends BaseClass
{
public void drive() throws java.io.IOException
{
localOut.println("Deriving...<BR>");
}
}
%>
<%
localOut = out;
out.println("Creating an DerivedClass...<BR>");
DerivedClass a = new DerivedClass();
a.start();
a.drive();
%>
</BODY>
</HTML>
Using Parameterized Constructors
<HTML>
<HEAD>
<TITLE>Using Parameterized Constructors</TITLE>
</HEAD>
<BODY>
<H1>Using Parameterized Constructors</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class a
{
a() throws java.io.IOException
{
localOut.println("In a\"s constructor...<BR>");
}
}
class b extends a
{
b(String s) throws java.io.IOException
{
localOut.println("In b\"s String constructor...<BR>");
localOut.println(s);
}
}
%>
<%
localOut = out;
b obj = new b("Hello from JSP!<BR>");
%>
</BODY>
</HTML>
Using Restricted Access
<HTML>
<HEAD>
<TITLE>Using Restricted Access</TITLE>
</HEAD>
<BODY>
<H1>Using Restricted Access</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class BaseClass
{
protected void start() throws java.io.IOException
{
localOut.println("Starting...<BR>");
}
}
class DerivedClass extends BaseClass
{
public void drive() throws java.io.IOException
{
localOut.println("Driving...<BR>");
}
}
%>
<%
localOut = out;
out.println("Creating an DerivedClass...<BR>");
DerivedClass a = new DerivedClass();
a.start();
a.drive();
%>
</BODY>
</HTML>
Using Superclass Variables With Subclassed Objects
<HTML>
<HEAD>
<TITLE>Using Superclass Variables With Subclassed Objects</TITLE>
</HEAD>
<BODY>
<H1>Using Superclass Variables With Subclassed Objects</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class BaseClass
{
public void start() throws java.io.IOException
{
localOut.println("Starting...<BR>");
}
}
class DerivedClass1 extends BaseClass
{
public void fly() throws java.io.IOException
{
localOut.println("Flying...<BR>");
}
}
class DerivedClass2 extends DerivedClass1
{
public void fly() throws java.io.IOException
{
localOut.println("Flying...<BR>");
}
}
%>
<%
localOut = out;
out.println();
out.println("Creating a DerivedClass2 object...<BR>");
BaseClass p = new DerivedClass2();
p.start();
%>
</BODY>
</HTML>
Working With Constructors and Inheritance
<HTML>
<HEAD>
<TITLE>Working With Constructors and Inheritance</TITLE>
</HEAD>
<BODY>
<H1>Working With Constructors and Inheritance</H1>
<%!
javax.servlet.jsp.JspWriter localOut;
class BaseClass
{
BaseClass() throws java.io.IOException
{
localOut.println("In a\"s constructor...<BR>");
}
BaseClass(String s) throws java.io.IOException
{
localOut.println("In a\"s String constructor...<BR>");
localOut.println(s);
}
}
class DerivedClass extends BaseClass
{
DerivedClass(String s) throws java.io.IOException
{
super(s);
localOut.println("In b\"s String constructor...<BR>");
localOut.println(s);
}
}
%>
<%
localOut = out;
DerivedClass obj = new DerivedClass("Hello from JSP!<BR>");
%>
</BODY>
</HTML>