Object-oriented (encapsulation, inheritance)

Source: Internet
Author: User

1. The access modifiers for the class are public, private, internal, protected.

Where PUBULC: public, as long as the reference to the namespace, you can freely access
Private: Only within the current class can be accessed

Internal: Internal, accessible within the current assembly, the Assembly is the namespace, this modifier is the default
Protected: Protected, the current class and its subclasses are accessible

2. Namespaces:
Also called an assembly, each folder in the project is a separate namespace
If you want to use a class file under one of the namespaces, you must first reference this namespace

3. Object-oriented features 1

Package: Pack, seal, protect.

private int _ids; -Private Member variables

public int Ids-Properties, encapsulating the basic structure
{
get{return _ids;}
set{_ids = value;}
}

A member variable can have a number of properties

The property return type can be any type, not must be the same as the member variable type

Properties can be read-only or write-only, or they can have both

You can add a restriction condition in a property, for example:

Enter a fraction (>=0&&=<100), not in range output 0

//In the newly created class Private decimal_score; {                Get{return_score;} Set{                    if(Value >=0&& value <= -) {_score=value; }                    Else{_score=0; }            }            }//Console.Write ("Please enter a score"); S.score=decimal.            Parse (Console.ReadLine ());            Console.WriteLine (S.score); Console.ReadLine ();

4. Object-oriented features 2

Inheritance is the mechanism by which subclasses automatically share data structures and methods of the parent class, which is a relationship between classes. When defining and implementing a class, it can be done on top of an already existing class, taking the content defined by the existing class as its own content, and adding a number of new content.

Subclasses can inherit all public methods and properties of the parent class

The parent class cannot invoke the methods and properties of a subclass, even if the


The subclass can be converted to a parent class, and the converted parent can also be converted back to the appropriate subclass example:

Phone PP = (phone) P;//phone is the parent class, Xiaomi, Huawei is a subclass
Xiaomi PS = (Xiaomi) pp;

The subclass is converted to a parent class, and the parent class cannot be converted to another subclass, such as Huawei PA = (Huawei) pp;

Parent class

Subclass-Subclass of a class, derived class, superclass

Xiaomi This class is a derived class of phone,
Xiaomi this class derives from the phone

Inheritance code:

class Phone    {        publicstring  game ()        {            return "  I was born for the game! " ;        }    }

Class Xiaomi:phone
{
public string Name ()
{
Return "I was born for a fever!" ";
}
}

class program    {        staticvoid Main (string[] args)        {            new  Xiaomi ();             string PS = p.game ();
Console.WriteLine (PS); Console.ReadLine ();
Phone PP = (phone) p;//subclass converted to parent class
Xiaomi PA = (Xiaomi) pp;
Console.WriteLine (Pa. Name ());
} }

Object-oriented (encapsulation, inheritance)

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.