20151024_004_c# Basics (Access modifiers in C #, inheritance, new keyword, Richter conversion, is and as, polymorphic, serialization and deserialization)

Source: Internet
Author: User

Access modifiers in the 1:c#

Public: Common members, completely public, without access restrictions.

Private: Privately accessible only within the current class.

Protected: protected and accessible only within the current class and in subclasses of that class.

Internal: can only be accessed in the current project, in the same project, internal and public permissions are the same.

Protected internal:protected + internal permissions.

1.1: There are only two access modifiers that can modify a class : Public/ Internal

1.2: Member in interface does not allow add access modifier, default is public

1.3: Members in a class do not add an access modifier, default is private

1.4: The member of the namespace does not add an access modifier, the default is internal

2: Inheritance (separate the repeating members of these classes into a class, as the parent class of these classes)

We may have some classes that write some duplicate members, and we can encapsulate these repeating members individually into a class as the parent class of those classes.

Student Teacher Driver sub-class/derived class

Person Parent class/base class

2.1: Subclass inherits the parent class, what does the subclass inherit from the parent class?

Subclasses inherit the properties and methods of the parent class, but the subclass does not inherit the private fields of the parent class.

2.2:q: Does the subclass inherit the constructor of the parent class?

A: The subclass does not inherit the constructor of the parent class, but the subclass defaults to calling the parent class parameterless constructor, creating the parent class object so that the child class can use the members of the parent class.

Solution:

1): Override a parameterless constructor in the parent class.

2): The constructor for calling the parent class is displayed in the subclass, using the keyword: base ()

2.3: Inherited features

1. Inheritance of the single root: a subclass can have only one parent class

2. Transitive nature of inheritance:

2.4: View class Diagram

2.5:object is the base class for all classes

2.6:new keywords

1): Create object

2): Hides a member of the same name inherited from the parent class (a method with the same names as the child and the parent class)

The hidden result is that the subclass cannot call a member of the same name as the parent class.

2.7: Richter Conversion

1: Subclasses can be assigned to a parent class (if there is a method that requires a parent class as an argument, we can pass a subclass object).

2: If a child class object is loaded in the parent class, you can cast the parent class to a subclass object.

2.7.1: A subclass object can call a member in the parent class, but the parent object will always be able to invoke only its own members.

2.8:is and AS

is: Represents a type conversion and returns a true if the conversion succeeds, otherwise a false is returned.

As: Represents a type conversion and returns a null if it can be converted to return the object itself.

PS:Q: Error in code: reason for accessibility inconsistency

A: The subclass must not have access rights higher than the parent class, exposing the members of the parent class

3: polymorphic

3.1: Ways to implement polymorphism:

1): Virtual method

Mark the method of the parent class as a virtual method, using the keyword virtual, which can be re-written again by the Quilt Class (override)

2): Abstract class

When a method in the parent class does not know how to implement it, consider writing the parent class as an abstract class and writing the method as an abstract method

3): interface

3.2: Abstract class

1. Abstract members must be marked abstract and cannot have any implementations.

2. Abstract members must be in an abstract class.

3. Abstract classes cannot be instantiated.

4. After the subclass inherits the abstract class, it must override all the abstract members in the parent class.

(You can not rewrite the subclass unless it is also an abstract class)

5. The access modifier for an abstract member cannot be private

6. You can include instance members in an abstract class.

and instance members of an abstract class can be implemented without a quilt class

7. Abstract classes are constructors. Although it cannot be instantiated.

8. If there are parameters in the abstract method of the parent class, then. Subclasses inheriting this abstract parent class must pass in the corresponding parameter when overriding the method of the parent class.

If there is a return value in the abstract method of the abstract parent class, the subclass must also have an incoming return value when overriding the abstract method.

3.3: Virtual method/abstract class/interface when used to implement polymorphism

1: If a method in the parent class has a default implementation, and the parent class needs to be instantiated , consider defining the parent class as a normal class, using a virtual method to implement polymorphism.

2: If a method of the parent class does not have a default implementation and the parent class does not need to be instantiated , the class can be defined as an abstract class.

3: Several objects do not have a common parent class, but have common behavior . You can use the interface to implement

Signature of method: return value and parameter of method

3.4: Comparison of virtual and abstract methods

3.5: Static methods and instance methods (definition and invocation)

3.6: Design mode

One way to design this project (simple factory design mode)

3.7: The value type is passed at the time of copy, which is worth itself

When a reference type is copied, a reference to the object is passed

3.8: Serialization and deserialization

serialization : Converting an object to a binary

deserialization : The conversion of a binary into an object

Role: Transferring Data

[Serializable] Only classes marked by Serializable can be serialized

BinaryFormatter//Serialized class

1     class Person2     {3         Private string_name;4         Private int_age;5 6          Public stringName7         {8             Get9             {Ten                 return_name; One             } A  -             Set -             { the_name =value; -             } -         } -  +          Public int Age -         { +             Get A             { at                 return_age; -             } -  -             Set -             { -_age =value; in             } -         } to     } +  -     class Program the     { *         Static voidMain (string[] args) $         {Panax NotoginsengPerson p =NewPerson (); -P.name ="Zhangsan"; theP.age = -; +  A             using(FileStream fswrite =NewFileStream (@"C:\Users\admin\Desktop\Serializable.txt", FileMode.OpenOrCreate, FileAccess.Write)) the             { +BinaryFormatter BF =NewBinaryFormatter (); - BF. Serialize (Fswrite, p); $             } $Console.WriteLine ("Serialize Success"); - Console.readkey (); -  the             //using (FileStream fsread = new FileStream (@ "C:\Users\admin\Desktop\Serializable.txt", FileMode.OpenOrCreate, FileAccess.Read)) -             //{Wuyi             //BinaryFormatter bf = new BinaryFormatter (); the             //Person pObj = (person) bf. Deserialize (fsread); -             //} Wu         } -}
Serialization and serialization

3.9: Partial class partial

3.10: Sealed class sealed cannot be inherited, can inherit other ordinary class

ToString (); is a virtual method of object, subclasses can override the virtual method of the parent class

3.11: interface

[Public] Interface I.. Able

{

Members

}

Not allowed to write functions with method body, no fields allowed, automatic attributes allowed in interface

------------------------------------------------------------------

An interface is a specification

1: As long as a class inherits an interface, the class must implement all the members of this interface

2: Members in an interface cannot have the "access modifier", the member access modifier in the interface is public, and cannot be modified.

3: Interfaces can only have methods, properties, indexers, events, and cannot have "fields" and constructors.

4: The interface can inherit from the interface and can inherit more.

5: An interface cannot inherit a class, and a class can inherit an interface (an interface can inherit only from an interface, and a class can inherit an interface or inherit a class)

6: The subclass that implements the interface must implement all members of the interface.

7: A class can inherit a class at the same time and implement multiple interfaces, if a subclass inherits the parent class A and implements the interface IA, then syntactically a must be written in front of the IA.

8:class Myclass:a,ia {} Because the class is single-inheritance.

9: Display the purpose of implementing the interface: The problem of the duplicate name of the workaround

When to display the implementation interface: When the methods and parameters in the inherited interface are identical, use the displayed interface.

10: When an abstract class implements an interface, subclasses are required to implement the interface.

Ps: Interview question: What are the similarities and differences between interfaces and classes in C #.

Different points:

Interfaces cannot be instantiated directly.

An interface does not contain an implementation of a method.

Interfaces can inherit multiple, and classes can only be single-inherited.

A class definition can be split between different source files.

Same point:

interfaces, classes, and structs can inherit from multiple interfaces.

An interface is similar to an abstract base class: Any non-abstract type that inherits an interface must implement all members of the interface.

Both interfaces and classes can contain events, indexers, methods, and properties.

The basics: Interfaces can only define methods (you can define behaviors only, you cannot define implementations, which are fields), because events, indexers, and properties are essentially methods, so events, indexers, and properties are also defined in an interface.

Ps: Interview question: What is the difference between abstract class and interface?

Same point:

cannot be instantiated directly, it is possible to implement its abstract method through inheritance.

Different points:

Interfaces support multiple inheritance, and abstract classes cannot implement multiple inheritance.

An interface can define behavior only, and an abstract class may define behavior as well as provide implementations.

The interface contains only methods (method), property, indexer (index), event signature, but cannot define fields and methods that contain implementations;

Abstract classes can define fields, properties, and methods that contain implementations.

Interfaces can be used for value types (structs) and reference types (class), and abstract classes can only be used for reference types. For example, a struct can inherit an interface and not inherit a class.

Supplementary answer: settingsprovider example when speaking design pattern.

20151024_004_c# Basics (Access modifiers in C #, inheritance, new keyword, Richter conversion, is and as, polymorphic, serialization and deserialization)

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.