机器视觉 第六章 C# 基础
·
一.for循环 双重for循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ZhiYouDay7_1
{
internal class Program
{
static void Main(string[] args)
{
//int i = 0 条件的初始变量
// i < length 循环的条件 条件成立循环执行 反之循环终止
//i++ 每次循环结束后 初始变量的自增或自减
//{循环代码}
//for (int i = 0; i < length; i++)
//{
//}
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine("1");
//}
//for (float i = 0; i < 5.5f; i--)
//{
// Console.WriteLine("2");
//}
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine("3");
//}
////for循环嵌套
//for (int i = 0; i < 5; i++)
//{
// Console.WriteLine("2");
// for (int j = 0; j < 10; j++)
// {
// Console.WriteLine("1");
// for (int k = 0; k < 10; k++)
// {
// Console.WriteLine("3");
// }
// }
//}
////break 关键字 用于循环体中
////break 终止当前循环体
//for (int i = 0; i < 10; i++)
//{
// if (i == 1)
// {
// break;
// }
// Console.WriteLine("1");
// // break;
//}
////continue 关键字 用于循环体中
////continue 终止当次循环 进入下次循环
//for (int i = 0; i < 10; i++)
//{
// if (i == 0)
// {
// continue;
// }
// Console.WriteLine("1");
// // continue;
//}
//// 例子 :输出1-100 除了 3 ,7 ,9
//for (int i = 1; i <= 100; i++)
//{
// if (i == 3 || i == 7 || i == 9)
// {
// continue;
// }
// Console.WriteLine(i);
//}
//// 例子 :输出1-50 但是循环条件是 i<100
//for (int i = 1; i < 100; i++)
//{
// if (i == 51) {
// break;
// }
// if (i > 50)
// {
// continue;
// }
// //Console.WriteLine();
//}
// 无限循环1
//输入密码 如果密码正确 程序结束 反之 提示密码错误重新输入
//for (; ; )
//{
// Console.WriteLine("请输入密码:");
// string passWordStr = Console.ReadLine();
// if (passWordStr == "123456")
// {
// Console.WriteLine("登录成功");
// break;
// }
// else
// {
// Console.WriteLine("密码错误");
// }
//}
//// 无限循环2
//while (true) {
//}
Console.ReadKey();
}
}
}
二.goto语句
//goto 语句 跳转语句
//LOGIN:
//Console.WriteLine("请输入密码:");
//string passWordStr = Console.ReadLine();
//if (passWordStr == "123456")
//{
// Console.WriteLine("登录成功");
//}
//else
//{
// Console.WriteLine("密码错误");
// goto LOGIN;
//}
三. swich语句 条件分支语句 类似if语句
//
//1.switch()中的变量可以是整形、string、bool、char、枚举或相应可以为null的类型,
//2.每个case标签后是常量表达式(定值)的值,不必连续,
// 也不必按特定顺序排列,但不能相同,否则会出现错误。
//3.default标签可选,意思是其余的结果。 类似与else
//4.break语句的作用是在case与case之间中断程序。
//switch (switch_on)
//{
// case:
// break;
// case:
// break;
// case:
// break;
// default:
//}
Console.WriteLine("请输入数字:");
string strNumber = Console.ReadLine();
int intNumber = int.Parse(strNumber);
switch (intNumber)
{
case 1:
{
Console.WriteLine("数字1");
}
break;
case 2:
{
Console.WriteLine("数字2");
}
break;
case 3:
{
Console.WriteLine("数字3");
}
break;
default:
{
Console.WriteLine("数字10000");
}
break;
}
四.访问修饰符
作用
所有类型成员都具有可访问级别。
分类
1.public
public : 同一程序集的其他任何代码或引用该程序集的其他程序集都可以访问该类型或成员。
2.internal : 同一程序集中的任何代码都可以访问该类型或成员,但其他程序集不可以访问。
3.private : 同一类和结构的代码可以访问该类型和成员。
4.protected : 同一类和派生(继承特性)类中的代码可以访问该类型和成员。
5.protected internal : 同一程序集中的任何代码或其他程序集中的任何派生类都可以访问该类型或成员。
6.private protected:该类型或成员可以通过从 class 派生的类型访问


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhiYouDay7_2ClassLibrary1;
namespace ZhiYouDay7_2
{
internal class Program
{
static void Main(string[] args)
{
//1.访问修饰符
//作用
//所有类型和类型成员都具有可访问性级别. 通过访问修饰符的使用 来限制或者保护数据的一种形式
/*
分类
public (跑步雷克): 同一程序集的其他任何代码或引用该程序集的其他程序集都可以访问该类型或成员。
internal (因特咯): 同一程序集中的任何代码都可以访问该类型或成员,但其他程序集不可以访问。
private (怕热的): 同一类和结构的代码可以访问该类型和成员。
protected (普泰忒): 同一类和派生(继承特性)类中的代码可以访问该类型和成员。
protected internal : 同一程序集中的任何代码或其他程序集中的任何派生类都可以访问该类型或成员。
private protected:该类型或成员可以通过从 class 派生的类型访问
*/
//同一程序集访问public描述的类
PublicClass publicClass = new PublicClass();
//不同程序集访问public描述的类
ClassLib classLib = new ClassLib();
//同一程序集访问Internal描述的类
InternalClass internalClass = new InternalClass();
//不同程序集访问Internal描述的类 是无法直接使用的
// InternalClassLib
//类中的私有成员 无法在类的外部直接调用
// ClassA.Mothod();
// 类中的共有成员 可以在类的外部直接调用
ClassA.Mothod1();
// 类中的Internal成员 可以在类的外部直接调用
ClassA.Mothod2();
// 类中的 protected l成员 无法在(没有继承关系)的类中直接调用
// People.Mothod();
People.Mothod3();
Console.ReadKey();
}
}
// 类、结构体的默认修饰符是internal。
public class PublicClass {
}
internal class InternalClass {
}
class Class1
{
//同一程序集访问public描述的类
PublicClass PublicClass { get; set; }
}
//类不能被描述为private
public class ClassA
{
// 类中所有的成员默认修饰符是private
//ClassA 中的函数
private static void Mothod() {
}
public static void Mothod1() {
Console.WriteLine("Mothod1");
}
internal static void Mothod2()
{
Console.WriteLine("Mothod2");
}
}
public class People {
protected static void Mothod()
{
Console.WriteLine("1");
}
public static void Mothod1()
{
// 类中的 protected成员 可以在本类中直接调用
Mothod();
Mothod3();
Mothod4();
}
protected internal static void Mothod3() {
}
private protected static void Mothod4()
{
}
}
public class Boy :People{
static void Mothod2()
{
// 类中的 protected成员 可以在继承类中直接调用
Mothod();
Mothod3();
Mothod4();
}
}
//C#的默认修饰符
//类、结构体的默认修饰符是internal。
//类中所有的成员默认修饰符是private。
//接口默认修饰符是internal。
//接口的成员默认修饰符是public。
//枚举类型成员默认修饰符是public。
//委托的默认修饰符是internal。
}
引入的C库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhiYouDay7_2ClassLibrary1
{
public class ClassLib
{
}
internal class InternalClassLib {
}
}
更多推荐



所有评论(0)