zoukankan      html  css  js  c++  java
  • C#学习(二)

    这周主要学习了一些基础知识,大部分都和C++、JAVA很像,来看看吧。

    1. 简单的变量。变量如何被赋值、静态变量不能被赋值、简单的for循环、while循环、do-while循环,都与C++、JAVA等十分相似。

    下面是简单的变量赋值、静态变量如何不能被赋值、简单的if语句和switch语句。

    ··········································································变量赋值···············································································

    ·······································································静态变量不能被赋值····································································

    ·······················································if语句··········································································

     ·····································································switch语句········································································· 

    2. 显示转换。一般情况下,值域大的变量不能赋值给值域小的变量,但是可以通过显示的转换进行这项操作。

    1 short x;
    2 int y = 500;
    3 //赋值语句: 值域小的变量 = 值域大的变量;
    4 x = y ; // does not compile 
    5 x =(short) y; //cast, explicit conversion OK

    但是我们要保证赋给x的值不能超过它能表示的最大值。

    3. 特殊的goto语句

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Week2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int testme = 2;
    14             switch (testme)
    15             {
    16                 case 2:
    17                     testme = 5;
    18                     Console.WriteLine("testme = " + testme);
    19                     goto case 3;    //执行case 3
    20                 case 3:
    21                     testme = 1;
    22                     Console.WriteLine("testme = " + testme);
    23                     break;
    24                 default:
    25                     testme = 2;
    26                     Console.WriteLine("testme = " + testme);
    27                     break;
    28             }
    29         }
    30 
    31     }
    32 }

    执行结果:

    这说明执行完case 2又执行了case3.

    4. 对象和类。

    新建类,定义它的成员变量和方法。如果没有构造函数,编译器会自动生成一个无参数的构造函数。没有访问修饰符的成员会被定义成私有。C#共有5种访问修饰符:

    下面是自定义的Time类(没有定义有实际作用的方法,这里只是练习一下创建并使用类):

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Week2
     8 {
     9     class Time
    10     {
    11         // private variables: 没有访问修饰符的成员为私有类型
    12         int Year; 
    13         int Month; 
    14         int Date;
    15         int Hour; 
    16         int Minute; 
    17         int Second;
    18 
    19         // public methods: explicitly(显式)定义为 public
    20         public void DisplayCurrentTime()
    21         {
    22             Console.WriteLine("stub for DisplayCurrentTime");
    23         }
    24 
    25     }
    26 }

    定义类对象并调用类方法:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Week2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Time time = new Time();
    14             time.DisplayCurrentTime();
    15         }
    16 
    17     }
    18 }

    运行结果

    调用DisplayCurrentTime()成功。

  • 相关阅读:
    HDU 1104 Remainder (POJ 2426 BFS+数论)
    POJ 1398 Complete the sequence! ★ (差分)
    POJ 2917 Diophantus of Alexandria ★(数论)
    POJ 1948 Triangular Pastures (二维01背包)
    POJ 1948 Triangular Pastures (二维01背包)
    starter kits 介绍
    starter kits 介绍
    信息分析与预测复习题(绪论)
    信息分析与预测复习题(绪论)
    starter kits 介绍
  • 原文地址:https://www.cnblogs.com/yongheng20/p/4347875.html
Copyright © 2011-2022 走看看