• Net基础班第十三天


    1、c#中的访问修饰符
    public :公开的公共的
    private:私有的,只能在当前类的内部访问
    protected:受保护的,只能在当前类的内部以及该类的子类中访问。
    internal:只能在当前项目中访问。在同一个项目中,internal和public的权限是一样。
    protected internal:protected+internal

    1)、能够修饰类的访问修饰符只有两个:public、internal。
    2)、可访问性不一致。
    子类的访问权限不能高于父类的访问权限,会暴漏父类的成员。


    2、设计模式
    设计这个项目的一种方式。

    3、简单工厂设计模式

    4、值类型在复制的时候,传递的是这个值得本身。
    引用类型在复制的时候,传递的是对这个对象的引用。

    5、序列化:就是将对象转换为二进制
    反序列化:就是将二进制转换为对象
    作用:传输数据。
    序列化:
    1)、将这个类标记为可以被序列化的。


    6、partial部分类

    7、sealed密封类
    不能够被其他类继承,但是可以继承于其他类。

    8、接口
    [public] interface I..able
    {
    成员;
    }


    练习:

    /*
    * 麻省会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 升飞机会飞
    * 用多态来实现
    * 虚方法、抽象类、接口
    */

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     /// <summary>
    10     /// 动物类,提供飞的虚方法
    11     /// </summary>
    12     class Animal
    13     {
    14         public virtual void Fly()
    15         {
    16             Console.WriteLine("动物有些会飞,有些不会飞!");
    17         }
    18     }
    19     /// <summary>
    20     /// 麻雀
    21     /// </summary>
    22     class Sparrow : Animal
    23     {
    24         public override void Fly()
    25         {
    26             Console.WriteLine("麻雀会飞!");
    27         }
    28     }
    29     /// <summary>
    30     /// 鹦鹉
    31     /// </summary>
    32     class Parrot : Animal
    33     {
    34         public override void  Fly()
    35         {
    36             Console.WriteLine("鹦鹉会飞!");
    37         }
    38     }
    39     /// <summary>
    40     /// 企鹅
    41     /// </summary>
    42     class Penguin:Animal
    43     {
    44         public override void Fly()
    45         {
    46             Console.WriteLine("企鹅不会飞!");
    47         }
    48     }
    49     /// <summary>
    50     /// 直升机
    51     /// </summary>
    52     class Helicopter : Animal 
    53     {
    54         public override void Fly()
    55         {
    56             Console.WriteLine("直升机会飞!");
    57         }
    58     }
    59 }
    虚方法Class
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         /*
    12          * 麻省会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 升飞机会飞
    13          * 用多态来实现
    14          * 虚方法
    15          */
    16         static void Main(string[] args)
    17         {
    18             //麻雀
    19             Animal Sparrow = new Sparrow();
    20             Sparrow.Fly();
    21             //鹦鹉
    22             Animal Parrot = new Parrot();
    23             Parrot.Fly();
    24             //企鹅
    25             Animal Penguin = new Penguin();
    26             Penguin.Fly();
    27             //直升机
    28             Animal Helicopter = new Helicopter();
    29             Helicopter.Fly();
    30 
    31             Console.ReadKey();
    32         }
    33     }
    34     
    35 }
    虚方法Main
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     /// <summary>
    10     /// 动物类,提供飞的抽象方法
    11     /// </summary>
    12     abstract class Animal
    13     {
    14         public abstract void Fly();
    15     }
    16     /// <summary>
    17     /// 麻雀
    18     /// </summary>
    19     class Sparrow : Animal
    20     {
    21         public override void Fly()
    22         {
    23             Console.WriteLine("麻雀会飞!");
    24         }
    25     }
    26     /// <summary>
    27     /// 鹦鹉
    28     /// </summary>
    29     class Parrot : Animal
    30     {
    31         public override void  Fly()
    32         {
    33             Console.WriteLine("鹦鹉会飞!");
    34         }
    35     }
    36     /// <summary>
    37     /// 鸵鸟
    38     /// </summary>
    39     class Ostrich : Animal
    40     {
    41         public override void Fly()
    42         {
    43             Console.WriteLine("鸵鸟不会飞!");
    44         }
    45     }
    46     /// <summary>
    47     /// 企鹅
    48     /// </summary>
    49     class Penguin:Animal
    50     {
    51         public override void Fly()
    52         {
    53             Console.WriteLine("企鹅不会飞!");
    54         }
    55     }
    56     /// <summary>
    57     /// 直升机
    58     /// </summary>
    59     class Helicopter : Animal 
    60     {
    61         public override void Fly()
    62         {
    63             Console.WriteLine("直升机会飞!");
    64         }
    65     }
    66 }
    抽象方法Class
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         /*
    12          * 麻省会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 升飞机会飞
    13          * 用多态来实现
    14          * 虚方法
    15          */
    16         static void Main(string[] args)
    17         {
    18             //麻雀
    19             Animal Sparrow = new Sparrow();
    20             Sparrow.Fly();
    21             //鹦鹉
    22             Animal Parrot = new Parrot();
    23             Parrot.Fly();
    24             //鸵鸟
    25             Animal Ostrich = new Ostrich();
    26             Ostrich.Fly();
    27             //企鹅
    28             Animal Penguin = new Penguin();
    29             Penguin.Fly();
    30             //直升机
    31             Animal Helicopter = new Helicopter();
    32             Helicopter.Fly();
    33 
    34             Console.ReadKey();
    35         }
    36     }
    37     
    38 }
    抽象方法Main
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication3
     8 {
     9     interface IAnimalable
    10     {
    11         void Fly();
    12     }
    13     /// <summary>
    14     /// 麻雀
    15     /// </summary>
    16     class Sparrow : IAnimalable
    17     {
    18         public void Fly()
    19         {
    20             Console.WriteLine("麻雀会飞!");
    21         }
    22     }
    23     /// <summary>
    24     /// 鹦鹉
    25     /// </summary>
    26     class Parrot : IAnimalable
    27     {
    28         public void Fly()
    29         {
    30             Console.WriteLine("鹦鹉会飞!");
    31         }
    32     }
    33     /// <summary>
    34     /// 鸵鸟
    35     /// </summary>
    36     class Ostrich : IAnimalable
    37     {
    38         public void Fly()
    39         {
    40             Console.WriteLine("鸵鸟不会飞!");
    41         }
    42     }
    43     /// <summary>
    44     /// 企鹅
    45     /// </summary>
    46     class Penguin : IAnimalable
    47     {
    48         public void Fly()
    49         {
    50             Console.WriteLine("企鹅不会飞!");
    51         }
    52     }
    53     /// <summary>
    54     /// 直升机
    55     /// </summary>
    56     class Helicopter : IAnimalable
    57     {
    58         public void Fly()
    59         {
    60             Console.WriteLine("直升机会飞!");
    61         }
    62     }
    63     
    64 
    65 }
    接口Interface
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication3
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //麻雀会飞  鹦鹉会飞  鸵鸟不会飞 企鹅不会飞  直升飞机会飞
    14             //用多态来实现
    15             //需方法、抽象类、接口
    16 
    17             //麻雀
    18             IAnimalable Sparrow = new Sparrow();
    19             Sparrow.Fly();
    20             //鹦鹉
    21             IAnimalable Parrot = new Parrot();
    22             Parrot.Fly();
    23             //鸵鸟
    24             IAnimalable Ostrich = new Ostrich();
    25             Ostrich.Fly();
    26             //企鹅
    27             IAnimalable Penguin = new Penguin();
    28             Penguin.Fly();
    29             //直升飞机
    30             IAnimalable Helicopter = new Helicopter();
    31             Helicopter.Fly();
    32 
    33             Console.ReadKey();
    34         }
    35     }
    36 }
    接口Main
  • 相关阅读:
    <C#>关于string.Empty & "" & null 的讨论
    c# checked unchecked 关键字 try
    sql2005数据库加锁后解锁
    c#对字符串转义符进行解码
    继承本质论
    javascript中parseInt和Number函数的用法区别
    BIRT 使用说明书
    最后一周
    修改字段
    SQLserver中join
  • 原文地址:https://www.cnblogs.com/2016Study/p/5575338.html
Copyright © 2020-2023  润新知