C#语言基础(二)

?:运算符

条件运算符?:  可以用if...else    例如:Exp1 ? Exp2 : Exp3;

? 表达式的值是由 Exp1 决定的。如果 Exp1 为真,则计算 Exp2 的值,结果即为整个 ? 表达式的值。如果 Exp1 为假,则计算 Exp3 的值,结果即为整个 ? 表达式的值。

一、 C#封装

封装是为了防止对实现细节的访问。访问修饰符 定义了类成员的范围和可见性

带有 internal 访问修饰符的任何成员可以被定义在该成员所定义的应用程序内的任何类或方法访问。

二、C#方法

 1 方法定义
 2 <Access Specifier> <Return Type> <Method Name>(Parameter List)
 3 {
 4    Method Body
 5 }
 6 Access Specifier:访问修饰符,这个决定了变量或方法对于另一个类的可见性。
 7 Return type:返回类型,一个方法可以返回一个值。返回类型是方法返回的值的数据类型。如果方法不返回任何值,则返回类型为 void 8 Method name:方法名称,是一个唯一的标识符,且是大小写敏感的。它不能与类中声明的其他标识符相同。
 9 Parameter list:参数列表,使用圆括号括起来,该参数是用来传递和接收方法的数据。参数列表是指方法的参数类型、顺序和数量。参数是可选的,也就是说,一个方法可能不包含参数。
10 Method body:方法主体,包含了完成任务所需的指令集。
11 
12 方法调用
13 //直接调用 FindMax 方法
14 ret = n.FindMax(a, b);

按引用传参

使用 ref 关键字声明引用参数

 

 1 using System;
 2 namespace CalculatorApplication
 3 {
 4    class NumberManipulator
 5    {
 6       public void swap(ref int x, ref int y)
 7       {
 8          int temp;
 9 
10          temp = x; /* 保存 x 的值 */
11          x = y;    /* 把 y 赋值给 x */
12          y = temp; /* 把 temp 赋值给 y */
13        }
14    
15       static void Main(string[] args)
16       {
17          NumberManipulator n = new NumberManipulator();
18          /* 局部变量定义 */
19          int a = 100;
20          int b = 200;
21 
22          Console.WriteLine("在交换之前,a 的值: {0}", a);
23          Console.WriteLine("在交换之前,b 的值: {0}", b);
24 
25          /* 调用函数来交换值 */
26          n.swap(ref a, ref b);
27 
28          Console.WriteLine("在交换之后,a 的值: {0}", a);
29          Console.WriteLine("在交换之后,b 的值: {0}", b);
30  
31          Console.ReadLine();
32 
33       }
34    }
35 }
View Code

三、C#可空类型

nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。Nullable< bool > 变量可以被赋值为 true 或 false 或 null。

< data_type> ? <variable_name> = null;

Null 合并运算符(??)为类型转换定义了一个预设值,以防可空类型的值为 Null。Null 合并运算符把操作数类型隐式转换为另一个可空(或不可空)的值类型的操作数的类型。

如果第一个操作数的值为 null,则运算符返回第二个操作数的值,否则返回第一个操作数的值。

 1 using System;
 2 namespace CalculatorApplication
 3 {
 4    class NullablesAtShow
 5    {
 6          
 7       static void Main(string[] args)
 8       {
 9          
10          double? num1 = null;
11          double? num2 = 3.14157;
12          double num3;
13          num3 = num1 ?? 5.34;      // num1 如果为空值则返回 5.34
14          Console.WriteLine("num3 的值: {0}", num3);
15          num3 = num2 ?? 5.34;
16          Console.WriteLine("num3 的值: {0}", num3);
17          Console.ReadLine();
18 
19       }
20    }
21 }
22 
23 num3 的值: 5.34
24 num3 的值: 3.14157
View Code

四、数组

 1 using System;
 2 namespace ArrayApplication
 3 {
 4    class MyArray
 5    {
 6       static void Main(string[] args)
 7       {
 8          int []  n = new int[10]; /* n 是一个带有 10 个整数的数组 */
 9          int i,j;
10 
11 
12          /* 初始化数组 n 中的元素 */        
13          for ( i = 0; i < 10; i++ )
14          {
15             n[ i ] = i + 100;
16          }
17 
18          /* 输出每个数组元素的值 */
19          for (j = 0; j < 10; j++ )
20          {
21             Console.WriteLine("Element[{0}] = {1}", j, n[j]);
22          }
23          Console.ReadKey();
24       }
25    }
26 }

五、C#字符串

using System;

namespace StringApplication
{
    class Program
    {
        static void Main(string[] args)
        {
           //字符串,字符串连接
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";

            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);

            //通过使用 string 构造函数
            char[] letters = { 'H', 'e', 'l', 'l','o' };
            string greetings = new string(letters);
            Console.WriteLine("Greetings: {0}", greetings);

            //方法返回字符串
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);

            //用于转化值的格式化方法
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}",
            waiting);
            Console.WriteLine("Message: {0}", chat);
            Console.ReadKey() ;
        }
    }
}

Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 17:58 on Wednesday, 10 October 2012

同时string有很多函数

 

六、C#结构体

  • 类是引用类型,结构是值类型。
  • 结构不支持继承。
  • 结构不能声明默认的构造函数。
  •  1 using System;
     2 using System.Text;
     3      
     4 struct Books
     5 {
     6    private string title;
     7    private string author;
     8    private string subject;
     9    private int book_id;
    10    public void setValues(string t, string a, string s, int id)
    11    {
    12       title = t;
    13       author = a;
    14       subject = s;
    15       book_id =id;
    16    }
    17    public void display()
    18    {
    19       Console.WriteLine("Title : {0}", title);
    20       Console.WriteLine("Author : {0}", author);
    21       Console.WriteLine("Subject : {0}", subject);
    22       Console.WriteLine("Book_id :{0}", book_id);
    23    }
    24 
    25 };  
    26 
    27 public class testStructure
    28 {
    29    public static void Main(string[] args)
    30    {
    31 
    32       Books Book1 = new Books(); /* 声明 Book1,类型为 Books */
    33       Books Book2 = new Books(); /* 声明 Book2,类型为 Books */
    34 
    35       /* book 1 详述 */
    36       Book1.setValues("C Programming",
    37       "Nuha Ali", "C Programming Tutorial",6495407);
    38 
    39       /* book 2 详述 */
    40       Book2.setValues("Telecom Billing",
    41       "Zara Ali", "Telecom Billing Tutorial", 6495700);
    42 
    43       /* 打印 Book1 信息 */
    44       Book1.display();
    45 
    46       /* 打印 Book2 信息 */
    47       Book2.display();
    48 
    49       Console.ReadKey();
    50 
    51    }
    52 }
    53 
    54 Title : C Programming
    55 Author : Nuha Ali
    56 Subject : C Programming Tutorial
    57 Book_id : 6495407
    58 Title : Telecom Billing
    59 Author : Zara Ali
    60 Subject : Telecom Billing Tutorial
    61 Book_id : 6495700

六、C#继承

 1 using System;
 2 namespace RectangleApplication
 3 {
 4    class Rectangle
 5    {
 6       // 成员变量
 7       protected double length;
 8       protected double width;
 9       public Rectangle(double l, double w)
10       {
11          length = l;
12          width = w;
13       }
14       public double GetArea()
15       {
16          return length * width;
17       }
18       public void Display()
19       {
20          Console.WriteLine("长度: {0}", length);
21          Console.WriteLine("宽度: {0}", width);
22          Console.WriteLine("面积: {0}", GetArea());
23       }
24    }//end class Rectangle  
25    class Tabletop : Rectangle
26    {
27       private double cost;
28       public Tabletop(double l, double w) : base(l, w)
29       { }
30       public double GetCost()
31       {
32          double cost;
33          cost = GetArea() * 70;
34          return cost;
35       }
36       public void Display()
37       {
38          base.Display();
39          Console.WriteLine("成本: {0}", GetCost());
40       }
41    }
42    class ExecuteRectangle
43    {
44       static void Main(string[] args)
45       {
46          Tabletop t = new Tabletop(4.5, 7.5);
47          t.Display();
48          Console.ReadLine();
49       } 
50    }
51 }

C# 不支持多重继承。但是,您可以使用接口来实现多重继承。

 1 using System;
 2 namespace InheritanceApplication
 3 {
 4    class Shape
 5    {
 6       public void setWidth(int w)
 7       {
 8          width = w;
 9       }
10       public void setHeight(int h)
11       {
12          height = h;
13       }
14       protected int width;
15       protected int height;
16    }
17 
18    // 基类 PaintCost
19    public interface PaintCost
20    {
21       int getCost(int area);
22 
23    }
24    // 派生类
25    class Rectangle : Shape, PaintCost
26    {
27       public int getArea()
28       {
29          return (width * height);
30       }
31       public int getCost(int area)
32       {
33          return area * 70;
34       }
35    }
36    class RectangleTester
37    {
38       static void Main(string[] args)
39       {
40          Rectangle Rect = new Rectangle();
41          int area;
42          Rect.setWidth(5);
43          Rect.setHeight(7);
44          area = Rect.getArea();
45          // 打印对象的面积
46          Console.WriteLine("总面积: {0}",  Rect.getArea());
47          Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
48          Console.ReadKey();
49       }
50    }
51 }
View Code

七、C#的多态

一个接口,多个功能。

在 C# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 Object。

包括静态多态,动态多态

静态多态分为:函数响应在编译时发生。包括,函数重载,运算符重载

动态多态分为:函数响应在运行时发生。 包括  关键字 abstract 创建抽象类,用于提供接口的部分类的实现

 

posted @ 2021-11-29 22:05  sylvia11  阅读(38)  评论(0编辑  收藏  举报