Object-oriented encapsulation and polymorphism

Source: Internet
Author: User

Object-oriented encapsulation:

1, the inheritance should conform to the is-a relationship. That is, the subclass is a parent class-The child class is the parent class. Such as: Truck is car, truck is a car.

2. If the protected modifier is used in the parent class, it means that only its subclasses are allowed access, and no other non-subclass access is allowed:

The subclass is inaccessible if the private modifier is used in the parent class.

3, in addition to the construction method can not inherit the quilt class, the others are directly inherited. The constructor of the parent class can be called with the base keyword: The syntax is as follows

: Base (parameter variable name)

You can also invoke the properties and methods of the parent class using base.

Note: These parameters are already defined in the subclass constructor, and only the variable name can be specified in base ().

This variable name must be consistent with the constructor in the parent class

4. Characteristics of Inheritance:

Transitive nature of a succession

The single- root nature of B inheritance indicates that a subclass can inherit only one parent class.

C sealed class The class it modifies cannot be inherited. Sealed with keyword

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

Object-oriented polymorphism:

polymorphism refers to two or more objects belonging to different classes, in a manner that responds differently to the same message (method invocation). the way to achieve polymorphism:

I. Abstract classes and abstract methods

1, the syntax of abstract class:

Access modifier abstract class class name

2. Methods in the parent class:

Access modifier abstract return type method ();

Note: Classes that contain abstract methods are necessarily abstract classes. Methods in an abstract class are not necessarily abstract methods.

Abstract classes cannot be instantiated. The method of the class can be implemented only through its subclasses, unless its subclasses are also an abstract class.

Abstract classes cannot be sealed or static, abstract classes of subclasses inherit and implement its abstract methods.

3, rewrite the abstract method:

Access modifier override return type method ()

4, the use of the occasion:

A parent class provides a function or rule that constrains the behavior of subclasses.

Example:

Abstract public class person
{
Abstract public void Sayhi ();
}

Sub-class
public class Student:person
{
      public override  void sayhi ()
         { 
              //code omitted
        }
      }

    public class  Teacher:person
    {
       public override void sayhi ()
        { 
              //code omitted
         }
      }

The principle of the Richter replacement

the "is" operator is used to check whether the object is the same as the given type. such as: if (obj is string) {}

"as" is used for type conversions between two objects. such as: string as obj;

Note: Theas operator generates NULL instead of throwing an exception when the conversion fails.

"is" checks whether an object is compatible with the specified type and returns a Boolean value : TRUE or Fasle. Note that the IS operator never throws an exception, see the following example:

ClassA

{

....

}

Object O=new object ();

Boolean b1= (O is Object); B1 is true.

Boolean b2= (O is ClassA); B2 is false.

If the object reference is Null,is, the operator always returns false because there is no object whose type can be checked.

Second, virtual method:

Access modifier Virtual return type method ()

{

Method body

}

1. Each object has a Equals () method that determines whether two instance objects are equal and is a virtual method that can override this method.

Public virtual bool Equals (Object obj)

By default, theEquals () method supports reference equality only , that is, whether the two objects that are compared refer to the same object.

Override the Equals () virtual method such as:

public override bool Equals (object obj)
{
Converts the object to be compared to the current type
Student target = obj as Student;

If empty, the type is different
if (target = = null)
return false;

if (Target.name = = THIS.name &&
Target.gender = = This.gender &&
Target.age = = This.age &&
Target.hobby = = This.hobby &&
Target.popularity = = this.popularity)
{
return true;
}
return false;
}

2. The difference between a virtual method and an abstract method:

A, the modified keyword is different

B, the difference between the method body in the parent class

C, the difference between overrides (abstract method must override)

D, abstract methods can only be in the abstract class, and virtual methods can be written in addition to the sealed class

3, the method overrides the condition:

A, the method signature must be the same

B, access modifier allowed to be amplified

C, the return type must be the same

Summary: Three main features of object-oriented:

encapsulation: Ensure the integrity and security of the object's own data

Inheritance: Establish the relationship between classes, implement code reuse, and facilitate the extension of the system

Polymorphic: The same method call can be implemented in different ways

Third, method overloading

1. Differences from rewrite methods

A, the application is different: if the parent class has abstract/virtual, can be overridden, the overriding method requires that the class is an inheritance relationship with the class.

Overloading does not require too much.

method Hiding : Methods of both parent and subclass exist.

V. Static (Satice) class/seal (sealed) class/abstract class their respective characteristics

Note: This article is reproduced in http://www.cnblogs.com/028fly/archive/2009/06/06/1497079.html

Object-oriented encapsulation and polymorphism

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.