C#語法學習六(修飾符)

修飾符

 

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 12:56
 * 修飾符                    類內部           子類          程序集內           程序集外
 * default                     OK
 * public                      OK
 * private                     OK
 * internal                    OK
 * protected                   OK
 * protected internal          OK
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using  System;
class
 Mod
{
    
void
 defaultMethod()
    {
        Console.WriteLine(
"this is a default method"
);
    }
    
public void
 publicMethod()
    {
        Console.WriteLine(
"this is a public method"
);
    }
    
private void
 privateMethod()
    {
        Console.WriteLine(
"this is a private method"
);
    }
    
internal void
 internalMethod()
    {
        Console.WriteLine(
"this is a internal method"
);
    }
    
protected void
 protectedMethod()
    {
        Console.WriteLine(
"this is a protected method"
);
    }
    
protected internal void
 protectedinternalMethod()
    {
        Console.WriteLine(
"this is a protectedinternal method"
);
    }
    
static void
 Main()
    {
        Mod mod
=new
 Mod();
        mod.defaultMethod();
        mod.publicMethod();
        mod.privateMethod();
        mod.internalMethod();
        mod.protectedMethod();
        mod.protectedinternalMethod();
    }
}
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 12:56
 * 修飾符                    類內部           子類          程序集內           程序集外
 * default                     OK                      
 * public                      OK                               OK
 * private                     OK
 * internal                    OK                               OK
 * protected                   OK
 * protected internal          OK                               OK
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using  System;
class  Mod
{
    
void  defaultMethod()
    {
        Console.WriteLine(
" this is a default method " );
    }
    
public   void  publicMethod()
    {
        Console.WriteLine(
" this is a public method " );
    }
    
private   void  privateMethod()
    {
        Console.WriteLine(
" this is a private method " );
    }
    
internal   void  internalMethod()
    {
        Console.WriteLine(
" this is a internal method " );
    }
    
protected   void  protectedMethod()
    {
        Console.WriteLine(
" this is a protected method " );
    }
    
protected   internal   void  protectedinternalMethod()
    {
        Console.WriteLine(
" this is a protectedinternal method " );
    }

}

class  Test
{
    
static   void  Main()
    {
        Mod mod
= new  Mod();
        mod.internalMethod();
        mod.protectedinternalMethod();
        mod.publicMethod();
    }
}
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 12:56
 * 修飾符                    類內部           子類          程序集內           程序集外
 * default                     OK                      
 * public                      OK              OK               OK
 * private                     OK
 * internal                    OK              OK               OK
 * protected                   OK              OK
 * protected internal          OK              OK               OK
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using  System;
class  Mod
{
    
void  defaultMethod()
    {
        Console.WriteLine(
" this is a default method " );
    }
    
public   void  publicMethod()
    {
        Console.WriteLine(
" this is a public method " );
    }
    
private   void  privateMethod()
    {
        Console.WriteLine(
" this is a private method " );
    }
    
internal   void  internalMethod()
    {
        Console.WriteLine(
" this is a internal method " );
    }
    
protected   void  protectedMethod()
    {
        Console.WriteLine(
" this is a protected method " );
    }
    
protected   internal   void  protectedinternalMethod()
    {
        Console.WriteLine(
" this is a protectedinternal method " );
    }

}
class  ModChild:Mod
{
    
public   void  RunMod()
    {
        publicMethod();
        internalMethod();
        protectedMethod();
        protectedinternalMethod();
    }
}
class  Test
{
    
static   void  Main()
    {
        ModChild modc
= new  ModChild();
        modc.RunMod();

    }
}
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 12:56
 * 修飾符                    類內部           子類          程序集內           程序集外
 * default                     OK                      
 * public                      OK              OK               OK               OK
 * private                     OK
 * internal                    OK              OK               OK
 * protected                   OK              OK
 * protected internal          OK              OK               OK
 * 
 * protected internal和internal的區別:
 * 1,當基類和子類在同一個程序集時internal為可見
 *當當基類和子類不在同一個程序集時子類不可以訪問internal的成員
 *而可以訪問protected internal成員
 * 2,當類的前面沒有加修飾符時,認的是internal,程序集內可見.
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using  System;
namespace  Demo
{
    
public   class  Mod
    {
        
void  defaultMethod()
        {
            Console.WriteLine(
" this is a default method " );
        }
        
public   void  publicMethod()
        {
            Console.WriteLine(
" this is a public method " );
        }
        
private   void  privateMethod()
        {
            Console.WriteLine(
" this is a private method " );
        }
        
internal   void  internalMethod()
        {
            Console.WriteLine(
" this is a internal method " );
        }
        
protected   void  protectedMethod()
        {
            Console.WriteLine(
" this is a protected method " );
        }
        
protected   internal   void  protectedinternalMethod()
        {
            Console.WriteLine(
" this is a protectedinternal method " );
        }

    }
}
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 12:56
 * 修飾符                    類內部           子類          程序集內           程序集外
 * default                     OK                      
 * public                      OK              OK               OK                OK
 * private                     OK
 * internal                    OK              OK               OK
 * protected                   OK              OK
 * protected internal          OK              OK               OK
 * protected internal和internal的區別:
 * 1,當基類和子類在同一個程序集時internal為可見
 *當當基類和子類不在同一個程序集時子類不可以訪問internal的成員
 *而可以訪問protected internal成員
 * 2,當類的前面沒有加修飾符時,認的是internal,程序集內可見.
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using  System;
namespace  Demo
{
    
class  Test
    {
        
static   void  Main()
        {
            Mod mod
= new  Mod();
            mod.defaultMethod();
            mod.publicMethod();
            mod.privateMethod();
            mod.internalMethod();
            mod.protectedMethod();
            mod.protectedinternalMethod();
        }
    }
}
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 01:37
 * 兩類專用的修飾符
 * sealed  不能被繼承的類
 * partial  可以聲明在不同文件中的同一個類
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using  System
class  A
{
    
}
class  B:A
{
    
}
class  Test
{
    
static   void  Main()
    {
        
    }
}

你可能感兴趣的:(C#)