Miscellaneous: inheritance and interfaces

Source: Internet
Author: User

1. C # differences between virtual base class inheritance and interfaces

① An interface does not contain any implementation. The interface does not have a constructor. All members are public. interfaces are used to define classes, classes, and abstract classes. Classes are responsible for the specific implementation of functions. Everything in the interface is abstract.

② Abstract classes are incomplete classes with abstract methods and attributes, and specific methods and attributes.

③ Classes are implemented by methods and attributes.

 

C # A class can only be derived from one class, and multiple interfaces can be implemented. One class is derived from another class and borrowed the implemented functions from the other class. When a class implements an interface, this class makes a commitment that all functions of the interface must be implemented (the class implements the interface and adds a specification to this class, class must comply with all the regulations in the Specification ).

Interfaces are designed for use, while classes are used to implement designed functions.The method in the interface can also contain attributes, indexers, and events. It cannot contain constants, fields, constructors, destructor, and static members. (These are not quite understandable)

 

All abstract things must be implemented:

If a class is derived from another class or implements one or more interfaces, the attributes of all abstract methods in the base class and interface must be overwritten by the derived class (everything in the interface is abstract, abstract classes are partially abstract ).

2. Method of class implementation Interface

Abstract class, used to describe what its derived class is, called the "is-a" principle.
Interface is used to describe what its derived class can do. It is called the "can-do" principle.

3. Differences between abstract classes and interfaces

  

4. An interview on class inheritance and rewriting of the parent class Method

    class A {
public A() {
Print();
}
public virtual void Print() { }
}
class B : A {
int x = 1;
int y;
public B() {
y = 2;
}
public override void Print() {
Console.WriteLine("x=" + x + " y=" + y);
}
}

The question above is what will be output when B is instantiated?

5. Inheritance Analysis in OO

6. An interface case

I have never known what the interface is. After reading a few articles, I finally realized it. Below is a self-written Demo:

// USB Interface
Public interface USB {
Void chargeforphones ();
}

// Nokia mobile phone
Public class Nokia: USB {

# Region USB members

Public void chargeforphones ()
{
Console. writeline ("charge for Nokia phones! ");
}

# Endregion
}

// Motorcycle mobile phone
Public class Moto: USB {

# Region USB members

Public void chargeforphones ()
{
Console. writeline ("charge for Moto phones! ");
}

# Endregion
}

// A dedicated device for charging devices with USB Interfaces
Public class charger {
Public void charge (USB ){
USB. chargeforphones ();
}
}

Class Program
{
Static void main (string [] ARGs)
{
// Now there is a machine dedicated to charging the USB interface device
Charger charger = New Charger ();
// There is another Nokia mobile phone (which uses the USB interface)
USB nokiawithusb = new Nokia ();
// Charge the Nokia mobile phone now
Charger. Charge (nokiawithusb );

// If another motorcycle's mobile phone needs to be charged, you can use this Charging Machine to charge it as long as the USB interface is implemented.
USB motowithusb = new Moto ();
Charger. Charge (motowithusb );
}
}

The method in the USB interface can be understood as what is displayed on the screen when a mobile phone with a USB interface or other mobile phones are charged, each brand will be designed by itself (that is, chargeforphones in the mobile phone's method for implementing the USB interface ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.