如何使用 Visual Studio 2010 在 C# 中为 ASP.NET 创建子类?
问问题
98140 次
4 回答
38
你是这个意思吗?
public class Foo
{}
public class Bar : Foo
{}
在这种情况下,Bar 是子类。
于 2010-11-22T13:25:42.267 回答
31
这是一个编写 ParentClass 然后创建 ChildClass 作为子类的示例。
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
输出:
父构造函数。 子构造函数。 我是家长班。
我没有重写 .Net 继承的另一个示例,而是从C Sharp Station 网站复制了一个不错的示例。
于 2010-11-22T13:26:02.680 回答
6
你的意思是类继承?
public class SubClass: MasterClass
{
}
于 2010-11-22T13:27:31.447 回答
1
这个页面解释得很好:
public class SavingsAccount : BankAccount
{
public double interestRate;
public SavingsAccount(string name, int number, int balance, double rate) : base(name, number)
{
accountBalance = balance;
interestRate = rate;
}
public double monthlyInterest()
{
return interestRate * accountBalance;
}
}
static void Main()
{
SavingsAccount saveAccount = new SavingsAccount("Fred Wilson", 123456, 432, 0.02F);
Console.WriteLine("Interest this Month = " + saveAccount.monthlyInterest());
}
If the monthlyInterest
method already exists in the BankAccount
class (and is declared abstract
, virtual
, or override
) then the SavingsAccount
method definition should include override
, as explained here. Not using override
to redefine such class methods will result in a CS0108 compiler warning, which can be suppressed by using new
as confusingly stated here.
于 2015-01-26T17:21:20.593 回答