C # basic review-Part 1

Source: Internet
Author: User

Encapsulation: HideCodeConvenient reuse and Modification
Inheritance:
Polymorphism:

Interview is a good analysis of object-oriented
It is best to take the surrounding transactions for example immediately
For example, table
First analysis class: Tables and chairs
They are all furniture.
So define how a furniture category is, and this is an attribute.
What can it do? This is the way to sit and put things on it.

Then define a chair class. It should be furniture, so it inherits from the furniture class.
It also has an attribute and another attribute, such as height.

We can encapsulate the chair into a class and then use this class.
Create many objects
For example, Chair 1, Chair 2, and then we define another person class.
As long as it calls the Chair method, we don't need to use it.
Know how the inside of the chair is implemented

Because all the chairs have one
--------------------------------------------

Enum week {Sunday, Monday, Tuesday ......}
--------------------------------------------

 
InternalProgramYou can access the private class by accessing the public introduced namespace in the set to access the inside of the protected class and the sub-class to access the private class.

--------------------------------------------
This and constructor

Public Person (string name): This (50, name) // call another constructor here and the age is 50 by default {} public person (INT age, string name) {This. name = Name; this. age = age ;}

This is used in the method to represent the object in the heap of its class.
--------------------------------------------
Virtual Folder system in the namespace

Right-click the properties of the project file. The Assembly name is the EXE name in the bin.
The default namespace is the same as the project. We modify the project name, but the default namespace is not changed. We need to modify it manually.

The output types include Assembly and console applications.
The namespace and Assembly name are irrelevant.

Import the namespace using system. Data;
Write namespace
Namespace MySpace
{
Class person
{

}
}

In another namespace, if you want to access the person object, introduce using MySpace.
In this way, we can use the new person object person Mai = new person ();
If there is a class with the same name in another namespace, we need the namespace. Class Name when accessing it.

You can create multiple classes in the same namespace, such

 
Namespace MySpace {class person {} class Aminal {}}

Each CS namespace under the same project is the same by default.

--------------------------------------------
Create a subclass object to call the constructor of the subclass before creating a new subclass (at this time, the constructor method body of the subclass has not been executed)
Create a parent class object, call the parent class constructor, and finally execute the constructor method code of the subclass.

Display the base () method for calling the parent class Constructor ()
Show other constructors that call the current class

 
Public Person (string name): This (50, name) // call another constructor here and the age is 50 by default {} public person (INT age, string name) {This. name = Name; this. age = age ;}

--------------------------------------------
Rys replacement principle-Child class can replace the parent class bit
Parent subclass
Person P = new student ()
P. Say (); // The say method of the parent class is called.
However, if the parent class does not have the say method, an error will be reported because the type pointer of the parent class will only find the members of the parent class.

If we want to call the subclass, we need to convert P to student.

Student s = P as student;

When a subclass object declares a parent class object, you can only call the methods of the parent class.
However, when a subclass object is declared as a subclass object, first check whether this method exists in the subclass object. If not, call the parent class. If the parent class does not exist, an error occurs.
--------------------------------------------
LSP's replacement principle

The parent class object cannot be forcibly converted to a subclass object.
Subclass 0 = (subclass) new parent class () is incorrect.
Unless your current object is a subclass
For example

Pserson P = new student (); student s = (student) C; S. say (); pserson P = new teacher (); subclasses can be implicitly converted to teacher t = (teacher) P of the parent class; the parent class can be strongly converted to the subclass is and astypea is typeB. Only typea as typeB is judged first.

--------------------------------------------
Simple factory Mode

 
Static person getperson (string typestr) {Switch (typestr) {Case "teacher": return new teacher (); Case "student": return new student (); default: return new person () ;}} static void testsimplefactory () {pserson P = getperson ("teacher"); p. sayhi ();}

-----------------------------------------------
Polymorphism (method rewriting)
Three conditions
1. There is an inheritance relationship
Class student: person

 

2. Subclass method override parent class Method
Write a virtual method in person to overwrite the quilt class.

Public Virtual void sayhi () {javasloe...} in student, override this method public override void sayhi () {dd ...}

3. parent class references to subclass objects
Person P = new student ();
P. Say (); // at this time, it will actually call the subclass method.
--------------------------------------------
As is

 
Person P = new student (); If (P is student) // determines whether P is a subclass of student or student {student s = (student) P ;} if you use as student s = P as student;

--------------------------------------------
Abstract class
Abstract classes can include non-abstract methods.

 
Public abstract class abstractclass {pulic void sayhi () {} public abstract void absmethod (); // abstract method}

ABSTRACT To be written outside the class keyword
--------------------------------------------
Abstract class class1
{
Public void say ()
{
Console. writeline ("abstract classes can have no abstract methods ");
}
}
// But the abstract class cannot be new
--------------------------------------------
Abstract classes cannot be instantiated (new)
Abstract classes can have abstract methods or not
The general class cannot have abstract methods.
Who has inherited the abstract class, either written as the abstract class or implemented all the abstract methods of the abstract class
--------------------------------------------
The interface is also a reference type similar to the class and abstract class similarity of three points
1. It cannot be instantiated.
2. Include unimplemented method declarations
3. Subclass must implement unimplemented Methods
--------------------------------------------
Interface Common Errors

Private interface interface1
{
}
Error 1 the elements defined in the namespace cannot be explicitly declared as private, protected, or protected internal.
--------------------------------------------
Interface interface1
{
Void say ();
}
Error 2 methods in the interface cannot be public-modified private.

--------------------------------------------
Interface interface1
{
Void say ()
{
Console. writeline ("the interface can be implemented using a method ??? ");
}
}
Error 3 "leleapplication1.interface1. Say ()": the interface member cannot be defined.

--------------------------------------------
If the parent method does not mark the virtual
The subclass override method may be incorrect.

 
Namespace consoleapplication1 {class program {static void main (string [] ARGs) {person P = new student (); p. sayhi (); console. readkey () ;}} class person {public void sayhi () {console. writeline ("person. sayhi ") ;}} class student: person {public override void sayhi () {console. writeline ("student. sayhi ");}}}

Error 1 "leleapplication1.student. sayhi ()": the inherited member "leleapplication1.person. sayhi ()" is not marked as virtual, abstract, or
Override, cannot be rewritten

--------------------------------------------
Differences between virtual and abstract methods
1. Virtual methods must be implemented. abstract methods must not be implemented.
2. abstract methods must be declared in abstract classes. Virtual methods can appear in abstract classes.
3. the abstract method must be rewritten in the subclass. The virtual method can be rewritten.

The main means to achieve Polymorphism
1. Virtual Method
2 Abstract METHODS Abstract
3 Interface

Notes for Virtual Methods
1. If the parent class has a method that requires override of the subclass, you can mark this method as virtual.
2. The virtual method must be implemented in the parent class. That is empty.
3. You can override or not override the subclass of the virtual method.
If there is no multi-state call without rewriting, the pointer type of the parent class is called, and the pointer type of the subclass of the parent class is called.

--------------------------------------------
New Keyword and overr visual

Namespace consoleapplication1 {class program {static void main (string [] ARGs) {person P = new student (); p. sayhi (); // as the subclass is rewritten, the member p1 = new teacher (); p1.sayhi (); // call the method teacher T = new teacher (); T. sayhi (); // The subclass is called directly here, but if the subclass method is not new, a warning console may appear. readkey () ;}} class person {Public Virtual void sayhi () {console. writeline ("person. sayhi ") ;}} class student: person {public override void sayhi () {console. writeline ("student. sayhi ") ;}} class teacher: person {public new void sayhi () {console. writeline ("teacher. sayhi ");} // if there is no new in the above section, it will warn that the new function is to hide the methods of the parent class because the parent class also has the same method and is a virtual method. this method of subclass is a new method of subclass, not a method of rewriting parent class }}

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.