Object-oriented in Chapter 03 C #

Source: Internet
Author: User

1. static and dynamic attributes in philosophy correspond to attributes and behaviors respectively.

2. Static methods are accessed by class names. Static methods can only access static members. Non-static methods can access all members.

3. Use of attributes and indexer.

4. Access type modifiers: Public, private, protected, and internal.

5. Static members can be used before the object is created.


Example:

 Using System;

Public Class CD
{
Public String Name;
Public Double Price;
Public String Type;
Public CD ( String Name, Double Price, String Type)
{ This . Name = Name; This . Price = price;This . Type = type ;}

}
Public Class Videoshop
{
Private CD [] cdset = New CD [ 10000 ];
Private Int Cdcount = 0 ;
Public Int Count {Get { Return This . Cdcount ;}}
// 1. The first index type
Public CD This [ String Cdname]
{
Get
{
Foreach (CD item In Cdset)
If (Item. Name = cdname)
Return Item;
Return Null ;
}
}
// 2. The second index type
Public CD This [ String Cdname, String Type]
{
Get
{
Foreach (CD item In Cdset)
If (Item. Name = cdname & item. type = type)
Return Item;
Return Null ;
}
}
// 3. The third index type
Public CDThis [ Int N]
{
Set
{
If (Cdset [N] = Null ) Cdcount ++;
If (Value = Null & Cdset [N]! = Null ) Cdcount --;
Cdset [N] = value;
}
Get
{
Return Cdset [N];
}
}
}
Class Test
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Videoshop myshop = New Videoshop ();
CD CD1 = New CD ( " Jinling 13-game " , 45 , " DVD " );
CD CD2 = New CD ( " Rome holiday " , 25 , " VCD " );
Myshop [myshop. Count] = CD1;// Call index type 3
Myshop [myshop. Count] = Cd2; // Call index type 3
Cd a = myshop [ " Jinling 13-game " ]; // Call index type 1
Cd B = myshop [ " Rome holiday " , " VCD " ]; // Call index type 2
Console. writeline ( " Name: {0} {1}, price: {2} " , A. type, A. Name, A. Price );
Console. writeline ( " Name: {0} {1}, price: {2} " , B. type, B. Name, B. Price );
Console. Readline ();
}
}

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.