C# 基础知识 (2)

写在前面

判断、循环、构造方法、方法重载等,和java语言基本类似。
C#中的goto、析构函数、readonly 只读对象、partial 类的一部分是java中没有的。

1、判断与循环

break 用于中断循环
continue 跳过本次循环

if…else

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入一个整数:");
        //将从控制台输入的值转换成int类型
        int num = int.Parse(Console.ReadLine());
        if (num % 2 == 0)
        {
            Console.WriteLine(num+"是2的倍数!");
        }else if(num % 3 == 0){
    		Console.WriteLine(num+"是3的倍数!");
		}
        else
        {
            Console.WriteLine(num+"不是2或3的倍数!");
        }
    }
}

switch…case

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入成绩(0~100的整数)");
        int points = int.Parse(Console.ReadLine());
        switch (points / 10)
        {
            case 10:
                Console.WriteLine("优秀");
                break;
            case 9:
                Console.WriteLine("及格");
                break;
            default:
                Console.WriteLine("不及格");
                break;
        }
    }
}

如果case 10和case 9输出的内容一样,还可以像下面这样

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入学生考试的成绩(0~100的整数)");
        int points = int.Parse(Console.ReadLine());
        if(points < 0 || points > 100)
        {
            points = 0;
        }
        switch (points / 10)
        {
            case 10:
            case 9:
                Console.WriteLine("优秀");
                break;
            case 8:
                Console.WriteLine("良好");
                break;
            case 7:
            case 6:
                Console.WriteLine("及格");
                break;
            default:
                Console.WriteLine("不及格");
                break;
        }
    }
}

for

class Program
{
    static void Main(string[] args)
    {
        //设置存放和的变量
        int sum = 0;
        for(int i = 1; i <= 10; i++)
        {
            Console.WriteLine(i);
            sum += i;
        }
        Console.WriteLine("1~10的和为:" + sum);
    }  
}

while

class Program
{
    static void Main(string[] args)
    {
        int i = 1;
        int sum = 0;//存放1~10的和
        while (i <= 10)
        {
            sum = sum + i;
            Console.WriteLine(i);
            i++;
        }
        Console.WriteLine("1~10的和为:" + sum);
    }  
}

do while

class Program
{
    static void Main(string[] args)
    {
        int i = 1;
        do
        {
            Console.WriteLine(i);
            i++;
        } while (i <= 10);
    }  
}

goto

class Program
{
    static void Main(string[] args)
    {
        int count = 1;
    login:
        Console.WriteLine("请输入用户名");
        string username = Console.ReadLine();
        Console.WriteLine("请输入密码");
        string userpwd = Console.ReadLine();
        if (username == "aaa" && userpwd == "123")
        {
            Console.WriteLine("登录成功");
        }
        else
        {
            count++;
            if (count > 3)
            {
                Console.WriteLine("用户名或密码错误次数过多!退出!");
            }
            else
            {
                Console.WriteLine("用户名或密码错误");
                goto login;//返回login标签处重新输入用户名密码
            }
        }
    }
}

2、类、对象、方法

一个命名空间里可以有多个类

using System;

namespace code_1
{
    class Test
    {
    }
    class Test1
    {
    }
}

类定义的具体语法形式如下。

类的访问修饰符    修饰符    类名
{
    类的成员
}

其中:

  • 类的访问修饰符:用于设定对类的访问限制,包括 public、internal 或者不写,用 internal 或者不写时代表只能在当前项目中访问类;public 则代表可以在任何项目中访问类。
  • 修饰符:修饰符是对类本身特点的描述,包括 abstract、sealed 和 static。abstract 是抽象的意思,使用它修饰符的类不能被实例化;sealed 修饰的类是密封类,不能 被继承;static 修饰的类是静态类,不能被实例化。
  • 类名:类名用于描述类的功能,因此在定义类名时最好是具有实际意义,这样方便用户理解类中描述的内容。在同一个命名空间下类名必须是唯一的。
    类的成员:在类中能定义的元素,主要包括字段、属性、方法。

字段在类中定义完成后,在类加载时,会自动为字段赋值。

namespace code_1
{
    class Test
    {
        private int id;                         //定义私有的整型字段 id,自动赋值0
        public readonly string name;            //定义公有的只读字符串类型字段 name,自动赋值空值
        internal static int age;                //定义内部的静态的整型字段 age,自动赋值0
        private const string major = "计算机";  //定义私有的字符串类型常量 major,已有初始值
    }
}

2.1、readonly 只读对象

在C#中运用只读字段主要有以下几个关键:
  (1)只读字段能够在界说的一起赋值或者在类的结构办法中给其赋值;
  (2)除了结构办法外,其他地方不能够修正只读字段的值;
  (3)只读字段的特点只能有get访问器,不能有set,这是显而易见的;

2.2、const 常量

const 常量只能在声明的一起初始化(赋值)。
readonly字段能够在声明或结构函数中初始化。

2.3、internal 内部对象

internal(内部):限定的是只有在同一程序集中可访问,可以跨类

2.4、static 静态对象

静态对象只有一份,所有的静态成员就会被创建在“静态存储区”里面,一旦创建直到程序退出,才会被回收。

2.4、get、set 访问器

namespace code_1
{
    class Book
    {
        private int id;
        private string name;
        private double price;
        private String desc;

        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

		// 也可以这样简写
		public string Name{get; set;}

		// 只读形式
		public double price{get;}=10.5;

		// 如果不允许其他类访问属性值
		public String desc{private get; set;}
    }
}

2.5、调用类成员

class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
    public void PrintMsg()
    {
        Console.WriteLine("图书编号:" + Id);
        Console.WriteLine("图书名称:" + Name);
        Console.WriteLine("图书价格:" + Price);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Book book = new Book();
        //为属性赋值
        book.Id = 1;
        book.Name = "计算机基础";
        book.Price = 34.5;
        book.PrintMsg();
    }
}

将 Book 类中的属性和方法都更改为静态的

class Book
{
    public static int Id { get; set; }
    public static string Name { get; set; }
    public static double Price { get; set; }
    public static void SetBook(int id, string name, double price)
    {
        Id = id;
        Name = name;
        Price = price;
    }
    public static void PrintMsg()
    {
        Console.WriteLine("图书编号:" + Id);
        Console.WriteLine("图书名称:" + Name);
        Console.WriteLine("图书价格:" + Price);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Book.SetBook(1, "计算机基础", 34.5);
        Book.PrintMsg();
    }
}

2.6、构造函数

构造函数用在创建类的对象时执行

class User
{
	// 与类同名的就是构造函数
    public User(string name, string password, string tel)
    {
        this.Name = name;
        this.Password = password;
        this.Tel = tel;
    }
    public string Name { get; set; }
    public string Password { get; set; }
    public string Tel { get; set; }
    public void PrintMsg()
    {
        Console.WriteLine("用户名:" + this.Name);
        Console.WriteLine("密  码:" + this.Password);
        Console.WriteLine("手机号:" + this.Tel);
    }
}


// 调用实例
class Program
{
    static void Main(string[] args)
    {
        User user = new User("小明","123456","13131351111");
        user.PrintMsg();
    }
}

2.7、析构函数

析构方法在垃圾回收、释放资源时使用。析构方法是在类操作完成后调用的。

// ~类名,就是一个析构方法
~User()
{
    Console.WriteLine("调用了析构方法");
}

2.8、lambda表达式

class LambdaClass
{
    public static int Add1(int a, int b) => a + b;
public static void Add2(int a, int b) => Console.WriteLine(a + b);
}

2.9、嵌套类

class OuterClass
{
    public class InnerClass
    {
        public string CardId { get; set; }
        public string Password { get; set; }
        public void PrintMsg()
        {
            Console.WriteLine("卡号为:" + CardId);
            Console.WriteLine("密码为:" + Password);
        }
    }
}


// 嵌套类的调用
class Program
{
    static void Main(string[] args)
    {
        OuterClass.InnerClass outInner = new OuterClass.InnerClass();
        outInner.CardId = "622211100";
        outInner.Password = "123456";
        outInner.PrintMsg();
    }
}

2.10、partial 类的一部分

一个类可以由多个部分组成,如下

public partial class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Points { get; set; }
}
public partial class Course
{
    public void PrintCoures()
    {
        Console.WriteLine("课程编号:" + Id);
        Console.WriteLine("课程名称:" + Name);
        Console.WriteLine("课程学分:" + Points);
    }
}

partial 也可以用来修饰方法,
使用部分方法需要注意如下 3 点:

  • 部分方法必须是私有的,并且不能使用 virtual、abstract、override、new、sealed、extern 等修饰符。
  • 部分方法不能有返回值。
  • 在部分方法中不能使用 out 类型的参数。
public partial class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Points { get; set; }
    partial void PrintCourse();
    //调用PrintCourse方法
    public void PrintMsg()
    {
        PrintCourse();
    }
}
public partial class Course
{
    public void PrintCoures()
    {
        Console.WriteLine("课程编号:" + Id);
        Console.WriteLine("课程名称:" + Name);
        Console.WriteLine("课程学分:" + Points);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值