C # learning notes (5) Overloading of intermediate methods, parameters, inheritance and polymorphism, exception handling, namespaces, interfaces, generics

Source: Internet
Author: User

22nd method Overloading

The compiler can automatically select the method to call.
Overloading is not an object-oriented feature, but a convenient feature"
Features
Code

{
Test theTest = new Test ();
// The Compiler automatically determines which function to use.
Int intMax = theTest. max (4, 5 );
Double doubleMax = theTest. max
(4.444, 5.555 );
String stringMax = theTest. max ("no listening
? "," No day is me ");
// Display
MessageBox. Show (intMax. ToString ());
MessageBox. Show (doubleMax. ToString
());
MessageBox. Show (stringMax. ToString
());
}

Public class Test {
// Member Method
Public int max (int x, int y ){
If (x> = y)
Return x;
Else
Return y;
}
Public double max (double x, double y ){
If (x> = y)
Return x;
Else
Return y;
}
Public string max (string str1, string str2)
{
If (str1.Length> = str2.Length ){
Return str1 ;}
Else {return str2 ;}
}
}

23rd method parameters (ref, out, params)

Parameter: parameter in the member method of the class

When we define a method, using some keywords to modify the form parameters will have different effects.
When defining a method: Form Parameter
When calling a method: real parameters

The parameter is not specified. Although params is unreasonable, it is supported.
Public partial class Form1: Form {
Public Form1 (){
InitializeComponent ();
}

Private void Form1_Load (object sender,
EventArgs e ){
/*
Int x = 9, y = 10;
MessageBox. Show ("before calling:" + x + "and
"+ Y );
Function1 (x, y );
MessageBox. Show ("after calling:" + x + "and
"+ Y );
//*/
/*
String s1 = "first string ";
String s2 = "second string ";
MessageBox. Show ("before calling:" "+ s1 +
"" And "" + s2 + ");
Funcion2 (ref s1, ref s2 );
MessageBox. Show ("after calling:" "+ s1 +
"" And "" + s2 + ");
//*/
/*
Int ans;
Add (90, 90, out ans );
MessageBox. Show ("ans:" + ans );
//*/

Double average;
Average = CalculateAverage (4.0, 3.2,
5.7 );
MessageBox. Show ("average of 4.0, 3.2, 5.7
Yes: "+ average );
//*/
Double [] data = {4.0, 3.2, 5.7, 33
};
Average = CalculateAverage (data );
MessageBox. Show ("4.0, 3.2, 5.7, 33 flat
Average Value: "+ average );
}
// Value Parameter
Public void Function1 (int x, int y ){
X = 10000;
Y = 88888;
}
Public void Funcion2 (ref string s1, ref
String s2 ){
S1 = "stepping on who I am ";
S2 = "I won't tell you ";
}
// Output parameters
Public void Add (int x, int y, out int ans ){
Ans = x + y;
}
// Multiple parameters
Public double CalculateAverage (params
Double [] values ){
Double sum = 0;
For (int I = 0; I <values. Length;
I ++)
Sum + = values [I];
Return (sum/values. Length );
}
}

24th inheritance and Polymorphism

:
Virtual
New
Override

Public class Animal
{
Public string word = "";
Public virtual void Introduce ()
{Word = "I am an animal .";}
}
Public class Dog: Animal
{
Public override void Introduce ()
{Word = "I am an Dog .";}
}
// No member method, but word can be assigned a value
// Overwrite the value
Public class Cat: Animal
{
Public override void Introduce ()
{Word = "I am an Cat .";}
}
Virtual: can be inherited
Override: inherited
This is the manifestation of the polymorphism subclass in a variety of forms

The derived class inherits members from its direct base class: method. domain, attribute, event, Index
Display. Apart from constructor and destructor, the derived class implicitly inherits
Member

25th Exception Handling

Exception:
1. The divisor is 0.
2. The accessed file does not exist.
3. Overflow
4. Forced type conversion of errors
5. Network reasons
6. etc.

Try-catch-finally

DivideByZeroException

Throw new ArgumentNullException ();

/*
Try {
Int nTheZero = 0;
Int nResult = 10/nTheZero;
}
Catch (Exception Ex ){
MessageBox. Show ("the divisor cannot be
Zero ");
}
Finally {
MessageBox. Show ("finally ");
}
MessageBox. Show ("the language after executing the try statement
Sentence ");
//*/
/*
Try {
Int nTheZero = 0;
Int nResult = 10/nTheZero;
}
Catch (DivideByZeroException divEx)
{
MessageBox. Show
(DivEx. ToString ());
}
Catch (Exception Ex ){
MessageBox. Show (Ex. ToString
());
}
Finally {
MessageBox. Show ("finally ");
}
MessageBox. Show ("the language after executing the try statement
Sentence ");
//*/
// The child with the exception is not found after matching.
Type should be placed in front of program more accurate
/*
Try {// capture all exceptions
Drop
Int nTheZero = 0;
Int nResult = 10/nTheZero;
}
Catch {
MessageBox. Show ("Catch !! ");
}
//*/
Try {
String s = null;
If (s = null ){
Throw new
ArgumentNullException ();
} // Create an exception by yourself and throw it
Exception
}
Catch {MessageBox. Show ("Accept thrown
Exception ");}
MessageBox. Show ("the language after executing the try statement
Sentence ");

26th lecture namespace

Namespace: a mechanism for organizing classes. For example, we can operate all files
Class in a namespace.

If the two namespaces are named at the same time, they cannot be distinguished. Therefore, you need to write the full name!

Space can be nested

Microsoft namespace class library

Using System. Windows. Forms;
// Using Ceng1;
// Using Ceng2;
// Using Ceng3;
// Using Ceng3.Ceng33;

Namespace _ 234 {
Public partial class Form1: Form {
Public Form1 (){
InitializeComponent ();
}

Private void Form1_Load (object sender,
EventArgs e ){
Person thePerson = new Person ();
// Ceng1.Person thePerson1 = new
Ceng1.Person ();
// Ceng2.Person thePerson2 = new
Ceng2.Person ();
// Ceng3.Ceng33. Person thePerson3 =
New Ceng3.Ceng33. Person ();
}
}
}
Namespace Ceng1 {
Public class Person {}
}
Namespace Ceng2 {
Public class Person {}
}
Namespace Ceng3 {
Public class Person {}
// Sub-space
Namespace Ceng33 {
Public class Person {}
}
}

Section 27th Interface

The interface seems to be: declares a class, and all the members of the class have "behavior"
Public interface IShape
{
Void Draw ();
}
No member variables. access restriction keywords cannot be used.

Interface

The interface inherits from two basic interfaces, IBase1 and IBase2:
Interface IChild: IBase1, IBase2
{
Void Method1 ();
Void Method2 ();
}

1. An interface defines a protocol. The protocol between the user and the real-time user.
2. When there are more interfaces, the external services are displayed. So no member variables
3. Formally speaking, an interface is an abstract class without member variables, but it cannot be mentioned in semantics.
4. But in reality, the problem of multi-inheritance is solved.
Namespace _ 234 {
Public partial class Form1: Form {
Public Form1 (){
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e ){
IFace PengGe = new Person ();
PengGe. ShowFace ();
}
}
Interface IFace {
Void ShowFace ();
}
// Class that implements the interface
Class Person: IFace {
Public void ShowFace () {MessageBox. Show ("scary, haha !!! ");}
}
}

Section 28th generic

Generic class: class with "Parameters. The parameter here refers to the type.

Note:
1. Here T and S do not know what type it is. Some statements cannot use t> 0, t = null.
2. If T and S are of the object type. Object is the highest parent class of all types, including basic data types.
3. Benefits: High Efficiency and simplicity. Avoid some basic overhead of objects.
4. Many things can be generic, method interface delegate, etc.

Private void Form1_Load (object sender, EventArgs e ){
// Use string and int to instantiate the Test <T, S> class.
Test <string, int> t = new Test <string, int> ("PengGe", 26 );
// Call methods in generic classes
T. SetValue ();
}

}
// Define a generic class, which has two type parameters: T, S
Public class Test <T, S> {
// Type parameters of generic classes can be used for class members.
Private T name;
Private S age;

Public Test (T Name, S Age ){
This. name = Name;
This. age = Age;
}
Public void SetValue (){
MessageBox. Show ("name:" + name. ToString ());
MessageBox. Show ("age:" + age. ToString ());
}

Related Article

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.