ASP.Net/Language Basics/Class Inheritance
Версия от 15:30, 26 мая 2010; (обсуждение)
Inherit a Class (C#)
<%@ Page Language="C#" %>
<script runat="server">
public class Animal {
private int legs;
private string bodyHair;
public int Legs {
get {
return legs;
}
set {
legs = value;
}
}
public string BodyHair {
get {
return bodyHair;
}
set {
bodyHair = value;
}
}
public string Walk() {
return "I"m walking on " + legs + " legs";
}
}
public class Dog : Animal {
public Dog() {
Legs = 4;
}
public string Bark() {
return "Woof";
}
public string Bite() {
return "Chomp Chomp";
}
public string Wag() {
return "Wag Wag";
}
}
public class Person : Animal {
public Person() {
Legs = 2;
}
public string Talk() {
return "yadda yadda yadda";
}
}
void Page_Load(object Sender, EventArgs E) {
Dog d = new Dog();
Response.Write(d.Walk());
Response.Write("<br />");
Response.Write(d.Bark());
Response.Write("<br />");
Person p = new Person();
Response.Write(p.Walk());
}
</script>
<html>
<head>
</head>
<body>
</body>
</html>
Inherits class (C#)
<%@ page Language="c#" runat="server" %>
<script runat="server">
public class ScientificCalculator : Calculator
{
public void SquareRoot()
{
double root = Math.Sqrt(CurrentValue);
Clear();
Add(root);
}
}
public class Calculator
{
private double currentValue;
public double CurrentValue
{
get
{
return currentValue;
}
}
public void Add(double addValue)
{
currentValue += addValue;
}
public void Subtract(double subValue)
{
currentValue -= subValue;
}
public void Multiply(double multValue)
{
currentValue *= multValue;
}
public void Divide(double divValue)
{
currentValue /= divValue;
}
public void Clear()
{
currentValue = 0;
}
}
void Page_Load()
{
ScientificCalculator MyCalc = new ScientificCalculator();
Response.Write("<b>Created a new ScientificCalculator object.</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
MyCalc.Add(23);
Response.Write("<br/><b>Added 23 - MyCalc.Add(23)</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
MyCalc.Subtract(7);
Response.Write("<br/><b>Subtracted 7 - MyCalc.Subtract(7)</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
MyCalc.Multiply(3);
Response.Write("<br/><b>Multiplied by 3 - MyCalc.Multiply(3)</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
MyCalc.Divide(4);
Response.Write("<br/><b>Divided by 4 - MyCalc.Divide(4)</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
MyCalc.SquareRoot();
Response.Write("<br/><b>Square root - MyCalc.SquareRoot()</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
MyCalc.Clear();
Response.Write("<br/><b>Cleared - MyCalc.Clear()</b><br/>");
Response.Write("Current Value = " + MyCalc.CurrentValue);
}
</script>
Inherits class (VB.net)
<%@ page language="vb" runat="server" %>
<script runat="server">
Public Class ScientificCalculator : Inherits Calculator
Public Sub SquareRoot()
Dim root As Double
Dim current As Double = MyBase.CurrentValue
root = Math.Sqrt(current)
MyBase.Clear()
MyBase.Add(root)
End Sub
End Class
Public Class Calculator
Private _current As Double
Public ReadOnly Property CurrentValue As Double
Get
Return _current
End Get
End Property
Public Sub Add(addValue As Double)
_current += addValue
End Sub
Public Sub Subtract(addValue As Double)
_current -= addValue
End Sub
Public Sub Multiply(addValue As Double)
_current *= addValue
End Sub
Public Sub Divide(addValue As Double)
_current /= addValue
End Sub
Public Sub Clear()
_current = 0
End Sub
End Class
Sub Page_Load()
Dim MyCalc As New ScientificCalculator()
Response.Write("<b>Created a new ScientificCalculator object.</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
MyCalc.Add(23)
Response.Write("<br/><b>Added 23 - MyCalc.Add(23)</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
MyCalc.Subtract(7)
Response.Write("<br/><b>Subtracted 7 - MyCalc.Subtract(7)</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
MyCalc.Multiply(3)
Response.Write("<br/><b>Multiplied by 3 - MyCalc.Multiply(3)</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
MyCalc.Divide(4)
Response.Write("<br/><b>Divided by 4 - MyCalc.Divide(4)</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
MyCalc.SquareRoot()
Response.Write("<br/><b>Square root - MyCalc.SquareRoot()</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
MyCalc.Clear()
Response.Write("<br/><b>Cleared - MyCalc.Clear()</b><br/>")
Response.Write("Current Value = " & MyCalc.CurrentValue)
End Sub
</script>