15-01-12 C # Object-oriented 11

Source: Internet
Author: User

inheritance, resolving code redundancy in a class;

The classes we write inherit directly or indirectly from the object class;

The Richter conversion two 1. Subclasses can be assigned to the parent class,//If there is a place that requires the parent class as an argument, we can give a subclass instead;

Student s = new Student ();

Person p = s;

person P1 = new Student ();

2. If the parent class is a subclass object, then the parent class can be strongly converted to a subclass object;

Student ss = (Student) p;

A subclass object can call a member of the parent class, but the parent object will never be able to invoke its own member;

is: Represents the type conversion, returns a true if the conversion succeeds, or returns a false;

As: Represents a type conversion, and returns a null if it is able to convert, otherwise the corresponding object is returned;

If (P is Student)

{

Student ss = (Student) p;

}

Else

{

Console.WriteLine ("Conversion failed");

}

Student SS = P as Student;

Student Teacher meilv Shuaiguo Yeshou 5 classes are inherited from the person class

person[] pers = new PERSON[10]; The above 5 classes of objects can be put into this array, the Richter conversion of the first article;

Random r = new Randon ();

for (int i = 0; i<pers. length;i++)

{

int rnumber = R.next (1,7);

Switch (RNUMBR)

{

Case 1:pers[i] = new Student (); Break

Case 2:pers[i] = new Teacher (); Break

Case 3:pers[i] = new Shuaiguo (); Break

Case 4:pers[i] = new Meilv (); Break

Case 5:pers[i] = new Yeshou (); Break

Case 6:pers[i] = new Person (); Break

}

}

Person[] Type array, no matter what type of object you are in, the person type is displayed;

for (int i = 0; i<pers. length;i++)

{

Pers[i]. Personsayhi ();

if (Pers[i] is Student)

{

((Student) pers[i]).   Studentsayhi (); The Richter conversion of the second article;

}

else if (Pers[i] is Teacher)

{

((Teacher) pers[i]). Teachersayhi ();

}

else if (Pers[i] is Shuaiguo)

{

((Shuaiguo) pers[i]). Shuaiguosayhi ();

}

else if (Pers[i] is MEILV)

{

((MEILV) pers[i]). Meilvsayhi ();

}

else if (Pers[i] is Yeshou)

{

((Yeshou) pers[i]). Yeshousayhi ();

}

Else

{

Pers[i]. Personsayhi ();

}

}

Public: Publicly available, accessible anywhere;

Private: Only accessible within the current class;

Protect: Permissions are higher than private, other class-independent classes cannot access, but its subclasses can access the members of the Protect; Protected, which can be accessed within the current class and within the subclass of the class;

A collection is a very important thing for programming;

From the face object, we write the code every day, is in the actual project, the development of the content to be used;

ArrayList is not a static class;

ArrayList al = new ArrayList ();

Collection: A collection of many data;       Array: The length is immutable and the type is single (except for an array of Richter conversions); Length can be arbitrarily changed, type casually;

List.  ADD (); Add a single element to the collection;

The length of the List.count collection;

The set is the same as the array, accessing the element by subscript;

We output an object to the console, and by default, the namespace of the class in which the object resides is printed;

In list[] No matter what content you add, it is the type of object;

for (int i = 0;i<list. count;i++)

{

if (List[i] is person)//There is an inheritance relationship when the time can be transferred;

{

(person) list[i]). SayHello ();

}

else if (List[i] is int[])

{

for (int j = 0; j< ((int[]) list[i]). Length; J + +)

{

Console.WriteLine (((int[) list[i]) [j]);

}

}

Else

{

Console.WriteLine (List[i]);

}

}

To ArrayList inside the data is very good, do not consider the type, regardless of length, but take the data out of the time is very troublesome, such as the above object and array, because it is in the type is object, to normal use must be type conversion;

You can use list. AddRange (New int[]{1,2,3,4,5,6,7}) method to add a collection;

If you add an array or an object or a collection using Add, you will use the value of the lump type conversion above when it is taken out, but if you add it by using AddRange, the output set will not need to be converted, but it will directly output the elements in the set.

List. AddRange (list);

List.clear () Delete the elements in the collection

List. Remove (TRUE)//delete a single element within the collection;

List. RemoveAt (0)//delete the element according to the index;

List. RemoveRange (0,3); Delete the element at the beginning of the No. 0 index in the collection, and delete the 3 items in the future;

List. Reverse (); reverse array;

List. Sort (); Keep the set in ascending order; only the numbers in it;

List. Insert (1, "Hello"); Inserts the content to be inserted at the specified index, no type requirement for insertion;

List. Insertrange (0,new string[]{"Zhang San", "John Doe"}); Inserts a collection at the specified index;

List. Contains (1); Determine if the list contains 1 of this element;

ArrayList can add any element to it, because it requires an element type of type object;

ArrayList can be of unlimited length, with its two count and capcity two elements; Count indicates the number of elements actually contained in this collection; Capcity represents the number of elements that can be contained in this collection;

When the number of elements actually contained in the collection (count) exceeds the number of elements that can be contained (capcity), the collection will request more space in memory to ensure that the set length is sufficient;

ArrayList collection is not very good now, learning this set is to learn the generic set to pave the way behind;

Generic sets the difference is that a generic collection can only add elements of a fixed type, which is the same as the root array, but the generic collection length is not fixed, and this array does not

The data types stored in the two collections of ArrayList and Hashtable are object, meaning that any type of data can be stored in ArrayList and Hashtable;

Hashtable we call the set of key-value pairs; In the set of key-value pairs, it is the key to find the value;

Hashtable ht = new Hashtable ();

Ht. ADD (key, value);

Both the key and the value are object types;

In general, we have to look at the array or the elements in the ArrayList set.

Ht[key] = value; It is not possible to traverse the hashtable with a for loop because the key is not necessarily a number, and a foreach is used to iterate over the key-value pairs

The foreach frame can be out by pressing the TAB key twice.

foreach (var item in collection)

{

var: infer the type of an item in the collection

Item: A collection of loops for each item

In what's inside

Collection represents the collection to loop;

}

C # is a strongly typed language, JS is a weakly typed language,

Strongly typed: In code, you must have a definite definition of the type of each variable,

int n = 15; N.gettype (); Acquisition type;

Weak type: There is no need to have a definite definition of the type of the variable, it can infer the type of the variable according to the value of the variable;

var n = 15; N.gettype (); Get the type; var can infer the type according to the value;

string input = Console.ReadLine (); That's right

var input = Console.ReadLine (); Error reason, when declaring a var variable, it must be assigned an initial value;

foreach (var item in HT. Keys)//Iterate through the keys in the collection, at which point the item represents the key in the collection, or it can traverse the value HT. Values, at which item represents the value in the collection

{

Console.WriteLine (Item,ht[item]);

}

Int[] Nums = {1,2,3,4,5,6,7};

foreach (var item in nums)

{

Console.WriteLine (item);

}

for (int i = 0,i < nums.length; i++)

{

Console.WriteLine (Nums[i]);

}

When the number of cycles is very large, the loop efficiency of the foreach is much greater than the For loop; In the case of a small number of operations, they are the same efficiency;

Keys must be unique in a set of key-value pairs, and values can be duplicated.

Ht. ADD (1, "Zhang San");

Ht. ADD (2, "John Doe");

Ht. ADD (1, "Harry"); There was an error because there was already an element with the key 1 in front of it,

HT[6] = "Zhang San";

HT[1] = "Zhao Liu";//Not error, but the original key is 1 of the element is covered out

When you add a key-value pair to the collection element, you must first determine if there is a key, if you do not add, if you do not add,

if (!ht. ContainsKey ("abc"))

{

Ht. ADD ("abc", "CBA");

}

Ht.clear ();

Ht. Remove (key);

The path class is a static class;

String str = @ "C:\Users\hhs\Desktop\BLOG\1.txt";

Remove the file name; The last \ Index is obtained according to the method of string, and then the file name is obtained according to the method of substring LastIndexOf string;

The path class is much simpler;

Path.getfilename (str);//quickly get the name of the file under the path;

Path.getfilenamewithoutextension (str);//quickly get the name of the file under the path, not including the extension;

Path.getextension (str);//Get the file extension directly;

Path.getdirectoryname (str);//Get the name of the folder where the file is located;

Path.GetFullPath (str); Get the full path where the file is located;

Path.Combine (@ "C:\a\", "b.txt"); combine two paths into one path;

Path This class is specifically used to manipulate the path;

File is definitely used to create files, and it is a static class.

File.create (@ "C:\User\a.txt");//Creates a new file in the specified path, the file class can really manipulate files, and can create. jpg. wav files;

File.delete (@ "C:\User\a.txt");//delete files according to the specified path;

File.Copy (@ "C:\User\a.txt", @ "C:\User\b.txt"); B.txt is generated after replication,

File.move ();

ReadAllBytes ();

WriteAllBytes ();

The byte array is converted to a string; Encoding.Default.GetString (byte array);

String into a byte array; Encoding.Default.GetBytes (string);

How to save the string in the form of binary, this is the encoding;

The reason for garbled: Is that you save this file with the code used to open this file with the encoding format is inconsistent;

ASC ASCII GB2312 Big5 Unicode UTF-8

Polymorphism is the most concentrated piece of object-oriented.

15-01-12 C # Object-oriented 11

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.