can we inherit more then on class in asp.net aspx page?
I want to inherit more than one class is there any method?
For instance in login.aspx page:
<%@ page language="c#" codefile="nishant.aspx.cs" autowireup="true" inherit="nishant"%>
now code behind file
nishant.aspx.cs:
class nish开发者_StackOverflow中文版ant
{
//code...
}
class bill
{
//code.....
}
Now, I want to inherit bill class then how I will ?
.NET does not support multiple inheritance, this includes asp.net, so no, this is not possible.
You can have your nishant
class inherit from the bill
class or the other way around, if you want to share functionality. You page can then inherit from the inheriting class and access the functionality of both.
Another option is to inherit from one class and implement an interface (or several interfaces), but the fact that you can implement more than one interface is not the same as multiple inheritance.
There are other things that can be done, depending on what exactly you are trying to achieve (I am primarily thinking about composition versus inheritance).
Multiple inheritance is not allowed. The only way is:
public class Bill : Page
{ }
public class Nishant : Bill
{ }
But rather you should think about your design. Such approach is usually not needed.
No. By nature, .Net allows only single inheritance. At best you could implement an interface, but you will still have to have the code in your nishant class or extract the functionality into your bill class and make function calls.
Although in the case you mention, this is not actually multiple inheritance. Your nishant class must be of type System.Web.UI.Page
. So if you create a library with a "bill class", you can then inherit it.
public class bill : System.Web.UI.Page
{
// Your custom code
}
///
public class nishant : bill
{
}
.NET does not support multiple inheritance (one class that inherit from two or more classes).
However you can have as many parent class as you want. Have a look at the decorator pattern.
Or use interfaces, you can have more than one.
In short: implement interfaces and use extension methods
Implementing interfaces might be the way to go, really depends on the functionality you want to inherit. You can read about inheritance and interfaces here: http://msdn.microsoft.com/en-us/library/ms973861.aspx
When you implement interfaces, but don't want to duplicate the same code into every class that implements a certain interface, you can also write extension methods for the interfaces. Read more about extension methods here: http://msdn.microsoft.com/en-us/library/bb384936.aspx
精彩评论